C++

Important Trems in C++

1. Object
2. Class
3. Data Abstracton
4. Encaptulation
5. Inheritance
6. Polymorphism

OBJECT :In real world to represent a thing by its properties or attribute, such as thing -fan, attributes- color,speed,weight etc. In c++ object is a basic run time entity that can represent any person ,place or thing.It is an instance of class.

CLASS :A group of similar type of objects that share common properties and relationships.Class is a user define data type.

DATA ABSTRACTION :It is a concept by which we can represent any object by its essential features without getting its inner details.Just like we know any person by its name without knowing its inner details.

ENCAPSULATION :It is process of wrapping of data and functions into a single unit is called encapsulation.

INHERITANCE :Inheritance is a process by which object of one class can acquire the properties of another class.

POLYMORPHISM :It is ability to take more than one form, just like a person play role is different in different place.


DIFFERENCE BETWEEN MAIN() FUNCTION IN C AND C++

C++ C
1 In C++ force the main() function to return a value of type int. Example return(0); But in C it is not so.
2 The supply of function prototype is must in C++ But not essential in C
3 The function declaration must define not only the return type but also the type of parameters the function is going to use. But not essential in C
4 Function overloading and operator overloading is only takes place in c++ But no such facility is available in C



FUNCTION PROTOTYPING

The prototype describe the function interface to the compiler by giving detail such as the number and the type of argument and the type of return value.When a function is called the compiler use the template to ensure that proper argument are passed and the return value is treated correctly.Any variation in matching the argument or the return type will be cought by the compiler at the time of compilation itself.
InC++ prototype is essential. But in C it is optional.
type function_name(argument-list)
Examples:
1. float add(int a,float b,float c);
2. float add (int a,float b,c);
3. float add(int,float,float);

In Line Function

Inline function is function that replace their call with their definition at the time of compilation.one of the objective of using function in a program is to save memory space which is profitable when a function is likely to be called many times.However every time a function is called, it takes a lot of extra time in executing a series of instruction for task such as:

1. Jumping to the function
2. Saving registers
3. Pushing argument into the stack and return to calling function.
When a function is small then it spent our more time in above given task.

! So to eliminate the cost of calls to small function.

SYNTAX Inline function_header
{
function body
}
Example:
Inline void f()
{
cout<<" My inline function";
}

void main()
{
f();
f();
f();
f();
}
Note: Some restriction with Inline function
Inline function does not have following:
Loop
if-else statement
Break statement
Continue statements
GoTo statement
While,DO-while,Switch.

DEFAULT ARGUMENT

c++ allows us to call its function without specifying all it arguments. In such cases the function assign a default value to the parameter which does not have a matching arguments in function call.Default value is specified when function is declared.
int fun(int a, int b)
{
statements;
}
main()
{
fun(a);
}
If argument not matched compiler provide some default value.We must add default value from right to left.

CONSTANT ARGUMENT

Const keyword is used. It tells the compiler that the function should not modify the argument.This type of deceleration is done only when we pass argument by reference or by pointer.
Example:
int strlen (count char *a);
int length(count string &s);
The above statements are at declaration end.

OVERLOADING

When we use same thing for different purpose than it is known as overloading.In C only predefined overloading is takes place such as & operator. In C & is used as address and used for logical operator.In C no user define overloading is possible.But C++ provide facility of user define Overloading for function and operators.

FUNCTION OVERLOADING
In function overloading we use same function name to create function that perform variety of different task.

Example
//Declaration
int add(int x,int y);
int add(int x,int y,int z);
float add(float a,float b);
float add(float a,float b,float c);
// Function Call
cout<<add(2,5);
cout<<add(2,5,0);
cout<<add(5.5,4.5);
In this scheme a function call first match the prototype having the same number of argument and type of argument and than call the appropriate function for the execution.
Steps for selection of function by arguments
1. The compiler first tries to find an exect match in which the type of actual argument are the same and use that function.
2. If an exact match is not found the compiler use the compatibility method by which lower is casted into upper in the sequence of waterfall model such as: casting sequence(char->int->float->double)
3. When above two conditions are fail and if a function call match with two function definition then this situation is known as ambiguous function call and gives an error.Such as: long add(long a); and double add(double a) from called area, In calling area write add(5); now here 5 is int and int is converted into both long as well as double it is a condition of ambiguous.

Example of function Overloading

# include < iostream.h >
overload volume;
int volume(int); // prototype declaration
double volume(double,int); // for overloading volume
long volume(long,int,int);

main()
{
cout<<volume(5); <<"\n";
cout<<volume(5.2,8); <<"\n";
cout<<volume(1001,75,15); <<"\n";
}
// function definition part

int volume(int a)
{
return(a*a*a);
}
double volume(double a,int b)
{
return(3.14*a*a*h);
}
long volume(long a,int b,int c)
{
return(a*b*c);
}
Note:
-> We should not overload unrelated functions
-> Some time default argument is used instead of overloading. It reduce the number of function to be define.
-> Overloading function are extensively used for handing class objects.


No comments:

Post a Comment