unit2 part4
unit2 part4
Like any other data type, an object may be used as a function argument.
The first method is called pass-by-value. Since a copy of the object is passed to the function,
any change made to the object inside the function do not effect the object used to call the
function. The second method is called pass-by-reference . When an address of the object is
passed, the called function works directly on the actual object used in the call. This means
that any changes made to the object inside the functions will reflect in the actual object .The
pass by reference method is more efficient since it requires to pass only the address of the
object and not the entire object.
#include <iostream>
class Demo {
private:
int a;
public:
void set(int x) { a = x; }
{ a = ob1.a + ob2.a; }
};
int main() {
// object declarations
Demo d1;
Demo d2;
Demo d3;
d1.set(10);
d2.set(20);
d3.sum(d1, d2);
d1.print();
d2.print();
d3.print();
return 0;
Output
Value of A : 10
Value of A : 20
Value of A : 30
Explanation
Above example demonstrate the use of object as a parameter. We
are passing d1 and d2 objects as arguments to the sum member
function and adding the value of data members a of both objects
and assigning to the current object’s (that will call the function,
which is d3) data member a.
#include <iostream>
class time
int hours;
int minutes;
public:
hours = h;
minutes = m;
void puttime(void)
};
int main()
t1.gettime(2, 45);
t2.gettime(3, 30);
t3.sum(t1, t2);
cout << "T1= "; t1.puttime();
return 0;
o/p
Since the member function sum() is invoked by the objects T3, with the objects T1 and T2 as
arguments.
Friend Functions
The friend function in C++ is defined outside the scope of the class. It has the authority to
access all protected and private class members. Friends are not member functions, but the
prototypes for friend functions appear in the class function only.
a. It is not in the scope of the class to which it has been declared as friend.
b. Since it is not in the scope of the class, it cannot be called using the object of that class.
c. It can be invoked like a member function without the help of any object.
class sample
{ int a; int b;
public:
void setvalue()
a=25;
b=40;
};
float mean(sample s)
return (float(s.a+s.b)/2.0);
int main()
sample x;
x . setvalue();
cout<<"mean value="<<mean(x)<<endl;
return(0);
o/p
class XYZ
int x;
public:
void setvalue(int i)
x = i;
};
class ABC
int a;
public:
a=i;
}
friend void max(XYZ,ABC);
};
cout<<m.x;
int main()
ABC j;
j.setvalue( 10);
XYZ s;
s.setvalue(20);
max(s,j);
return(0);
O/P
20
class class_1
int value1;
public:
void indata(int a)
value1=a;
void display(void)
cout<<value1<<endl;
};
class class_2
int value2;
public:
void indata(int a)
value2=a;
void display(void)
cout<<value2<<endl;
}
friend void exchange(class_1 &, class_2 &);
};
int temp=x.value1;
x.value1=y.value2;
y.value2=temp;
int main()
class_1 c1;
class_2 c2;
c1.indata(100);
c2.indata(200);
c1.display();
c2.display();
exchange(c1,c2);
c1.display();
c2.display();
return(0);
o/p
values before exchange
100
200
Values after exchange
200
100
In the above program the objects x and y are aliases of c1 and c2 respectively.
Constructors
A constructor is a special member function whose task is to initialize the objects of its
class . It is special because its name is the same as the class name and can be declared in the
public section . The constructor is invoked whenever an object of its associated class is
created. They do not have a return type, not even void. It is called constructor because it
construct the values of data members of the class.
class integer
int m,n;
------------
------------
};
integer :: integer(void)
m=0; n=0;
}
When a class contains a constructor like the one defined above it is guaranteed that an object
created by the class will be initialized automatically.
For example:-
This declaration not only creates the object int1 of type integer but also initializes its data
members m and n to zero.
The default constructor for class A is A :: A( ). If no such constructor is defined, then the
compiler supplies a default constructor .
A a ;//invokes the default constructor of the compiler of the compiler to create the object
"a" ;
#include <iostream>
class Employee
public:
Employee()
};
int main(void)
Employee e2;
return 0;
o/p
Example2
#include <iostream>
using namespace std;
class abc
{ private:
char nm[];
public: abc ()
{
cout<<"enter your name:";
cin>>nm;
}
void display( )
{
cout<<nm;
}
};
int main()
{
abc d;
d.display();
getch();
return(0);
}
PARAMETERIZED CONSTRUCTOR:-
the constructors that can take arguments are called parameterized constructors.
Using parameterized constructor we can initialize the various data elements of
different objects with different values when they are created.
Example:-
class integer
{
int m,n;
public: integer( int x, int y);
--------
---------
};
implicitly or explicitly
#include <iostream>
class integer
int m,n;
public: integer(int,int);
void display(void)
cout<<"m=:"<<m ;
cout<<"n="<<n;
};
m=x;
n=y;
int main()
int1.display();
int2.display();
output:
object 1
m=0
n=100
object2
m=25
n=25
Constructor Overloading
#include <iostream>
class Demo {
private:
int X;
int Y;
public:
Demo();
void Display();
}; //End of class
Demo::Demo()
X = 10;
Y = 20;
Demo::Demo(int a, int b)
X = a;
Y = b;
void Demo::Display()
int main()
{
Demo d1;
d1.Display();
d2.Display();
return 0;
Output
Default Constructor:
Value after initialization:
X: 10
Y: 20
Parameterized Constructor:
Value after initialization:
X: 30
Y: 40
integer I2(I1);
Would define the object I2 and at the same time initialize it to the values of I1.
Another form of this statement is
integer I2 =I1;
#include <iostream>
class A
public:
int x;
x=a;
x = i.x;
};
int main()
{
// A a2 = a1;
cout<<a2.x;
return 0;
o/p
20
In the above case, copy constructor can be called in the following
ways:
#include <iostream>
class Time
{
public :
int Hours;
int Minutes;
int Seconds;
void ShowTime()
cout << "Time is "<< Hours << " Hours : "<< Minutes << " Minutes : and
" << Seconds << " Seconds \n";
/* see the use of default arguments in both arguments towards the end*/
Hours = TempHours;
Minutes = TempMinutes;
Seconds = TempSeconds;
};
int main()
Time Time1(12,15,15);
Time1.ShowTime();
Time Time2(10,30);
return 0;
o/p
Note: the actual parameter when specified overridesthe default value. The
missing arguments must be the trailing ones
DESTRUCTOR:-
A destructor, us the name implies is used to destroy the objects that have been
created by a constructor. Like a constructor, the destructor is a member function
whose name is the same as the class name but is preceded by a tilde.
For Example :-
~ integer( ) { }
A destructor never takes any argument nor does it return any value. It will be
invoked implicitly by the compiler upon exit from the program to clean up
storage that is no longer accessible. It is a good practice to declare destructor in
a program since it releases memory space for future use.
#include <iostream>
int count=0;
class alpha
{
public:
alpha()
count ++;
~alpha()
count--;
};
int main()
alpha A1,A2,A3,A4;
alpha A5;
{
cout<<"\n \n enter block2 \n";
alpha A6;
return(0);
output:-
enter main
no of object created 1
no of object created 2
no of object created 3
no of object created 4
enter block 1
no of object created 5
no of object destroyed 5
enter block 2
no of object created 5
no of object destroyed 5
re-enter main
no of object destroyed 4
no of object created 3
no of object created 2
no of object created 1
NOTE: Constructors are called in order of object declaration, and
the destructors are called in the reverse order of its declaration.
Example2 Program on Destructor
#include <iostream>
class Demo {
private:
int X;
public:
// constructor
Demo(int x)
X = x;
Print();
// destructor
~Demo()
Print();
}
void Print()
};
int main()
Demo OB1(10);
Demo OB2(20);
Demo OB3(30);
return 0;
Output
Value of X: 10
Value of X: 20
Value of X: 30
Destructor Called
Value of X: 30
Destructor Called
Value of X: 20
Destructor Called
Value of X: 10