Unit 4 Data Member and Member Function
Unit 4 Data Member and Member Function
VIRTUAL FUNCTIONS
Polymorphism refers to the property by which objects belonging to different classes are able to respond to the
same message, but different forms. An essential requirement of polymorphism is therefore the ability to refer
to objects without any regard to their classes.
When we use the same function name in both the base and derived classes, the function in the base class is
declared as virtual using the keyword virtual preceding its normal declaration.
When a function is made virtual, C++ determines which function to use at runtime based on the type of object
pointed to by the base pointer, rather than the type of the pointer. Thus, by making the base pointer to point to
different objects, we can execute different versions of the virtual function.
#include<iostream.h>
class Base
{
public:
void display()
{
cout<<”Display Base”;
}
virtual void show()
{
cout<<”Show Base”;
}
};
class Derived : public Base
{
public:
void display()
{
cout<<”Display Derived”;
}
void show()
{
cout<<”show derived”;
}
};
void main()
{
Base b;
Derived d;
Base *ptr;
cout<<”ptr points to Base”;
ptr=&b;
ptr->display(); //calls Base
ptr->show(); //calls Base
cout<<”ptr points to derived”;
ptr=&d;
ptr->display(); //calls Base
ptr->show(); //class Derived
}
Output:
ptr points to Base
Display Base
Show Base
ptr points to Derived
Display Base
Show Derived
When ptr is made to point to the object d, the statement ptr->display(); calls only the function associated with
the Base i.e.. Base::display() where as the statement ptr->show(); calls the derived version of show(). This is
because the function display() has not been made virtual in the Base class.
OPERATOR OVERLOADING
C++ has the ability to provide the operators with as special meaning for a data type. The mechanism of giving
such special meanings to an operator is known as operator overloading. We can overload all the operators
except the following:
Class member access operator (“.” and “*”)
Scope resolution operator “::”
Size operator (sizeof)
Conditional operator
To define an additional task to an operator, specify what it means in relation to the class to which the operator
is applied. This is done with the help of a special function, called operator function.
The process of overloading involves following steps:
1. Create a class that defines the data type that is to be used in the overloading operation.
2. Declare the operator function operator op() in the public part of the class. It may be a member function or a
friend function.
3. Here op is the operator to be overloaded.
4. Define the operator function to implement the required operations.
complex complex::operator+(complex c)
{
complex t;
t.real=real+c.real;
t.img=img+c.img;
return t;
}
Example 3:-
Unary minus operator using a friend function
#include<iostream.h>
#include<iostream.h>
using namespace std;
class space
{
int x,y,z;
public:
void g etdata(int a,int b,int c);
void display();
friend void operator-(space &);
};
void space :: getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space :: display()
{
cout<<x<<" "<<y<<" "<<z<<endl;
}
void operator-(space &s)
{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
int main()
{
space S;
S.getdata(10,-20,30);
S.display();
-S;
cout<<"after negation\n";
S.display();
}
Output:
10 -20 30
after negation
-10 20-30
Example
complex operator+(complex s)
{
complex t;
t.real=real+s.real;
t.img=img+s.img;
return t;
}
The following program explains binary operator overloading:
#include < iostream.h >
#include < conio.h >
class sum
{
int a;
public:
sum()
{
a=0;
}
sum(int i)
{
a=i;
}
sum operator+(sum p1)
{
sum t;
t.a=a+p1.a;
return t;
}
void main ()
{
cout<<”Enter Two Numbers:”
int a,b;
cin>>a>>b;
sum x(a),y(b),z;
z.display();
z=x+y;
cout<<”after applying operator \n”;
z.display();
getch();
}
Output:
Enter two numbers 5 6
After applying operator
The sum of two numbers 11
Explanation: The class name is “sum”. We have created three objects two for to do the sum and the other for
returning the sum. ‟ +‟ is a binary operator operates on members of two objects and returns the result which is
member of a object. Here number of parameters is 1. The sum is displayed in display function.
Write a program to over load arithmetic operators on complex numbers using member function
#include<iostream.h>
class complex
{
float real,img;
public:
complex(){ }
complex(float x, float y)
{
real=x;
img=y;
}
complex operator+(complex c)
void display();
};
complex complex::operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
}
void complex::display()
{
int imag=img;
If(img<0)
{
imag=-imag;
cout<<real<<”-i”<<imag;
}
else
cout<<real<<”+i”<<img;
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
c3.display();
return 0;
}
Write a program to over load arithmetic operators on complex numbers using friend
Function.
#include<iostream.h>
class complex
{
float real,img;
public:
complex(){ }
complex(float x, float y)
{
real=x;
img=y;
}
friend complex operator+(complex);
void display();
};
complex operator+(complex c1, complex c2)
{
complex temp;
temp.real=c1.real+c2.real;
temp.img=c1.img+c2.img;
return temp;
}
void complex::display()
{
If(img<0)
{
img=-img;
cout<<real<<”-i”<<img;
}
else
cout<<real<<”+i”<<img;
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
c3.display();
return 0;
}