C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Monday 24 April 2017

C++ Function Prototype (declaration)

                        C++ Function Prototype (declaration)


If a user - defined function is defined after main ()  function, compiler will show error. It is because compiler is unaware of user-defines function, types of argument passed to function and return type.

In C++, the function prototype is a declaration of function without its body to give compiler information about the user-defined function. Function prototype in the above example is : 


int add(int , int);


You can see that, there is no body of the function in prototype. Also, there are only return type of arguments but no arguments. You can also declare function prototype as below but it's not necessary to write arguments.


 int add(int a, int b);

Note: It is not necessary to define prototype if the user- defined function exixts before main() function.

Function Call


To execute the codes of function body, the user defined function needs to be invoked (called).

In the above program, add(num1, num2); inside main () function calls the user-defined function.

The function returns an integer which is stored in variable add.


Function Defination


The function itself is referred as function definition. Function definition in the above program is : 
























When the function is called, control is transfered to the first statement of the function body.

Then, other statements in function body are executed sequentially.

When all codes insides function is executed , control of program moves to the calling program.


Passing Arguments to Function

In programming , argument (parameter) refer to the data which is passed to a function (function definition) while calling it.

In the above example, two variables, num1 and num2 are passed to function during function call. these arguments are known as actual arguments.

The value of num1 and num2 are initilized to variables a and b respectively. These arguments a and b called formal arguments.

This is demonstrated in figure below : 


























Notes on passing arguments

The numbers of actual arguments and formals arguments and formals argument should be the same . (Exception: Function Overloading)

The type of first actual argument should match the type of first formal argument. Similarly, type of second actual argument should match the type of second formal argument and so on.

You may call function a without passing any argument. The number (s) of argument passed to a function depends on how programmer want to solve the program.

You may assign default values to the argument. These arguments are known as default arguments.

In the above program, both arguments are of int type. But it's not necessary to ahve both arguments of same type.


Return Statement


A function can return a single value to the calling program using return statement.

In the above program, the value of add returned from user-defined function to the calling program using statement below:

return add;  

The figur below demonstrates the working of return statement.


























In  the above program, the value of add inside user-defined function is returned to the calling function. The value is then stored to a variable sum.

Notice that the variable returned, i.e., add is of type int and sum is also of int type.

Also, notice that the return type of a function is defined in function declarator int add (int a, int b) . The int before add (int a, int b ) means the function should return value of type int.

If no Value is returned to thr calling function then, void should be used.


Saturday 22 April 2017

C++ Functions

                                         C++  Functions


In Programming, function refers ti a segment that groups code to perform a specific task. 


Depending on whether a function is predefined or created by the programmer, there are two types of function: 

1.   Library Function
2.   User-defined Function

Library Function  -


Library functions are the built-in function in C++ programming.

Programmer can use library function by invoking function directly, they don't need to write it themselves.


Example :   Library  Function



















Output :



In the example above , squrt() library function is invoked to calculate the square root of number.


Notice code #include<cmath> in the above program,Here, cmath is a header file. The function definition of Sqrt() (body type function) is present in the cmat   header file. 


You can use all function defined in cmath when you include hte content of file cmath  in this program using #include <cmath>


Every valid C++ program has at least one function, that is main() function


User- defined Function :


C++ allows programmer to define their own function.

A user-defined function group code to perform a specific task and that group of codes defined in the body of function.


How user-defined function works in C Programming ?


When  a program begins runnung, the system calls the main() function, that is, the system Starts executing codes from main() function.  

When control of the program reaches to function name() inside main(), it moves to void function name() and all codes inside void function name () is executed.

Then, control of the program moves back to the main function where the code after the call to the function name () is executed .


Example User Defined Function  :

C++ program to add two integers, Make a function add()     to add integers and display sum in       main () function.






















Output  :






Sunday 16 April 2017

C++ Inheritance

                                                          C++  INHERITANCE


Inheritance is one of the key features of Object-oriented programming in C++. It allows user to create a new class (derived class) from an existing class (base class).

The derived class inherits all the features from the base class and can have additional features of its own.

Why inheritance should be used?

Suppose in your game, you want three characters - a maths teacher, a footballer, and a businessman.
Since all of the characters are persons, they can walk and talk. However, they also have some special skills. A maths teacher can teach maths, a footballer can play football and a businessman can run a business.

You can individually create three classes who can walk, talk and perform their special skill as shown in the figure below.

In each of the classes, you would be copying the same code for a walk and talk for each character.

If you want to add a new feature-eat, you need to implement the same code for each character. This can easily become error prone (when copying) and duplicate codes.

It'd be a lot easier if we had a Person class with basic features like talk, walk, eat sleep, and add special skills to those features as per our characters, This is done using inheritance,




Using inheritance, new you don,t implement the same code for the walk and talk for each class. You just need to inherit them.

So, for Maths teacher (derived class), You inherit all feature os a Person (base class ) and add a new feature Teach Maths. Likewise., for a footballer, you inherit all the features of a person and add a new feature Play Football and so on.

This makes your code cleaner, understandable and extendable.


Implementation of inheritance in C++ Programming:

















In above ex. , class Person is a base class and classes Maths Teacher and Footballer are the derived from Person.


The derived class appears with the declaration of a class followed by a colon, the keyword public and name of a base class from which it is derived.

Since Maths Teacher and Footballer are derived from Person, all data member and a member function of Person can be accessible from them.


Access specifiers in Inheritance:

When creating a derived class from a base class, you can use different access specifiers to inherit the data member of the base class.

These can be public, protected or private.

in the above example, the base class Person has been inherited public-ly by MathsTeacher and Footballer.


Member Function Overriding Inheritance:


Suppose, a base class and derived class has a member function with same name and arguments.

If you create an object of the derived class and try to access that member function, the function in derived is only invoked.

The member function of derived class overrides the member function of the base class.





Saturday 15 April 2017

C++ Multidimensional Arrays

                              C++ Multidimensional Arrays


In C++, you can create an array of an array known as multidimensional array. 

For Example : 


int   x [3] [4];

Here, x is two dimensionall array . it can hold a maximum of 12 elements.

You can think this array as table with  3 rows and each rows has 4 columns .

for example. :

Three-dimensional array also works in a similar way. For example :


float x [2][4][3];

This array x can hold a maximum of 24 elements. You can think this example as : Each of the 2 elements can hold 4 elements, which makes 8 elements and each of those 8 elements can hold 3 elements. Hence, total number of elements this array can hold is 24.


Multidimensional Array Initialisation :

you can initialise a multidimmensional array in more than one way.

Initialisation of two dimensional array:


int test[2][3] = { 2, 4, -5, 9, 0, 9};

Better way to initialise this array with same array elements as above.

int  test[2][3]  = { { 2, 4, 5,},{9, 0, 0}};

Initialisation of  three  dimensional  array

int test [2][3][4] = {3, 4, 2, 3, 0,, -3, 9, 11, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};

Better way to initialise this array with same elements as above.

Example - Two Dimensional Array

C++ program to store temperature of two different cities for a week and display it.




































Output :

































Example - Three Dimensional Array

C++ Program to store value entered by user in three dimensional 
array and display it.





































Output :

Enter 12 value:











































C++ Pointer and Arrays

                                 C++ Pointer and Arrays


Pointers are the variables that hold address. Not only can pointers store address of a single variable,it can also store address of cells of an array.

Consider this example. :



























Suppose, pointer needs to point to the fourth element of an array, that is hold address of fourth array element in above case.

Since ptr points to the third element in the above example, ptr. + 1 will point to the fourth element.

You may think , ptr +1 gives you the address of next byte to the ptr. But it's not correct.

This is because pointer ptr is a pointer to an int and size of int is fixed for a operating system (size in int is 4 byte of 64-bit operating system). Hence, the address between ptr and ptr+1 differs by 4 bytes.

If pointer ptr was pinter to char then, the address between ptr+1 would have differed by 1 byte since size of a character is 1 byte.

Example : Pointer and Arrays

C++ Program to display address of elements of an array using both array and pointers


           #include <iostream>


























Output :
In the above program, a different pointer ptr is used for display the address of array elements arr.

But, array element can be accessed using pointer notation by using same array name arr.                  

 For example : 















Example : Pointer and Arrays

C++ program to display address of array elements using pointer. notation.






















Output :





















You know that, pointer ptr holds the address and expression *ptr gives the value stored in the address.

Simillary, you can get the value stored in the pointerptr + 1 using*(ptr+1).

Consider this code below : 

int ptr[5] = {3, 4, 5, 5, 3};

  • &ptr[0] is equal to ptr and *ptr is equal to ptr[0]
  • &ptr[1] is equal to ptr + 1 and *(ptr+1) is equal to ptr[1] 
  • &ptr[2] is equal to ptr + 2 and *(ptr+2) is equal to ptr[2] 
  • &ptr[i] is equal to ptr + i and *(ptr+i) is equal to ptr[i] 

Example: C++ pointer and Array


C++ Program to insert and display data entered by using pointers notation.




























Output : 







Popular Posts

Recent Posts

Categories

Unordered List

Text Widget