公有继承:
#include<iostream>
using namespace std;
class student
{
public:
void get_value()
{
cin >> num >> name >> sex;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num;
string name;
char sex;
};
class Student1:public student
{
public:
void display_1()
{
//display(); //改为此行后正确,“只能通过基类的公用成员函数来引用基类的私有数据成员”,课本上如是说。
cout << "num:" << num << endl; //报错在这一行 派生类企图引用基类的私有变量!
cout << "age:" << age <<endl;
cout << "address:" << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
student srm;
srm.get_value();
srm.display();
Student1 zxy;
zxy.get_value();
zxy.display();
}
程序报错
正确代码如下,用基类的公用成员函数去引用基类的私有数据成员!
#include<iostream>
using namespace std;
class student
{
public:
void get_value()
{
cin >> num >> name >> sex;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num;
string name;
char sex;
};
class Student1:public student
{
public:
void get_value2()
{
get_value();
cin >> age >> addr;
}
void display_1()
{
display();
//cout << "num:" << num << endl;
cout << "age:" << age <<endl;
cout << "address:" << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
student srm;
srm.get_value();
srm.display();
Student1 zxy;
zxy.get_value2();
zxy.display_1();
}
#include<iostream>
using namespace std;
class student
{
public:
student(int i=25,string s="srm",char c='b'):num(i),name(s),sex(c){}
void get_value()
{
cin >> num >> name >> sex;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num;
string name;
char sex;
};
class Student1:private student
{
public:
void get_value2()
{
get_value();
cin >> age >> addr;
}
void display_1()
{
display();
//cout << "num:" << num << endl;
cout << "age:" << age <<endl;
cout << "address:" << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
student srm;
//srm.get_value();
srm.display();
Student1 zxy;
zxy.get_value2();
//zxy.get_value(); //用这个函数就开始出错了,因为get_value()是基类的公用,继承后变成了派生类的私有
zxy.display_1();
}
简单的将继承改为 私有继承,仍然能编译通过,
加入构造函数
#include<iostream>
using namespace std;
class student
{
public:
student(int i=25,string s="srm",char c='b'):num(i),name(s),sex(c){}
void get_value()
{
cin >> num >> name >> sex;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num;
string name;
char sex;
};
class Student1:private student
{
public:
Student1(int i=25,string s="rcz"):student(),age(i),addr(s){}
void get_value2()
{
get_value();
cin >> age >> addr;
}
void display_1()
{
display();
//cout << "num:" << num << endl;
cout << "age:" << age <<endl;
cout << "address:" << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
student srm;
//srm.get_value();
srm.display();
cout << endl;
Student1 zxy;
//zxy.get_value();
zxy.display_1();
}
保护成员与保护继承
保护成员对类的用户来说是私有成员,类外不可访问,但是保护成员可以被类的派生类的成员函数引用!
就像父辈的保险箱,只能被子女(其派生类)打开。