Program To Illustrate The Single Inheritance :
#include<iostream>
using namespace std;
class base
{
int a;
protected:
int b;
public:
int c;
void getdata();
void putdata();
};
void base:: getdata()
{
cout<<"Enter The Values Of A,B,C\n";
cin>>a>>b>>c;
}
void base::putdata()
{
cout<<"Values Are\n";
cout<<"A="<<a;
cout<<"\nB="<<b;
cout<<"\nC="<<c;
}
class derive:public base
{
int d;
protected:
int e;
public:
void display()
{
cout<<"\nD="<<d<<"\nE="<<e;
}
void input()
{
cout<<"\nEnter D AND E\n";
cin>>d>>e;
}
};
main()
{
derive p;
p.getdata();
p.input();
p.putdata();
p.display();
}
p.base::display();
#include<iostream>
using namespace std;
class base
{
int a;
protected:
int b;
public:
int c;
void getdata();
void putdata();
};
void base:: getdata()
{
cout<<"Enter The Values Of A,B,C\n";
cin>>a>>b>>c;
}
void base::putdata()
{
cout<<"Values Are\n";
cout<<"A="<<a;
cout<<"\nB="<<b;
cout<<"\nC="<<c;
}
class derive:public base
{
int d;
protected:
int e;
public:
void display()
{
cout<<"\nD="<<d<<"\nE="<<e;
}
void input()
{
cout<<"\nEnter D AND E\n";
cin>>d>>e;
}
};
main()
{
derive p;
p.getdata();
p.input();
p.putdata();
p.display();
}
Ambiguity:
When we use same name for member function in both base class and derived class then there is an ambiguity that which function will be invoked by the object of derived class in main function.
Solution:
So After checking it i found that the function in derived class is invoked by the object.Only if it is defined,if it is not defined then it will call the base class function.
We can Also Solve This Ambiguity By Using Scope Resolution Operator As Follows:
p.base::display();
p.derive::display();
Also See Our C program To print Alphabet pattern