0% found this document useful (0 votes)
18 views

Unit 3

unit

Uploaded by

adhibillowria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit 3

unit

Uploaded by

adhibillowria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Unit-III Inheritance and Operator overloading

3.1 Inheritance
One of the most important concepts in object-oriented programming is
that of inheritance. Inheritance allows us to define a class in terms of
another class, which makes it easier to create and maintain an
application. This also provides an opportunity to reuse the code
functionality and fast implementation time.
When creating a class, instead of writing completely new data members
and member functions, the programmer can designate that the new class
should inherit the members of an existing class. This existing class is
called the base class, and the new class is referred to as the derived
class.
The idea of inheritance implements the is a relationship. For example,
mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well
and so on.
Base and Derived Classes
A class can be derived from more than one classes, which means it can
inherit data and functions from multiple base classes. To define a
derived class, we use a class derivation list to specify the base class(es).
A class derivation list names one or more base classes and has the form
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and
base-class is the name of a previously defined class. If the access-
specifier is not used, then it is private by default.
Consider a base class Shape and its derived class Rectangle as follows
#include<iostream>
usingnamespace std;
// Base class
classShape
{
public:
voidsetWidth(int w){
width = w;
}
voidsetHeight(int h){
height = h;
}
protected:
int width;
int height;
};
classRectangle:publicShape // Derived class
{
public:
intgetArea(){
return(width * height);
}
};
int main(void)
{
RectangleRect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout<<"Total area: "<<Rect.getArea()<<endl;
return0;
}
When the above code is compiled and executed, it produces the
following result –
Total area: 35
Access Control and Inheritance
A derived class can access all the non-private members of its base class.
Thus base-class members that should not be accessible to the member
functions of derived classes should be declared private in the base class.
We can summarize the different access types according to - who can
access them in the following way −
Access public protected Private
Same class yes yes Yes
Derived classes yes yes No
Outside classes yes no No
A derived class inherits all base class methods with the following
exceptions −
 Constructors, destructors and copy constructors of the base class.
 Overloaded operators of the base class.
 The friend functions of the base class.
3.2 Type of Inheritance
When deriving a class from a base class, the base class may be inherited
through public, protected or private inheritance. The type of
inheritance is specified by the access-specifier as explained above.
We hardly use protected or private inheritance, but public inheritance
is commonly used. While using different type of inheritance, following
rules are applied −
 Public Inheritance − When deriving a class from a public base class,
public members of the base class become public members of the
derived class and protected members of the base class become
protected members of the derived class. A base class's private members
are never accessible directly from a derived class, but can be accessed
through calls to the public and protected members of the base class.
 Protected Inheritance − When deriving from a protected base class,
public and protected members of the base class become protected
members of the derived class.
 Private Inheritance − When deriving from a private base class,
public and protected members of the base class become private
members of the derived class.
C++ supports five types of inheritance:
 Single inheritance
 Multiple inheritance
 Hierarchical inheritance
 Multilevel inheritance
 Hybrid inheritance
3.2.1 Single Inheritance
Single inheritance is defined as the inheritance in which a derived class
is inherited from the only one base class.

Where 'A' is the base class, and 'B' is the derived class.
Single Level Inheritance Example: Inheriting Fields
When one class inherits another class, it is known as single level
inheritance. Let's see the example of single level inheritance which
inherits the fields only.
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Output :
Salary: 60000
Bonus: 5000
In the above example, Employee is the base class and Programmer is the
derived class.
Single Level Inheritance Example: Inheriting Methods
Let's see another example of inheritance in C++ which inherits methods
only.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
Output:
Eating...
Barking...
Example
#include<iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};
class B : private A
{
public:
void display()
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();
return 0;
}
Ouput:
Multiplication of a and b is : 20
3.2.1.1 To make a Private Member Inheritable
The private member is not inheritable. If we modify the visibility mode
by making it public, but this takes away the advantage of data hiding.
C++ introduces a third visibility modifier, i.e., protected. The member
which is declared as protected will be accessible to all the member
functions within the class as well as the class immediately derived from
it.
Visibility modes can be classified into three categories:

 Public: When the member is declared as public, it is accessible to all


the functions of the program.
 Private: When the member is declared as private, it is accessible
within the class only.
 Protected: When the member is declared as protected, it is accessible
within its own class as well as the class immediately derived from it.
Visibility of Inherited Members
Base class visibility Derived class visibility

Public Private Protected

Private Not Inherited Not Inherited Not Inherited


Protected Protected Private Protected
Public Public Private Protected
3.2.2 Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another
derived class.
Multi Level Inheritance Example
When one class inherits another class which is further inherited by
another class, it is known as multi level inheritance in C++. Inheritance
is transitive so the last derived class acquires all the members of all its
base classes.
Let's see the example of multi level inheritance in C++.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Output:
Eating...
Barking...
Weeping...
3.2.3 Multiple inheritance is the process of deriving a new class that
inherits the attributes from two or more classes.

Syntax of the Derived class:


class D : visibility B-1, visibility B-2, ?
{
// Body of the class;
}
Example
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}
Output:
The value of ais : 10
The value of b is : 20
Addition ofa and b is : 30
In the above example, class 'C' inherits two base classes 'A' and 'B' in a
public mode.
3.2.3.1 Ambiguity Resolution in Inheritance
Ambiguity can be occurred in using the multiple inheritance when a
function with the same name occurs in more than one base class.
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
std::cout << "Class A" << std::endl;
}
};
class B
{
public:
void display()
{
std::cout << "Class B" << std::endl;
}
};
class C : public A, public B
{
void view()
{
display();
}
};
int main()
{
C c;
c.display();
return 0;
}

Output:
error: reference to 'display' is ambiguous
display();
The above issue can be resolved by using the class resolution operator
with the function. In the above example, the derived class code can be
rewritten as:
class C : public A, public B
{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.
}
};
3.2.4 Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of
inheritance.

Example:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};
class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
class D : public B, public C
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Output:
Enter the value of 'a' :
10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000
3.2.5 Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than
one class from a base class.

Syntax of Hierarchical inheritance:


class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
Example:
#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::end
l;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <<m<< std::endl;
std::cout << "Enter the base and height of the triangle: " << std::endl
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}
Output:
Enter the length and breadth of a rectangle:
23
20
Area of the rectangle is : 460
Enter the base and height of the triangle:
2
5
Area of the triangle is : 5
3.3Operators Overloading
Operator overloading is a compile-time polymorphism in which the
operator is overloaded to provide the special meaning to the user-defined
data type. Operator overloading is used to overload or redefines most of
the operators available in C++. It is used to perform the operation on the
user-defined data type. For example, C++ provides the ability to add the
variables of the user-defined data type that is applied to the built-in data
types.
The advantage of Operators overloading is to perform different
operations on the same operand.
Operator that cannot be overloaded are as follows:
o Scope operator (::)
o Sizeof
o member selector(.)
o member pointer selector(*)
o ternary operator(?:)
Syntax
return_type class_name : : operator op(argument_list)
{
// body of the function.
}
Where the return type is the type of value returned by the function.
class_name is the name of the class.
operator op is an operator function where op is the operator being
overloaded, and the operator is the keyword.
Example
In this example, void operator ++ () operator function is defined (inside
Test class).
// program to overload the unary operator ++.
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
Output:
The Count is: 10
Example of overloading the binary operators.
// program to overload the binary operators.
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(){}
A(int i)
{
x=i;
}
void operator+(A);
void display();
};
void A :: operator+(A a)
{
int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;
}
int main()
{
A a1(5);
A a2(4);
a1+a2;
return 0;
}
Output:
The result of the addition of two objects is : 9
3.3.1 Rules for Operator Overloading
 Existing operators can only be overloaded, but the new operators
cannot be overloaded.
 The overloaded operator contains atleast one operand of the user-
defined data type.
 We cannot use friend function to overload certain operators.
However, the member function can be used to overload those operators.
 When unary operators are overloaded through a member function
take no explicit arguments, but, if they are overloaded by a friend
function, takes one argument.
 When binary operators are overloaded through a member function
takes one explicit argument, and if they are overloaded through a friend
function takes two explicit arguments.
3.4 Overloading of Unary and Binary operators
An operator is a symbol that tells the compiler to perform specific task.
Every operator has their own functionality to work with built-in data
types. Class is user-defined data type and compiler doesn't understand,
how to use operators with user-defined data types. To use operators with
user-defined data types, they need to be overload according to a
programmer's requirement.
Operator overloading is a way of providing new implementation of
existing operators to work with user-defined data types.
An operator can be overloaded by defining a function to it. The function
for operator is declared by using the operator keyword followed by the
operator.
There are two types of operator overloading in C++
 Binary Operator Overloading
 Unary Operator Overloading
3.4.1 Overloading Binary Operator
Binary operator is an operator that takes two operand(variable). Binary
operator overloading is similar to unary operator overloading except that
a binary operator overloading requires an additional parameter.
Binary Operators
 Arithmetic operators (+, -, *, /, %)
 Arithmetic assignment operators (+=, -=, *=, /=, %=)
 Relational operators (>, <, >=, <=, !=, ==)
Example of Binary Operator Overloading
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int L,B;
public:
Rectangle() //Default Constructor
{
L = 0;
B = 0;
}
Rectangle(int x,int y) //Parameterize Constructor
{
L = x;
B = y;
}
Rectangle operator+(Rectangle Rec) //Binaryoperator overloading
func.
{
Rectangle R;
R.L = L + Rec.L;
R.B = B + Rec.B;
return R;
}
voidDisplay()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}
};
void main()
{
Rectangle R1(2,5),R2(3,4),R3;
//Creating Objects
cout<<"\n\tRectangle1 : ";
R1.Display();
cout<<"\n\n\tRectangle2 : ";
R2.Display();
R3 =R1 + R2; Statement 1
cout<<"\n\n\tRectangle3 : ";
R3.Display();
}
Output :
Rectangle 1 :
L:2
B:5
Rectangle2 :
L:3
B:4
Rectangle3 :
L:5
B:9
In statement 1, Left object R1 will invoke operator+() function and right
object R2 is passing as argument.
Another way of calling binary operator overloading function is to call
like a normal member function as follows,
R3 = R1.operator+ ( R2 );
3.4.2 Overloading Unary Operator
Unary operator is an operator that takes single operand(variable). Both
increment(++) and decrement(--) operators are unary operators.
Example of Unary Operator Overloading
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int L,B;
public:
Rectangle() //Default Constructor
{
L = 0;
B = 0;
}
void operator++() Unaryoperator overloadingfunc.
{
L+=2;
B+=2;
}
void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}
};
void main()
{
Rectangle R; //Creating Object
cout<<"\n\tLength Breadth before increment";
R.Display();
R++;
cout<<"\n\n\tLengthBreadth after increment";
R.Display();
}
Output :
Length Breadth after increment
L:0
B:0
Length Breadth after increment
L:2
B:2

You might also like