>

Thursday, 17 December 2015

Name Hiding

#include "stdafx.h"
#include "conio.h"
#include
using namespace std;
class Shape
{
public:
void draw(){cout<<"Shape draw"<<endl;}
void dimensions(int dimension){cout<<"Shape dimensions"<<endl;}
};
class Rectangle:public Shape
{
public:
void draw(){cout<<"Rectangle draw"<<endl;}
void dimensions(int width,int height)
{
cout<<"Rectangle dimensions"<<endl;
}
};
class Circle:public Shape
{
public:
void draw()
{
cout<<"Circle draw"<<endl;
}
void dimensions(float radius,int x,int y)
{
cout<<"Circle dimensions"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Shape *s;
int choice = 0;
cout<<"enter 1 for rectangle ,2 for circle"<>choice;
if(choice == 1)
{
Rectangle R;
s=&R;
s->draw();
//Name Hiding
R.dimensions(10,20); //Compilation - oK
R.dimensions(10); //error C2660: 'Rectangle::dimensions' : function does not take 1 arguments
}
else if(choice == 2)
{
Circle C;
s=&C;
s->draw();
}
else
cout<<"wrong selection"<<endl;
return 0;
}
In both shape and rectangle classes, the dimension( ) method has same name but different signature.
If a call to the dimension () method in base class is made by Derived class object, it results in compilation
Error.

0 comments:

Post a Comment