Home | | Computer Science 11th std | C++: Overriding / Shadowing Base class functions in derived class

Example Program in C++ - C++: Overriding / Shadowing Base class functions in derived class | 11th Computer Science : Chapter 16 : Inheritance

Chapter: 11th Computer Science : Chapter 16 : Inheritance

C++: Overriding / Shadowing Base class functions in derived class

In case of inheritance there are situations where the member function of the base class and derived classes have the same name.

Overriding / Shadowing Base class functions in derived class

 

In case of inheritance there are situations where the member function of the base class and derived classes have the same name. If the derived class object calls the overloaded member function it leads confusion to the compiler as to which function is to be invoked. The derived class member function have higher priority than the base class member function.This shadows the member functionof the base class which has the same name like the member function of the derived class. The scope resolution operator resolves this problem.

 

Illustration 16.11 the use of scope resolution operator in derived class

#include<iostream>

#include<string>

using namespace std;

class Employee

{

private:

      char name[50];

      int code;

public:

      void getdata();

      void display();

};

class staff: public Employee

{

private:

      int ex;

public:

      void getdata();

      void display();

};

void Employee::display()

{

      cout<<"Name:"<<name<<endl;

      cout<<"Code:"<<code<<endl;

}

void staff::display()

{

      Employee :: display();//overriding

      cout<<"Experience:"<<ex<<" Years"<<endl;

}

int main()

{

      staff s;

      cout<<"Enter data"<<endl;

      s.getdata();

      cout<<endl<<endl<<"\tDisplay Data"<<endl;

      s.display();

      return 0;

}

Output

Enter data

Name: SUGANYA

Code: 1201

Experience: 30

Display Data

Name: SUGANYA

Code:1201

Experience:30 Years

 

In the above program getdata() and display() are defined both in base and in derived class. So when the derived class staff inherits the properties of Employee class it will have two getdata() and display() each. To differentiate the derived getdata() and display() from the defined getdata() and display() :: (scope resolution) operator is given along with the base class name to the base class members

When a derived class member function has the same name as that of its base class member function ,the derived class member function shadows/hides the base class’s inherited function .This situation is called function overriding and this can be resolved by giving the base class name followed by :: and the member function name.

 

thisPointer

 

'this' pointer is a constant pointer that holds the memory address of the current object. .It identifies the currently calling object.It is useful when the argument variable name in the member function and the data member name are same. To identify the datamember it will be given as this->data member name

 

Illutration 16.8.1 illustrates the use of this pointer

#include<iostream>

using namespace std;

class T

{

public:

int x;

void foo()

{

x = 6;  // same as this->x = 6;

this->x = 5; // explicit use of this->

cout<<endl<<x<<" "<<this->x;

}

void foo(int x) // parameter x shadows the member with the same name

{

this->x = x; // unqualified x refers to the parameter.'this->' required for disambiguation

cout<<endl<<x<<" "<<this->x;

}};

int main()

{

      t1,t2;

      t1.foo();

      t2.foo();

}

Ouput

5

5

--------------------------------

Process exited after 0.1 seconds with return value 0 Press any key to continue . . .

 

Another Example Program 16.8.1A using this pointer

#include <iostream>

using namespace std;

class Container {

public:

// Constructor definition

Container(double l = 2.0, double b = 2.0, double h = 2.0)

{

cout<<"Constructor called." << endl; length = l;

breadth = b;

height = h;

}

double Volume() {

return length * breadth * height;

}

int compare(Container container)

{

return this->Volume() >Container.Volume();

}

private:

      double length; // Length of a Container

      double breadth;        // Breadth of a Container

      double height; // Height of a Container

};

int main(void) {

Container Container1(3.3, 1.2, 1.5); // Declare Container1

Container Container2(8.5, 6.0, 2.0); // Declare Container2

      if(Container1.compare(Container2))

      {

      cout << "Container2 is smaller than Container1" <<endl;

      } else {

      cout << "Container2 is equal to or larger than Container1" <<endl;

      }

return 0;

}

Output

Constructor called.

Constructor called.

Container2 is equal to or larger than Container1

--------------------------------

Process exited after 0.09358 seconds with return value 0

Press any key to continue . . .

 

Tags : Example Program in C++ , 11th Computer Science : Chapter 16 : Inheritance
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 16 : Inheritance : C++: Overriding / Shadowing Base class functions in derived class | Example Program in C++


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.