MODULE 4
Overloading, Templates and Inheritance
Operator Function
1. Member operator function
2. Non Member operator function
Overloading using Member Operator Function
Syntax of an operator function
return-type operator OP(argument list);
Where OP -> operator to be overloaded.
Type of Operator | No. of Arguments |
Unary | 0 |
Binary | 1 |
return-type class-name :: operator OP(argument list)
{
Operations that overloaded operator performs
}
2. Write a program in C++ to overload unary minus(-).
Solution :
#include<iostream> using namespace std ; class Example { int x,y,z; public:
Example(int p,int q,int r) { x=p; y=q; z=r; } void display() { cout<<x<<” ”<<y<<” ”<<z; } void operator-() { x=-x; y=-y; z=-z; } }; int main() { Example obj (10,-20,30); cout<<”Original numbers are” obj.display(); - obj; obj.display(); return 0; } |
//Defines class Example
//Parameterized constructor
// Step 1 Operator function to overload minus
// Step 2 Invokes operator function
cout<<”Numbers after applying unary minus” |
3. Write a C++ program to overload binary +, - .
#include<iostream>
using namespace std ;
class Example
{
int a,b;
public:
Example(int p, int q)
{
a=p;
b=q;
}
void display()
{
cout<<a<<” ”<<b<<endl;
}
Example operator+(Example X);
Example operator-(Example X);
};
Example Example::operator+(Example X)
{
Example E;
E.a= a+X.a;
E.b= b+X.b;
return E
}
Example Example:: operator-(Example X)
{
Example F;
F.a= a-X.a;
F.b= b-X.b;
return F
}
int main()
{
Example obj1(10,20);
Example obj2(5,5);
Example obj3(0,0);
cout<<”Addition is”
obj3= obj1+ obj2;
obj3.display();
cout<<”Subtraction is”
obj3= obj1- obj2;
obj3.display();
return 0 ;
}
4. Write C++ program to demonstrate use of binary (“+”) operator overloading to add two complex numbers using member function.
#include<iostream>
using namespace std ;
class Demo
{
int real,imag;
public:
Demo(int p, int q)
{
real=p;
imag=q;
}
void display()
{
cout<<real<<” +j”<<imag<<endl;
}
Demo operator+(Demo X);
};
Demo Demo::operator+(Demo X)
{
Demo D;
D.real=real+X.real;
D.imag=imag+D.imag;
return D
}
int main()
{
Demo obj1(10,20);
Demo obj2(5,5);
Demo obj3(0,0);
cout<<”Addition is”
obj3= obj1+ obj2;
obj3.display();
return 0 ;
}
5. Write a program to overload unary –, ++,--(prefix versions) using friend function.
#include<iostream> using namespace std ; class Example { int x,y,z; public: Example(int p,int q,int r) { x=p; y=q; z=r; } void display() { cout<<x<<” ”<<y<<” ”<<z; } friend void operator-(Example &O); friend void operator++(Example &O); friend void operator--(Example &O); }; void operator-(Example & O) { O.x=-O.x; O.y=-O.y; O.z=-O.z;
} void operator++(Example & O) { O.x=O.x++; O.y=O.y++; O.z=O.z++; }
void operator--(Example & O) { O.x=O.x--; O.y=O.y--; O.z=O.z--; } }; int main() { Example O(10,-20,30); cout<<”Original numbers are”; O.display(); - O; cout<<”Numbers after applying unary minus”; O.display(); ++ O; cout<<”Numbers after applying ++”; O.display(); -- O; cout<<”Numbers after applying --”; O.display(); return 0 ; } |
//Defines class Example
//Parameterized constructor
//Operator functions to overload
//Invokes operator function for –
//Invokes operator function for ++
//Invokes operator function for -- |
6. Write a C++ program to subtract 2 complex numbers using concept of overloading using friends function.
include<iostream>
using namespace std ;
class Example
{
int real, imag;
public:
Example(int p, int q)
{
real=p;
imag=q;
}
void display()
{
cout<<real<<”+j”<<imag<<endl;
}
friend Example operator-(Example X1,Example X2);
};
Example operator-(Example X1, Example X2)
{
Example E;
E.real= X1.real-X2.real;
E.imag= X1.imag-X2.imag;
return E;
}
int main()
{
Example obj1(10,20);
Example obj2(5,5);
Example obj3(0,0);
cout<<“Subtraction is”
obj3= obj1- obj2;
obj3.display();
return 0;
7. Write a Program to overload new and delete operator.
#include<iostream>
using namespace std;
class Employee
{
int id;
int salary;
public:
Employee(int x, int y)
{
id=x;
salary=y;
}
void display()
{
cout<<”Employee id:”<<id<<endl;
cout<<”Employee salary:”<<salary<<endl;
}
void *operator new(size_t size );
void operator delete(void *p);
};
void *Employee::operator new(size_t size )
{
void *p;
cout<<”Overloading new”<<endl;
p=malloc(size);
if(!p)
{
bad_alloc b;
throw b;
}
return p;
}
void Employee:: operator delete(void *p)
{
cout<<”Overloading delete”<<endl;
free(p);
}
int main()
{
Employee *E1,*E2;
try
{
E1= new Employee(10,50000);
}catch(bad_alloc ba)
{
cout<<”Allocation error for E1”;
return 1;
}
try
{
E2= new Employee(11,60000);
}catch(bad_alloc ba)
{
cout<<”Allocation error for E2”;
return 1;
}
E1->display();
E2->display();
delete E1;
delete E2;
return 0;
}
Output
Overloading new
Overloading new
10 50000
11 60000
Overloading delete
Overloading delete
8. Explain the rules for operator overloading?
Operators which cannot be overloaded
All C++ operators can be overloaded except following
Operators which cannot be overloaded by using Friend Function
Following operators cannot be overloaded using friends
Rules for operator overloading
9. Explain function overloading?
F1→ void accept(int x);
F2→ void accept(float y);
F3→ void accept(int x, int y);
Here accept function is overloaded.
int add(int x, int y);
float add(int x, int y);
Here add functions have different return types but they have same argument list. So they are not overloaded functions.
Thus return type does not play any role in function overloading.
Table : Function prototypes and required function calls
Sr. No. | Function Protoypes | Function calls |
1. | void accept(int x); | accept(10); |
2. | void accept(float y); | accept(1.5); |
3. | void accept(int x, int y); | accept(10,20); |
10. Write a program in C++ for bubble sort using function template.
Solution :
#include<iostream> using namespace std; template <class T> void bubblesort(T A[], int n) { for(int i=0;i<=n-1;i++) for(int j=n-1;i<j;j--) if(A[j]<A[j-1]) { swap (A[j], A[j-1]) } template <class X> void swap(X &p, X &q) { X temp=p; p=q; q=temp; }
int main() { int array[5]; cout<<“Enter elements of array”; for(int i=0;i<5;i++) { cin>>a[i]; } bubblesort(array,5); cout<<“Sorted array”; for(int i=0;i<5;i++) { cout<<array[i]<<“”; return 0; } |
//template function for bubble sort
// calls template function for swap
// template function for swap
//accept array elements for sorting
//calls template function for sorting |
Output
Enter elements of array
25 15 30 10 50
Sorted array 10 15 25 30 50
11. Explain class template ?
Syntax
template<class T>
class class_name
{
-------------
class members
-------------
};
12. Explain single and multiple inheritance?
Single Inheritance
● It consists of single base and single derived class.
● Derived class inherits all the protected and public members from single base class.
● Private data members of base class will not be inherited.
☞ General form
class derived-class : access specifier base-class
{
//Body of derived class
};
Fig: Single Inheritance
| class A { //Body of class A }; class B : public A { //Body of class B }; |
Multiple Inheritance
☞ Multiple inheritance with syntax
● It consists of multiple base classes and single derived class.
● Derived class inherits protected and public members from multiple base classes.
● If there are two base classes, then derived class inherits features from both the classes.
☞ General form
class derived-class : access specifier base-class1, access specifier base-class2
{
//Body of derived class
};
Fig: Multiple Inheritance
| class A { //Body of class A }; class B { //Body of class B }; class C : public A, public B { //Body of class C }; |
13. Explain virtual base class?
class Employee
{
};
class Project: public virtual Employee
{
};
class Salary: virtual public Employee
{
};
class Employee_Info: public Project, public Salary
{
};
14. Explain pointer and inheritance?
Example
Example