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.
#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;
}
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.
'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
#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();
}
5
5
--------------------------------
Process exited after 0.1 seconds with return value 0 Press any
key to continue . . .
#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;
}
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 . . .
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.