C++面向对象程序设计第五章课后习题

一、

#include<iostream>
using namespace std;


class Student{
	public:
		void get_value()
		{
			cout << "please input num, name and sex:" << endl;
			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_value_1()
		{
			cout << "please input age, addr:" << endl;
			cin >> age >> addr;
		}
		void display_1()
		{
			display();
			cout << "age:" << age << endl;
			cout << "address:" << addr << endl;
		}
	private:
		int age;
		string addr;
}; 

int main()
{
	Student1 stud;
	stud.get_value();
	stud.get_value_1();
	stud.display_1();
	return 0;
}

我的样例运行结果如下:
在这里插入图片描述

二、

#include<iostream>
using namespace std;


class Student{
	public:
		void get_value()
		{
			cout << "please input num, name and sex:" << endl;
			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_value_1()
		{
			get_value();
			cout << "please input age, addr:" << endl;
			cin >> age >> addr;
		}
		void display_1()
		{
			display();
			cout << "age:" << age << endl;
			cout << "address:" << addr << endl;
		}
	private:
		int age;
		string addr;
}; 

int main()
{
	Student1 stud;
	stud.get_value_1();
	stud.display_1();
	return 0;
}

我的样例运行结果如下:
在这里插入图片描述

三、

#include<iostream>
#include<string>
using namespace std;

class Student{
	public:
	
	protected:
		int num;
		string name;
		char sex;
};

class Student1:protected Student{
	public:
		void get_value1();
		void display1();
	private:
		int age;
		string addr;
};

void Student1::get_value1()
{
	cout << "please input num, name and sex:"<< endl;
	cin >> num >> name >> sex;
	cout << "please input age and addr:" << endl; 
	cin >> age >> addr;
}

void Student1::display1()
{
	cout << "num:" << num << endl;
	cout << "name:" << name << endl;
	cout << "sex:" << sex << endl;
	cout << "age:" << age << endl;
	cout << "address:" << addr << endl;
}

int main()
{
	Student1 stud;
	stud.get_value1();
	stud.display1();
	return 0;
}

运行结果同上

四、

修改为公用继承public方式的C++代码如下:

#include<iostream>
#include<string>
using namespace std;

class Student{
	public:
	
	protected:
		int num;
		string name;
		char sex;
};

class Student1:public Student{
	public:
		void get_value1();
		void display1();
	private:
		int age;
		string addr;
};

void Student1::get_value1()
{
	cout << "please input num, name and sex:"<< endl;
	cin >> num >> name >> sex;
	cout << "please input age and addr:" << endl; 
	cin >> age >> addr;
}

void Student1::display1()
{
	cout << "num:" << num << endl;
	cout << "name:" << name << endl;
	cout << "sex:" << sex << endl;
	cout << "age:" << age << endl;
	cout << "address:" << addr << endl;
}

int main()
{
	Student1 stud;
	stud.get_value1();
	stud.display1();
	return 0;
}

如果基类中有public的成员,则公用继承和保护继承不能互相代替。
派生类公用继承的情况下,类内类外皆可访问基类public成员
派生类保护继承的情况下,类内可以访问public成员,类外不可以用派生类访问基类public成员

五、

(1)b1.i可,因为i是public且是公有继承,b1.j、b1.k不可,j是保护成员,k是私有成员,类外不可访问

(2)都可以

测试C++代码如下:

#include<iostream>
using namespace std;

class A{
	public:
		void f1();
		int i;
	protected:
		void f2()
		{
			cout << "f2()" << endl;
		}
		int j;
	private:
		int k;
};

class B:public A{
	public:
		void f3();
	protected:
		int m;
	private:
		int n;
};

void A::f1()
{
	cout << "f1()" << endl;
}


void B::f3()
{
	f1();
	f2();
}

class C:public B{
	public:
		void f4();
	private:
		int p;
};

int main()
{
	A a1;
	B b1;
	C c1;
	b1.f3();
	return 0;
}

(3)i和j可以,k不可以,因为k是基类A中的私有成员

测试C++代码如下:

#include<iostream>
using namespace std;

class A{
	public:
		void f1();
		int i;
	protected:
		void f2()
		{
			cout << "f2()" << endl;
		}
		int j;
	private:
		int k;
};

class B:public A{
	public:
		void f3();
	protected:
		int m;
	private:
		int n;
};

void A::f1()
{
	cout << "f1()" << endl;
}


void B::f3()
{
	f1();
	f2();
	i = 0;
	j = 0;
}

class C:public B{
	public:
		void f4();
	private:
		int p;
};

int main()
{
	A a1;
	B b1;
	C c1;
	b1.f3();
	return 0;
}

(4)c1.i可,其他的protected、private都不可在类外使用

(5)f1()、f3()、f4()可,f2()不可

(6)都可以

测试C++代码如下:

#include<iostream>
using namespace std;

class A{
	public:
		void f1();
		int i;
	protected:
		void f2()
		{
			cout << "f2()" << endl;
		}
		int j;
	private:
		int k;
};

class B:public A{
	public:
		void f3();
	protected:
		int m;
	private:
		int n;
};

void A::f1()
{
	cout << "f1()" << endl;
}


void B::f3()
{
	f1();
	f2();
	i = 0;
	j = 0;
}

class C:public B{
	public:
		void f4()
		{
			f1();
			f2();
			f3();
		}
	private:
		int p;
};

int main()
{
	A a1;
	B b1;
	C c1;
	
	b1.f3();
	c1.f4();
	return 0;
}

六、分析所有成员在各类的范围内的访问属性

  • 对于A类:
    在main函数中用a1能引用A类成员函数f1()
    在A中成员函数中可以调用i,若成员函数为f1(),则还可以调用f2(),若成员函数为f2(),则还可以调用f1()
  • 对于B类:
    在main函数中用b1能引用A类成员函数f1()以及派生类B类中的f3()和k
    在B中成员函数中可调用f1(),f2(),k,m
  • 对于C类:
    在main函数中用c1只能引用C类中的f4()
    在C中成员函数可以调用f1(),f2(),f3(),k,m,n
  • 对于D类:
    在main函数中用d1只能引用D类成员函数f5()
    在D中成员函数中可调用f1(),f2(),f3(),k,f4(),C中的m成员变量,p, q

七、

#include<iostream>
using namespace std;

class A{
	public:
		A()
		{
			a = 0;
			b = 0;
		}
		A(int i)
		{
			a = i;
			b = 0;
		}
		A(int i, int j)
		{
			a = i;
			b = j;
		}
		void display()
		{
			cout << "a = " << a << " b = " << b;
		}
	private:
		int a, b;
}; 

class B : public A{
	public:
		B()
		{
			c = 0;
		}
		B(int i) : A(i)
		{
			c = 0;
		}
		B(int i, int j) : A(i, j)
		{
			c = 0;
		}
		B(int i, int j, int k) : A(i, j)
		{
			c = k;
		}
		void display1()
		{
			display();
			cout << "c = " << c << endl;
		}
	private:
		int c;
}; 

int main()
{
	B b1;
	B b2(1);
	B b3(1, 3);
	B b4(1, 3, 5);
	b1.display1();
	b2.display1();
	b3.display1();
	b4.display1();
	return 0;
}

运行结果如下:
在这里插入图片描述

八、

#include<iostream>
using namespace std;

class A{
	public:
		A()
		{
			cout << "constructing A" << endl;
		}
		~A()
		{
			cout << "destructing A" << endl;
		}
};

class B : public A{
	public:
		B()
		{
			cout << "constructing B" << endl;
		}
		~B()
		{
			cout << "destructing B" << endl;
		}
};

class C : public B{
	public:
		C()
		{
			cout << "constructing C" << endl;
		}
		~C()
		{
			cout << "destructing C" << endl;
		}
};

int main()
{
	C c1;
	return 0;
}

在这里插入图片描述
Attention!!!
在声明派生类时, 一般还应当自己定义派生类的构造函数和析构函数,因为构造函数和析构函数不能从基类继承

由于初始化C就会先构造A再构造B,所以构造完毕后就从C开始一步步析构掉

九、

因为题目是说Teacher和Cadre是基类,所以不得不在两个基类中重复定义姓名、年龄等通用信息,题目不严谨,一般都会定义一个People类,让Teacher和Cadre继承它

详细C++代码如下:

#include<bits/stdc++.h>
using namespace std;

class Teacher{
	public:
		Teacher(char *p, int age, char sex, char *address, int phone, char *title);
		void display();
	private:
		char Name[20];
		int Age;
		char Sex;
		char Address[20];
		int Phone;
		char Title[20];
};

Teacher::Teacher(char *p, int age, char sex, char *address, int phone, char *title)
{
	strcpy(Name, p);
	Age = age;
	Sex = sex;
	strcpy(Address, address);
	Phone = phone;
	strcpy(Title, title);
}

void Teacher::display()
{
	cout << "Name:" << Name << endl;
	cout << "Age:" << Age << endl;
	cout << "Sex:" << Sex << endl;
	cout << "Address:" << Address << endl;
	cout << "Phone:" << Phone << endl;
	cout << "Title:" << Title << endl; 
}

class Cadre{
	public:
		Cadre(char *p, int age, char sex, char *address, int phone, char *post);
		char *GetPost()
		{
			return Post;
		}
	private:
		char Name[20];
		int Age;
		char Sex;
		char Address[20];
		int Phone;
		char Post[20];
};

Cadre::Cadre(char *p, int age, char sex, char *address, int phone, char *post)
{
	strcpy(Name, p);
	Age = age;
	Sex = sex;
	strcpy(Address, address);
	Phone = phone;
	strcpy(Post, post);
}

class Teacher_Cadre : public Teacher, public Cadre{
	public:
		Teacher_Cadre(char *p, int age, char sex, char *address, int phone, char *title, char *post, float wages):Teacher(p, age, sex, address, phone, title), Cadre(p, age, sex, address, phone, post)
		{
			Wages = wages;
		}
	void show();
	private:
		float Wages;
};



void Teacher_Cadre::show()
{
	Teacher::display();
	cout << "Post:" << GetPost() << endl;
	cout << "Wages:" << Wages << endl;
}

int main()
{
	char name[20] = "Rihood";
	char address[20] = "Cyprus";
	char title[20] = "Teacher";
	char post[20] = "Leader";
	Teacher_Cadre tc(name, 20, 'm', address, 666, title, post, 30000);
	tc.show();
	return 0;
}

运行结果如下:
在这里插入图片描述

十、

#include<bits/stdc++.h>
using namespace std;

class Teacher{
	public:
		Teacher(int num, string name, char sex)
		{
			Num = num;
			Name = name;
			Sex = sex;
		}
		void display()
		{
			cout << "Num:" << Num << endl;
			cout << "Name:" << Name << endl;
			cout << "Sex:" << Sex << endl; 
		}
	private:
		int Num;
		string Name;
		char Sex;
};

class BirthDate{
	public:
		BirthDate()
		{
		}
		BirthDate(int year, int month, int day)
		{
			Year = year;
			Month = month;
			Day = day;
		}
		void display2()
		{
			cout << "Year:" << Year << endl;
			cout << "Month:" << Month << endl;
			cout << "Day:" << Day << endl; 
		}
	private:
		int Year;
		int Month;
		int Day; 
};

class Professor : public Teacher{
	public:
		Professor(int num, string name, char sex, BirthDate birth):Teacher(num, name, sex)
		{
			birthday = birth;
		}
		void Show()
		{
			display();
			birthday.display2();
		}
		BirthDate birthday;
};

int main()
{
	BirthDate birth(2000, 2, 30);
	Professor prof1(24, "Rihood", 'm', birth);
	prof1.Show();
	BirthDate birth2(1999, 3, 14);
	prof1.birthday = birth2;
	prof1.Show();
	return 0;
}

运行结果如下:
在这里插入图片描述
Attention!!!
BirthDate()无参构造函数一定要有

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值