谭浩强C++ 第十一章

第十一章课后习题

1、补充例11.1——public继承 (P338)
//公有继承, 
//基类 private->派生类 不可访问;    基类 public->派生类 public;    基类 protected->派生类 protected; 
#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<<"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 main(){
   
    Student1 stud;
    //stud.get_value(); //基类公有,在公有继承派生类中仍是公有,可以在类外调用 
    stud.get_value1();
    stud.display1();
    return 0;
}
2、补充例11.2——private继承 (P340)
//私有继承, 
//基类 private->派生类 不可访问;    基类 public->派生类 private;    基类 protected->派生类 private;  
#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<<"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 main(){
   
    Student1 stud;
    stud.get_value1();
    //stud.get_value(); //基类公有,在私有继承的派生类私有,不可在类外调用 
    stud.display1();
    return 0;
}
3、补充例11.3——protected继承 (P344)
//保护继承, 
//基类 private->派生类 不可访问;   基类 public->派生类 protected;    基类 protected->派生类 protected;
#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(){
   
			//display();
			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 
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值