1
Method Overriding
• As we know, inheritance is a feature of OOP that
allows us to create derived classes from a base class.
The derived classes inherit features of the base
class.
• Suppose, the same function is defined in both the
derived class and the based class. Now if we call this
function using the object of the derived class, the
function of the derived class is executed.
• This is known as function overriding in C++. The
function in derived class overrides the function in
2
Method Overriding
• To override a function you must have the same signature in
child class. By signature we mean the data type and
sequence of parameters.
• In function overriding, the function in parent class is called
the overridden function and function in child class is called
overriding function.
Prepard by Ashish Mishra
3
Requirements for Overriding a
Function
1.Inheritance should be there. Function
overriding cannot be done within a class. For
this we require a derived class and a base
class.
2.Function that is redefined must have exactly
the same declaration in both base and derived
class, that means same name, same return
type and same parameter list.
Prepard by Ashish Mishra
4
Method Overriding
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "Derived Function" << endl;
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}
Output: Derived Function
Derived Function
5
Calling the overrided function using Base class
and Derived class object. Base class object will
call base version of the function and derived
class's object will call the derived version of
the function.
class Base
{
public:
void show()
{
cout << "Base classn";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Classn";
}
}
int main()
{
Base b; //Base class object
Derived d; //Derived class object
b.show();
d.show();
}
OUTPUT:-
Base class
Derived class
6
Call Overridden Function From Derived Class
#include <iostream>
using namespace std;
class Parent {
public:
void aPrint()
{
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void aPrint()
{
cout << "Derived Function" << endl;
// call of overridden function
Parent::aPrint();
}
};
int main()
{
Child Child_Derived;
Child_Derived.aPrint();
return 0;
}
Prepard by Ashish Mishra
Output-
Derived Function
Base Function
7
Access overridden function using pointer
of Base type
class Parent {
public:
void hello()
{
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void hello()
{
cout << "Derived Function" << endl;
}
};
int main()
{
Child Child_Derived;
// pointer of Parent type that points to derived1
Parent* ptr = &Child_Derived;
// call function of Base class using ptr
ptr->hello();
return 0;
}
Output
Base Function
8
This also Access overridden function
int main()
{
Child Child_Derived;
Parent ptr = Child_Derived;
ptr.hello();
return 0;
}
Output
Base Function
9
What is output??
#include <iostream>
class Shape {
public:
void display() {
std::cout << "Inside Shape class“;
}
};
class Square : public Shape {
public:
void display() {
std::cout << "Inside Square class" ;
}
};
int main() {
Shape* shapePtr = new Square;
shapePtr->display();
delete shapePtr;
return 0;
}
Output
Inside Shape class
10
What is output??
#include <iostream>
class Shape {
public:
void draw() {
std::cout << "Drawing a shape" << std::endl;
}};
class TwoDimensionalShape : public Shape {
public:
void draw() {
std::cout << "Drawing a 2D shape" << std::endl;
}};
class ThreeDimensionalShape : public Shape {
public:
void draw() {
std::cout << "Drawing a 3D shape" << std::endl;
}};
class Sphere : public ThreeDimensionalShape {
public:
void draw() {
std::cout << "Drawing a sphere" << std::endl;
}};
int main() {
Sphere s;
Shape* shapePtr1 = &s;
Shape shape2 = s;
TwoDimensionalShape* shapePtr2 = new TwoDimensionalShape;
ThreeDimensionalShape* shapePtr3 = new Sphere;
shapePtr1->draw();
shape2.draw();
shapePtr2->draw();
shapePtr3->draw();
return 0;
Output
Drawing a shape
Drawing a shape
Drawing a 2D shape
Drawing a 3D shape

Lecture 14-overridding.pptx..................................

  • 1.
    1 Method Overriding • Aswe know, inheritance is a feature of OOP that allows us to create derived classes from a base class. The derived classes inherit features of the base class. • Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed. • This is known as function overriding in C++. The function in derived class overrides the function in
  • 2.
    2 Method Overriding • Tooverride a function you must have the same signature in child class. By signature we mean the data type and sequence of parameters. • In function overriding, the function in parent class is called the overridden function and function in child class is called overriding function. Prepard by Ashish Mishra
  • 3.
    3 Requirements for Overridinga Function 1.Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class. 2.Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list. Prepard by Ashish Mishra
  • 4.
    4 Method Overriding #include <iostream> usingnamespace std; class Base { public: void print() { cout << "Base Function" << endl; } }; class Derived : public Base { public: void print() { cout << "Derived Function" << endl; } }; int main() { Derived derived1; derived1.print(); return 0; } Output: Derived Function Derived Function
  • 5.
    5 Calling the overridedfunction using Base class and Derived class object. Base class object will call base version of the function and derived class's object will call the derived version of the function. class Base { public: void show() { cout << "Base classn"; } }; class Derived:public Base { public: void show() { cout << "Derived Classn"; } } int main() { Base b; //Base class object Derived d; //Derived class object b.show(); d.show(); } OUTPUT:- Base class Derived class
  • 6.
    6 Call Overridden FunctionFrom Derived Class #include <iostream> using namespace std; class Parent { public: void aPrint() { cout << "Base Function" << endl; } }; class Child : public Parent { public: void aPrint() { cout << "Derived Function" << endl; // call of overridden function Parent::aPrint(); } }; int main() { Child Child_Derived; Child_Derived.aPrint(); return 0; } Prepard by Ashish Mishra Output- Derived Function Base Function
  • 7.
    7 Access overridden functionusing pointer of Base type class Parent { public: void hello() { cout << "Base Function" << endl; } }; class Child : public Parent { public: void hello() { cout << "Derived Function" << endl; } }; int main() { Child Child_Derived; // pointer of Parent type that points to derived1 Parent* ptr = &Child_Derived; // call function of Base class using ptr ptr->hello(); return 0; } Output Base Function
  • 8.
    8 This also Accessoverridden function int main() { Child Child_Derived; Parent ptr = Child_Derived; ptr.hello(); return 0; } Output Base Function
  • 9.
    9 What is output?? #include<iostream> class Shape { public: void display() { std::cout << "Inside Shape class“; } }; class Square : public Shape { public: void display() { std::cout << "Inside Square class" ; } }; int main() { Shape* shapePtr = new Square; shapePtr->display(); delete shapePtr; return 0; } Output Inside Shape class
  • 10.
    10 What is output?? #include<iostream> class Shape { public: void draw() { std::cout << "Drawing a shape" << std::endl; }}; class TwoDimensionalShape : public Shape { public: void draw() { std::cout << "Drawing a 2D shape" << std::endl; }}; class ThreeDimensionalShape : public Shape { public: void draw() { std::cout << "Drawing a 3D shape" << std::endl; }}; class Sphere : public ThreeDimensionalShape { public: void draw() { std::cout << "Drawing a sphere" << std::endl; }}; int main() { Sphere s; Shape* shapePtr1 = &s; Shape shape2 = s; TwoDimensionalShape* shapePtr2 = new TwoDimensionalShape; ThreeDimensionalShape* shapePtr3 = new Sphere; shapePtr1->draw(); shape2.draw(); shapePtr2->draw(); shapePtr3->draw(); return 0; Output Drawing a shape Drawing a shape Drawing a 2D shape Drawing a 3D shape