第十一章课后习题
1、补充例11.1——public继承 (P338)
#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_value1(){
get_value();
cin>>age>>addr;
}
void display1(){
display();
cout<<"age: "<<age<<endl;
cout<<"addr: "<<addr<<endl;
}
private:
int age;
string addr;
};
int main(){
Student1 stud;
stud.get_value1();
stud.display1();
return 0;
}
2、补充例11.2——private继承 (P340)
#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:private Student{
public:
void get_value1(){
get_value();
cin>>age>>addr;
}
void display1(){
display();
cout<<"age: "<<age<<endl;
cout<<"addr: "<<addr<<endl;
}
private:
int age;
string addr;
};
int main(){
Student1 stud;
stud.get_value1();
stud.display1();
return 0;
}
3、补充例11.3——protected继承 (P344)
#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;
}
protected:
int num;
string name;
char sex;
};
class Student1:protected Student{
public:
void get_value1(){
get_value();
cin>>age>>addr;
}
void display1(){
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"age: "<<age<<endl;
cout<<"addr: "<<addr<<endl;
}
private:
int age;
string addr;
};
int mai