<分区>
我发现了以下内容:-
继承中的公共(public)模式:如果我们从公共(public)基类派生子类。然后基类的公共(public)成员将在派生类中变为公共(public)成员,而基类的 protected 成员将在派生类中变为 protected 。基类的私有(private)成员永远不会在子类中被继承。
但是在运行以下程序时,派生类正在访问基类的私有(private)数据成员,如何以及为什么
程序如下:-
#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 ;
}