Unit - 6
Inheritance
Q1) Explain operator overloading?
A1)
Operator Function
- Operator is overloaded with the help of operator function.
- It defines the operations to be performed by overloaded operator.
- This operator function can be of two types
1.Member operator function
2.Non Member operator function
- Non member operator functions are usually friend functions of class. Operator functions are discussed in preceding section.
Overloading using Member Operator Function
Syntax of an operator function
- Member operator function is a member of the class.
- It is declared in a class as follows :
Return-type operator OP(argument list);
Where OP -> operator to be overloaded.
- Number of parameters in an argument list depends on the type of operator.
Type of Operator | No. Of Arguments |
Unary | 0 |
Binary | 1 |
- Thus according to above table, overloading unary operators using member functions takes no argument and binary operator takes one argument.
- It is defined as follows :
Return-type class-name :: operator OP(argument list)
{
Operations that overloaded operator performs
}
Q2) Write a program in C++ to overload unary minus(-).
A2)
#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” |
Q3) Write a C++ program to overload binary +, - .
A3)
#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 ;
}
Q4) Write C++ program to demonstrate use of binary (“+”) operator overloading to add two complex numbers using member function.
Q5)
#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 ;
}
Q6) Write a program to overload unary –, ++,--(prefix versions) using friend function.
A5)
#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 -- |
Q7) Write a C++ program to subtract 2 complex numbers using concept of overloading using friends function.
A6)
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;
Q8) Write a Program to overload new and delete operator.
A7)
#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
Q9) Explain the rules for operator overloading?
A8)
Operators which cannot be overloaded
All C++ operators can be overloaded except following
- Class member access operator(.*)
- Conditional operator(?:)
- Scope resolution operator(::)
- Size of operator(sizeof)
Operators which cannot be overloaded by using Friend Function
Following operators cannot be overloaded using friends
- () Function call operator
- = Assignment operator
- [] subscripting operator
- -> class member access operator
Rules for operator overloading
- Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
- We cannot create new operators for overloading. Only built in operators can be overloaded.
- We cannot redefine meaning of in built operators i.e we cannot change the meaning +, - etc.
- Precedence and associativity of operators cannot be changed.
- Overloaded operators cannot have default arguments.
- Operators overloaded using member functions takes no argument for unary and one argument for binary operator.
- Operators overloaded using friend functions takes one argument for unary and two arguments for binary operator.
Q10) Explain function overloading?
A9)
- Function overloading is one of the way to achieve compile time polymorphism in c++.
- Overloaded functions have same function name and different argument list.
F1→ void accept(int x);
F2→ void accept(float y);
F3→ void accept(int x, int y);
Here accept function is overloaded.
- F1 and F2 are overloaded as they have same function name but different argument list.
- Though argument list consists of single argument it is of different data type.
- F1 and F3 are overloaded as they have same function name and number of arguments are different.
- F2 and F3 are overloaded as they have same function name and number of arguments are different.
- Argument list may have different number of arguments OR different data type of arguments OR both.
- Return type of functions may be same or different.
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.
- Overloaded functions perform tasks of similar nature.
- As all overloaded functions have same name compiler determines the function to be called on the basis of argument list.
- Compiler searches for the exact match of function prototype and then executes corresponding function.
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); |
Q11) Write a program in C++ for bubble sort using function template.
A10)
#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
Q12) Explain class template ?
A11)
- Class template defines a generic class that specifies all the functions used by the class. However, the type of data on which function works will be specified as a parameter, when objects of that class are created.
- Thus they are also called as parameterized classes.
- Assume we have created a stack class that stores integer values. Now if we want to store float values in stack, we have to recreate stack class to store float values.
- This can be avoided by creating template class. Its general form is as follows :
Syntax
Template<class T>
Class class_name
{
-------------
Class members
-------------
};
Q13) Explain single and multiple inheritance?
A12)
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 }; |
Q14) Explain virtual base class?
A13)
- When two or more classes are derived from a common base class, we can prevent multiple copies of the base class being present in a derived class by declaring the base class as virtual. Such a base class is known as virtual base class.
- When base class is made virtual, only one copy of base class data members is inherited in derived class.
- So the structure of classes will be as follows :
Class Employee
{
};
Class Project: public virtual Employee
{
};
Class Salary: virtual public Employee
{
};
Class Employee_Info: public Project, public Salary
{
};
Q15) Explain pointer and inheritance?
A14)
- Pointer is used to access the private member variables stored in memory locations. Pointer is public memory variable. Private and Public member variables are stored in memory. Pointer holds memory location of variable.
- Is used declare the pointer with an identifier.
Example
Int *numPoint;
Std::cout << "The numPoint points to the memory address " << numPoint << ".\n";
- Working of inheritance is same as pointer. The base class and derived class member variables are stored in memory locations.
- Inheritance, in which from existing class new class is generated with properties of existing class.
- Inheritance provides code reusability.
- Public, Private and protected Access specifier can be used.
- Syntax
Class childClass : accessSpecifier ParentClass
Example
Using namespace std;
Class Parent
{
Public:
Int id_pr
};