C++: Inheritance |Single inheritance

Single Inheritance

Example:1
Write a C++ program to declare a class named student with attributes as roll and name. Declare another class named physical with attributes as age, height, and weight. The class physical is the derived class of student. Take the input for the details and display them.
Sol:

Class diagram:

Base Class name:Student
Attributes :Roll,name
Methods/member functions:
read(), show()

Derived Class name:Physical
Attributes :Age,ht,wt
Methods/member functions:
get(), disp()

#include<iostream>
using namespace std;
class student
{
	private:
		int roll;
		char name[20];
	public:
		void read();
		void show();
};
class physical:public student
{
	private:
		float age,ht,wt;
	public:
		void get();
		void disp();
};
void student::read()
{
	cout<<"Enter roll and name ";
	cin>>roll>>name;
}
void student::show()
{
	cout<<"roll "<<roll<<" name "<<name<<endl;
}
void physical::get()
{
	read();
	cout<<"Enter age,ht and weight ";
	cin>>age>>ht>>wt;
}
void physical::disp()
{
	
	show();
	cout<<"age "<<age<<" height "<<ht<<" weight " <<wt<<endl;
}

int main()
{
	physical p;
	cout<<"size of student "<<sizeof(student)<<endl;
	cout<<"size of office "<<sizeof(physical)<<endl;
	p.get();
	p.disp();
	return(0);
}

Output:

size of student 24
size of office 36
Enter roll and name 101
Amit
Enter age,ht and weight 10
5
30
roll 101 name Amit
age 10 height 5 weight 30

 

Question:
Can the name of the member function of a base class and derived class be the same?
Sol:

Yes, Names can be the same.

Question:
How can you resolve ambiguity if the name of the member functions of the base class and derived class is the same?
Sol:

If name of the member functions of base class and derived class are same then in order to call the member function of base class from within the member function of derived class we have to mention the name of the class along with SRO(::).
Syntax:
Classname::memberfunctionname();
Student::read();