Passing objects As arguments An object can be passed as an argument to a function by the following ways: Pass-by-value, a copy of the entire object is passed to the function . Pass-by-reference, only the address of the object is passed implictly to the function. Pass- by-pointer, the address of the object is passed explicitly to the function.
Passing objects by value A copy of the object is passed to the function and any modifications made to the object inside the function is not reflected in the object used to call the function.
E.g. Pass –By-value #include <iostream.h> void swapnum(int c, int d) { int temp = c; c = d; d = temp; } int main(void) // input a=10 ---- 1000 { b=20--- 2000 int a = 10; //output c=10— 3000 int b = 20; d=20---4000 not reflected swapnum(a, b); cout<<"A is %d and B is %d\n“<< a, b; return 0;
Passing objects by reference An address of the object is passed to the function and any changes made to the object inside the function is reflected in the actual object.
E.g. Pass –By-reference #include <iostream.h> void swapnum(int &c, int &d) { int temp = c; c = d; d = temp; } int main(void) { // Alais name,reflected,value swap int a = 10; //input a=10 ---1000 int b = 20; b=20----2000 //output c=20---1000 swapnum(a, b); d=10---2000 cout<<"A is %d and B is %d\n“<< a, b; return 0;
Passing objects by Pointer The address of the object is passed explicitly to the function.
E.g. Pass –By-Pointer #include <iostream.h> void swapnum(int * c, int *d) { int temp = c; c = d; d = temp; } int main(void) int a = 10; // address swap int b = 20; swapnum(&a, &b); cout<<"A is %d and B is %d\n“<< a, b; return 0;
Friend Function and Friend Classes The concept of encapsulation and data hiding dictate that non-member functions should not be allowed to access an object’s private and protected members. Using friend function or friend class a non-member function can able to access private data members. A function declaration must be prefixed by the keyword friend whereas the function definition must not. A function can be a friend to multiple classes.
Friend function -Special characteristics The scope of a friend function is not limited to the class in which it has been declared as a friend. A friend function cannot be called using the objects of that class; it is not in the scope of the class. It can be invoked like a normal function without the use of any object. It can be declared in the private part or the public part of the class without affecting its meaning.
e.g. //friend function # include <iostream.h> class sample { int a,b; public: void setdata() a=10,b=20; } friend float mean(sample S); }; float mean(sample S) return float(S.a+S.b)/2.0; int main () sample x; x.setdata(); cout<<“Mean value=“<<mean(x)<<“\n”; return 0;
e.g. Normal function accessing object’s private members #include<iostream.h> #include<conio.h> class AC; class AB { int x; public: void setvalue(int i) x=i; } friend void add(AB,AC); };
class AC { int a; public: void setvalue(int i) a=i; } friend void add(AB,AC); }; void add(AB m,Can) cout<<“The total value of distance is:”<<(m.x+n.a)<<“m”; int main() clrscr(); AB ab1; ab1.setvalue(10); AC ac1; ac1.setvalue(20); add(ab1,ac1); getch(); return(0);
Static data member A data member of a class can be qualified as static. Characteristics of Static data members: It is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one copy of that member is created for entire class and is shared by all the objects of that class. It is visible only within the class, but its lifetime is the entire program.
Static Member functions A static function can have access to only other static members declared in the same class. A Static member function can be called using the class name(instead of its objects)as follows: syntax: Class name ::function name
#include<iostream. h> #include<conio #include<iostream.h> #include<conio.h> class stat { int code; static int count; public: stat() code=++count; } void showcode() cout<<"\n\tObject number is :"<<code; static void showcount() cout<<"\n\tCount Objects :"<<count; };
int stat::count; void main() { clrscr(); stat obj1,obj2; stat::showcount(); obj1.showcode(); obj2.showcode(); getch(); } Output: Count Objects: 2 Object Number is: 1 Object Number is: 2
Difference b/w static and Non-static 1) A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
2) A static member function can be called, even when a class is not instantiated, A non-static member function can be called only after instantiating the class as an object.
3)A static member function cannot be declared virtual whereas a non-static member functions can be declared as virtual
4) A static member function cannot have access to the 'this' pointer of the class.
Function A function is a set of program statements that can be processed independently. A function can be invoked by a function call. The communication between a caller(calling function) and callee(called function) takes place through parameters.
Advantages of Function Reduction in the amount of work and development time. Program and function debugging is easier. Reduction in size of the program due to code reusability. Library of functions can be implemented by combining well designed, tested and proven functions.
Function components Every function has the following elements associated with it: Function declaration or prototype. Function parameters(formal parameters) Combination of function declaration and its definition. Function definition(function declarator and a function body) Return Statement. Function call
Syntax: Components of a function Void func(int a,int b); // Prototype Void func(int a,int b) // declarator (formal parameters) { //body } func(x,y); //call (actual parameter)
Eg. Maximum of two numbers #include<iostream.h> int max(int x,int y); // prototype void main() // function caller { int a,b,c; cout<<“Enter two integers:”; cin>>a>>b; c=max(a,b); cout<<“max(a,b)”<<c<<endl; Output: int max(int x,int y) // function definition Enter two integers:40,50 { max(a,b):50 if(x>y) return x; else return y; }
Library Function Library functions are shipped along with compliers. They are predefined and precompiled into library files and their prototypes can be found in the files with .h as their extension in the include directory The definitions are available in the form of objects codes in the files with .lib as their extension in the lib directory. In order to make use of a library function, include the corresponding header file. Once the header file is included, any function available in that library can be invoked.
Eg. Sqrt(),pow(),max() –declared and math.h //use of library function calls to round and truncate a result #include<iostream.h> #include<math.h> void main() { float n,n1,n2; cout<<“Enter any fractional number:”; cin>>n; n1=ceil(n); //rounds up n2=floor(n); //rounds down cout<<“ceil(“<<n<<“)=“<<n1<<endl; cout<<“floor(“<<n<<“)=“<<n2<<endl; } Output: Enter any fractional number:2.9 ceil(2.9)=3 floor(2.9)=2
Inline Function Inline functions are those whose function body is inserted in place of the function call statement during the compilation process. Within inline code, the program will not incur any context switching overhead. An inline function definition is similar to an ordinary function except that the keyword inline precedes the function definition. The significant feature of inline function is ,there is no explicit function call and body is substituted at the point of inline function call , therefore, runtime overhead for function linkage mechanism is reduced
//Square of number using inline function #include<iostream.h> inline intsquare(int num) // memory space optimized { return num*num; } void main(){ float n; cout<<“Enter a number:”; cin>>n; cout<<“Square is”<<Square(n)<<endl; Output: Enter a number:6 Square is:36
Function overloading Function overloading is a concept that allows multiple functions to share the same name with different arguments types. Assigning one or more function body to the same name is known as function overloading or function name overloading.
// Multiple swap function ,function overloading #include<iostream // Multiple swap function ,function overloading #include<iostream.h> void swap(char &x,char &y) { char t; t=x; x=y; y=t; } void swap(int &x,int &y) int t; void swap(float &x,float &y) float t;
void main() { char ch1,ch2; cout<<“Enter two charater<ch1,ch2>:”; cin>>ch1>>ch2; swap(ch1,ch2); // complier calls swap(char &a, char &b) cout<<“On swapping<ch1,ch2>:”<<ch1<<“ “<<ch2<<endl; int a,b; cout<<“Enter two intergers<a,b>:”; cin>>a>>b; swap(a,b);// complier calls swap(int &a, int &b) cout<<“On swapping<a,b>:”<<a<<“ “<<b<<endl; float c,d; cout<<“Enter two floats<c,d>:”; cin>>c>>d; swap(c,d);// complier calls swap(float &a, float &b) cout<<“On swapping<c,d>:”<<c<<“ “<<d<<endl; }
Output: Enter two character<ch1,ch2>:A B On Swapping<ch1,ch2>: B A Enter two integer<a,b>: 2 3 On Swapping<a,b>: 3 2 Enter two floats<c,d>:10.5 5.5 On Swapping<c,d>: 5.5 10.5