I found the following thing :-
Public mode in inheritance: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class. Private members of the base class will never get inherited in sub class.
But on running the following program the derived class is accessing the private data members of the base class, HOW AND WHY
The program is as follows :-
#include<iostream>
using namespace std ;
class Student
{
private : long int sapId ;
char name[20] ;
public : void getStudent()
{
cout << "Enter The Sap Id :- " ;
cin >> sapId ;
cout << "Enter The Name of The Student :- " ;
cin >> name ;
}
void putStudent()
{
cout << "SAP ID :- " << sapId << endl ;
cout << "Name :- " << name << endl ;
}
} ;
class CSE : public Student
{
protected : char section ;
int rollNo ;
public : void getCSE()
{
cout << "Enter Section :- " ;
cin >> section ;
cout << "Enter Roll Number :- " ;
cin >> rollNo ;
}
void putCSE()
{
cout << "Section :- " << section << endl ;
cout << "Roll Number :- " << rollNo << endl ;
}
} ;
main()
{
CSE obj ;
obj.getStudent() ;
obj.getCSE() ;
cout << endl ;
obj.putStudent() ;
obj.putCSE() ;
return 0 ;
}