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

Unit 4 Lec Notes

Operator overloading allows operators to be redefined for user-defined types. It allows operators to work on user-defined types like built-in types. Common operators that can be overloaded include unary operators like ++ and --, binary operators like + and -, and assignment operators. Operator overloading is implemented through member functions or friend functions.

Uploaded by

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

Unit 4 Lec Notes

Operator overloading allows operators to be redefined for user-defined types. It allows operators to work on user-defined types like built-in types. Common operators that can be overloaded include unary operators like ++ and --, binary operators like + and -, and assignment operators. Operator overloading is implemented through member functions or friend functions.

Uploaded by

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

UNIT-4

Operators 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 of Operator Overloading

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.

Rules for Operator Overloading

o Existing operators can only be overloaded, but the new operators cannot be overloaded.
o The overloaded operator contains at least one operand of the user-defined data type.
o We cannot use friend function to overload certain operators. However, the member
function can be used to overload those operators. like = () [] ->.
o 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.
o 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.

Types of Operator Overloading

Operator Overloading can be done by using three approaches, they are

1. Overloading unary operator.


2. Overloading binary operator.
3. Overloading binary operator using a friend function

1. Overloading Unary Operator: Let us consider to overload ( ++) unary operator. In


unary operator function, no arguments should be passed. It works only with one class
objects. It is a overloading of an operator operating on a single operand.

// program to overload the unary operator ++.

#include <iostream.h>
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

Example2. C++ program to show unary operator overloading

#include <iostream.h>
class Distance {
public:
// Member Object
int feet, inch;
// Constructor to initialize the object's value
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Overloading(-) operator to perform decrement
// operation of Distance object
void operator-()
{
feet--;
inch--;
cout << "\nFeet & Inches(Decrement): " << feet << "'" << inch;
}
};
// Driver Code
int main()
{
// Declare and Initialize the constructor
Distance d1(8, 9);
// Use (-) unary operator by single operand
-d1;
return 0;
}
2. Overloading Binary Operator: In binary operator overloading function, there
should be one argument to be passed. It is overloading of an operator operating on
two operands.

Example1. program to overload the binary operators.

#include <iostream.h>
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


Example2. C++ program to show binary operator overloading

#include <iostream.h>
class Distance {
public:
// Member Object
int feet, inch;
// No Parameter Constructor
Distance()
{
this->feet = 0;
this->inch = 0;
}
// Constructor to initialize the object's value
// Parameterized Constructor
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Overloading (+) operator to perform addition of
// two distance object
Distance operator+(Distance& d2) // Call by reference
{
// Create an object to return
Distance d3;

// Perform addition of feet and inches


d3.feet = this->feet + d2.feet;
d3.inch = this->inch + d2.inch;
// Return the resulting object
return d3;
}
};
int main()
{
// Declaring and Initializing first object
Distance d1(8, 9);
// Declaring and Initializing second object
Distance d2(10, 2);
// Declaring third object
Distance d3;
// Use overloaded operator
d3 = d1 + d2;
// Display the result
cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
}

3. Overloading Binary Operator using a Friend function: In this approach, the


operator overloading function must precede with friend keyword, and declare a
function class scope. Keeping in mind, friend operator function takes two
parameters in a binary operator, varies one parameter in a unary operator. All the
working and implementation would same as binary operator function except this
function will be implemented outside of the class scope Let’s take the same
example using the friend function.

Syntax:

friend return-type operator operator-symbol (Variable 1, Varibale2)


{
//Statements;
}

Example1 : Program demonstrating Unary operator overloading using Friend function

#include<iostream.h>
class UnaryFriend
{
int a=10;
int b=20;
int c=30;
public:
void getvalues()
{
cout<<"Values of A, B & C\n";
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void show()
{
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void friend operator-(UnaryFriend &x); //Pass by reference
};
void operator-(UnaryFriend &x)
{
x.a = -x.a; //Object name must be used as it is a friend function
x.b = -x.b;
x.c = -x.c;
}
int main()
{
UnaryFriend x1;
x1.getvalues();
cout<<"Before Overloading\n";
x1.show();
cout<<"After Overloading \n";
-x1;
x1.show();
return 0;
}

Example2. // C++ program to show binary operator overloading using friend


function.
#include <iostream.h>
class Distance {
public:
int feet, inch;
// No Parameter Constructor
Distance()
{
this->feet = 0;
this->inch = 0;
}
// Constructor to initialize the object's value
// Parameterized Constructor
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Declaring friend function using friend keyword
friend Distance operator+(Distance&, Distance&);
};
// Implementing friend function with two parameters
Distance operator+(Distance& d1, Distance& d2) // Call by reference
{ // Create an object to return
Distance d3;
// Perform addition of feet and inches
d3.feet = d1.feet + d2.feet;
d3.inch = d1.inch + d2.inch;
// Return the resulting object
return d3;}
// Driver Code
int main()
{
// Declaring and Initializing first object
Distance d1(8, 9);
// Declaring and Initializing second object
Distance d2(10, 2);
// Declaring third object
Distance d3;
// Use overloaded operator
d3 = d1 + d2;
// Display the result
cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
}
Example : Write a C++ program to overload '+' operator to concatenate two
strings.
#include<iostream.h>
#include<string.h>
class String
{
public:
char str[20];
public:
void accept_string()
{
cout<<"\n Enter String : ";
cin>>str;
}
void display_string()
{
cout<<str;
}
String operator+(String x) //Concatenating String
{
String s;
strcat(str,x.str);
strcpy(s.str,str);
return s;
}
};
int main()
{
String str1, str2, str3;
str1.accept_string();
str2.accept_string();
cout<<"\n ----------------------------------------------";
cout<<"\n\n First String is : ";
str1.display_string(); //Displaying First String
cout<<"\n\n Second String is : ";
str2.display_string(); //Displaying Second String
cout<<"\n ----------------------------------------------";
str3=str1+str2; //String is concatenated. Overloaded '+' operator
cout<<"\n\n Concatenated String is : ";
str3.display_string();
return 0;
}
Example : Write a C++ program to overload subscript [] operator .

#include <iostream.h>
const int SIZE = 10;

class safearay {
private:
int arr[SIZE];

public:
safearay() {
int i;
for(i = 0; i < SIZE; i++) {
arr[i] = i;
}
}

int operator[](int i) {
if( i > SIZE ) {
cout << "Index out of bounds" <<endl;
// return first element.
return arr[0];
}

return arr[i];
}
};

int main() {
safearay A;

cout << "Value of A[2] : " << A[2] <<endl;


cout << "Value of A[5] : " << A[5]<<endl;
cout << "Value of A[12] : " << A[12]<<endl;

return 0;
}
Example : Write a C++ program to overload function call() operator .

#include <iostream.h>

class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12

public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}

// overload function call


Distance operator()(int a, int b, int c) {
Distance D;

// just put random calculation


D.feet = a + c + 10;
D.inches = b + c + 100 ;
return D;
}

// method to display distance


void displayDistance() {
cout << "F: " << feet << " I:" << inches << endl;
}
};

int main() {
Distance D1(11, 10), D2;

cout << "First Distance : ";


D1.displayDistance();

D2 = D1(10, 10, 10); // invoke operator()


cout << "Second Distance :";
D2.displayDistance();

return 0;
}
Inheritance
In C++, inheritance is a process in which one object acquires all the properties and behaviors of its
parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors
which are defined in other class.

In C++, the class which inherits the members of another class is called derived class and the class
whose members are inherited is called base class. The derived class is the specialized class for the base
class.

Syntax:

class derived-class-name :access specifier base-class-name

// body of class

};

The base-class access specifier must be public, private or protected. If no access specifier is present,
the access specifier is private by default if the derived class is a class. In all cases, the base's private
elements remain private to the base and are not accessible by members of the derived class.

When the access specifier for a base class is public, all public members of the base become public
members of the derived class, and all protected members of the base become protected members of the
derived class.

When the base class is inherited by using the private access specifier, all public and protected
members of the base class become private members of the derived class.

When a base class' access specifier is protected, public and protected members of the base become
protected members of the derived class.

Advantage of Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need to define
the member again. So less code is required in the class.
Types Of Inheritance : C++ supports five types of inheritance:

o Single inheritance
o Multilevel inheritance
o Multiple inheritance
o Hierarchical inheritance
o Hybrid inheritance

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.

Example:1

#include <iostream.h>

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

Example:2
#include <iostream.h>
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:3
#include <iostream.h>
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;
}

Output:

Multiplication of a and b is : 20

In the above example, class A is privately inherited. Therefore, the mul() function of class 'A' cannot
be accessed by the object of class B. It can only be accessed by the member function of class B.

How 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:

o Public: When the member is declared as public, it is accessible to all the functions of the
program.
o Private: When the member is declared as private, it is accessible within the class only.
o 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

2. Multilevel Inheritance

Multilevel inheritance is a process of deriving a class from another derived class.


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.

Example:

#include <iostream.h>
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. Multiple Inheritance

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.h>
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()
{
cout << "The value of a is : " <<a<< endl;
cout << "The value of b is : " <<b<< 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 a is : 10
The value of b is : 20
Addition of a and b is : 30

In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.

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. Let's understand this through an example:

#include <iostream.h>
class A
{
public:
void display()
{
cout << "Class A" << endl;
} };
class B
{
public:
void display()
{
cout << "Class B" << 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();
o 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.

} };

An ambiguity can also occur in single inheritance. Consider the following situation:

class A
{
public:
void display()
{
cout<<”Class A”;
}
};
class B
{
public:
void display()
{
cout<<”Class B”;
} };

In the above case, the function of the derived class overrides the method of the base class. Therefore,
call to the display() function will simply call the function defined in the derived class. If we want to
invoke the base class function, we can use the class resolution operator.

int main()
{
B b;
b.display(); // Calling the display() function of B class.
b.B :: display(); // Calling the display() function defined in B class.
}
4. Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.

Example:

#include <iostream.h>
class A
{
protected:
int a;
public:
void get_a()
{
cout << "Enter the value of 'a' : " << endl;
cin>>a;
} };

class B : public A
{
protected:
int b;
public:
void get_b()
{
cout << "Enter the value of 'b' : " << endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
cout << "Enter the value of c is : " << endl;
cin>>c;
}
};

class D : public B, public C


{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
cout << "Multiplication of a,b,c is : " <<a*b*c<< 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

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.h>
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;
cout << "Enter the length and breadth of a rectangle: " << endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
cout << "Area of the rectangle is : " <<m<< endl;
cout << "Enter the base and height of the triangle: " << endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
cout <<"Area of the triangle is : " << n<< 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

Constructor and Destructor Execution in Inheritance:


When an object of a derived class is created, if the base class contains a constructor, it will be called
first, followed by the derived class' constructor. When a derived object is destroyed, its destructor is
called first, followed by the base class' destructor, if it exists (i.e. constructor functions are executed in
their order of derivation. Destructor functions are executed in reverse order of derivation).
Example1:Constructor and Destructor execution in single inheritance

#include <iostream.h>

class base
{
public:
base()
{
cout << "Constructing base\n";
}
~base()
{
cout << "Destructing base\n";
}};
class derived: public base
{
public:
derived(){
cout << "Constructing derived\n";
}
~derived(){
cout << "Destructing derived\n";
}};
void main(){
derived ob;
} Output:
Constructing base
Constructing derived
Destructing derived
Destructing base
Note:In the above program, first base's constructor is executed followed by derived's. Next (because
obis immediately destroyed in this program), derived’s destructor is called, followed by base's.
Example 2: Constructor and Destructor execution in multilevel inheritance
#include <iostream.h>
class base
{
public:
base()
{
cout << "Constructing base\n";
}
~base()
{
cout << "Destructing base\n";
}};
class derived1 : public base{
public:
derived1()
{
cout << "Constructing derived1\n";
}
~derived1(){
cout << "Destructing derived1\n";
}};
class derived2: public derived1
{
public:
derived2()
{
cout << "Constructing derived2\n";
}
~derived2(){
cout << "Destructing derived2\n";
}};
void main()
{
derived2 ob;
}
Output:
Constructing base Constructing
derived1Constructing
derived2 Destructing
derived2Destructing
derived1Destructing base.
Example 3: Constructor and Destructor execution in multiple inheritance
#include <iostream.h>
class base1
{
public:
base1()
{
cout << "Constructing base1\n";
}
~base1(){
cout << "Destructing base1\n";
}};
class base2
{
public:
base2()
{
cout<< "Constructing base2\n";
}
~base2()
{
cout << "Destructing base2\n";
}};
class derived: public base1, public base2
{
public:
derived()
{
cout << "Constructing derived\n";
}
~derived(){
cout << "Destructing derived\n";
}};
void main()
{
derived ob;
}
Output:
Constructing base1
Constructing base2
Constructing derived
Destructing derived
Destructing base2
Destructing base1
Note: In the above program, constructors are called in order of derivation, left to right, as specified in
derived's inheritance list. Destructors are called in reverse order, right to left.

Passing Parameters to Base-Class Constructors:


In cases where only the derived class' constructor requires one or more parameters, we simply use the
standard parameterized constructor syntax. However, how do you pass arguments to a constructor in a
base class? The answer is to use an expanded form of the derived class's constructor declaration that
passes along arguments to one or more base-class constructors. The general form of this expanded
derived-class constructor declaration is shown here:
derived-constructor (arg-list) : base1(arg-list),base2(arg-list), …...,baseN(arg-list)
{
// body of derived constructor
}
Here, base1 through baseN are the names of the base classes inherited by the derived class. Notice that
a colon separates the derived class' constructor declaration from the base-class specifications, and that
the base-class specifications are separated from each other by commas, in the case of multiple base
classes.

Example: Passing Parameters to Base Class Constructors in Single Inheritance


#include <iostream.h>
class base
{
protected:
int i;
public:
base(int x)
{
i=x;
cout << "Constructing base\n";
}
~base(){
cout<< "Destructing base\n";
}};
class derived: public base
{
int j;
public:
derived(int x, int y): base(y)
{
j=x;
cout << "Constructing derived\n";
}
~derived()
{
cout << "Destructing derived\n";
}
void show()
{
cout<< i << " " << j << "\n";
}
};
void main()
{
derived ob(3, 4);
ob.show();
}

Output:

Multipath inheritance
Multipath inheritance in C++ is derivation of a class from other derived classes, which are derived
from the same base class. In this type of inheritance, there involves other inheritance like
multiple, multilevel, hierarchical etc.

It is famously known as diamond problem in computer programming.

Multipath inheritance
 Here, class D is derived from derived classes B & C directly and from class A indirectly.
(hierarchical and multiple)
 Both derived classes inherits the features of base class. Hence when we derive a new class
by inheriting features form these two classes derived from the same base class, then
same features from the first base is inherited to the finally derived class from two paths.
This cause ambiguity in accessing first base class members.

//example of multipath inheritance


//ambiguous base class member access
class A
{
private: //..
public:
void showdata()
{
//...
}
};
class B: virtual public A
{
//...
};
class C: virtual public A
{
//..
};
class D: public B, public C
{
public:
void show_data()
{
showdata();//ambiguous which showdata() ?
}
};

 To eliminate this problem, C++ has a mechanism to inherit a single copy of properties
from the common base class.
 This is done by declaring the base class as virtual while creating derive classes from this
base class.

//example of multipath inheritance


//removing ambiguity with virtual base class
class A
{
private: //..
public:
void showdata()
{
//...
}
};
class B: virtual public A
{
//...
};
class C: virtual public A
{
//..
};
class D: public B, public C
{
public:
void show_data()
{
showdata();//ambiguous which showdata() ?
}
};
 The ambiguity can be resolved without making virtual base class as:
//example of multipath inheritance
//ambiguous base class member access
class A
{
private: //..
public:
void showdata()
{
//...
}
};
class B: virtual public A
{
//...
};
class C: virtual public A
{
//..
};
class D: public B, public C
{
public:
void show_data()
{
showdata();//ambiguous which showdata()
A::showdata();//still ambiguous because B and C both have A::showdata();
B::showdata(); //ok, not ambiguous
C::showdata(); //ok
}
};
Virtual base class

Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a
given class appearing in an inheritance hierarchy when using multiple inheritances.

Need for Virtual Base Classes:


Consider the situation where we have one class A .This class is A is inherited by two other
classes B and C. Both these class are inherited into another in a new class D as shown in figure
below.

As we can see from the figure that data members/function of class A are inherited twice to class D.
One through class B and second through class C. When any data / function member of class A is
accessed by an object of class D, ambiguity arises as to which data/function member would be
called? One inherited through B or the other inherited through C. This confuses compiler and it
displays error.
#include<iostream.h> int main() {
class A { D obj;
public: int b; obj.b = 40; // statement 3
}; obj.b = 30; // statement 4
class B : virtual public A { obj.d1 = 60;
public: int d1; obj.d2 = 70;
}; obj.d3 = 80;
class C: virtual public A { cout<< "\n A : "<< obj.b;
public: int d2; cout<< "\n B : "<< obj.d1;
}; cout<< "\n C : "<< obj.d2;
class D : public B, public C { cout<< "\n D : "<< obj.d3;
public: int d3; }
};
Function Overriding
If derived class defines same function as defined in its base class, it is known as function overriding in
C++. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of
the function which is already provided by its base class.

Example1: Accessing the Overridden Function Example 2: Accessing Overridden Function


Using a Pointer
#include <iostream.h>
#innclude <iostream.h>
class parent_class {
class parent_class
{ public:
public: // define overridden function
// define overridden function void display_message() {
void display_message()
{ cout << "I am the base class function.\n\n";
cout << "I am the base class } };
function.\n\n"; // define the derived class
}
class derived_class : public parent_class
};
// define the derived class {
class derived_class : public parent_class public:
{
// define overriding function
public:
// define overriding function void display_message()
void display_message() {
{
cout << "I am the derived class function.\n\n";
cout << "I am the derived class
function.\n\n"; } };
// call the overridden function of the base int main()
class {
}
// create instances of the derived class
};
int main() derived_class obj1, obj2;
{ // call the overriding function
// create instances of the derived class
obj1.display_message();
derived_class obj;
// pointer of base class type // call the overridden function of the Base class
parent_class *ptr = &obj; obj2.parent_class::display_message();
// call the overridden function
return 0;
ptr->display_message();
return 0; }
}
Example 3: Accessing Overridden Function Example4: Accessing the Overridden Function
Inside the Derived Class Using a Reference
#include<iostream.h>
#include <iostream.h> class BaseClass
class parent_class {
{ public:
public: virtual void Display()
// define overridden function {
void display_message() cout << "\nThis is Display() method of BaseClass";
{ }
cout << "I am the base class function.\n\n"; void Show()
} {
}; cout << "\nThis is Show() method of BaseClass";
// define the derived class }
class derived_class : public parent_class };
{
public: class DerivedClass : public BaseClass
// define overriding function {
void display_message() public:
{
cout << "I am the derived class void Display()
function.\n\n"; {
// call the overridden function of the base cout << "\nThis is Display() method of
class derivedClass";
parent_class::display_message(); }
} };
}; int main()
int main() {
{ DerivedClass dr;
derived_class obj1, obj2; BaseClass &bs = dr;
obj1.display_message(); bs.Display();
return 0;} dr.Show();
}

You might also like