以往章节
1. (简答题) 将例5.1的程序片段补充和改写成一个完整、正确的程序,用公用继承方式。在程序中应包括输入数据的函数,在程序运行时输入num,name,sex,age,addr的值,程序应输出以上5个数据的值。
#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 display_1() {
display();
cout << "age: " << age << endl;
cout << "address: " << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
Student1 s1;
s1.get_value1();
s1.display_1();
return 0;
}
2. (简答题) 将例5.2的程序片段补充和改写成一个完整、正确的程序,用私有继承方式。在程序中应包括输入数据的函数,在程序运行时输入num,name,sex,age,addr的值,程序应输出以上5个数据的值。
#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 display_1() {
display();
cout << "age: " << age << endl;
cout << "address: " << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
Student1 s1;
s1.get_value1();
s1.display_1();
return 0;
}
3. (简答题) 将例5.3的程序修改、补充,写成一个完整、正确的程序,用保护继承方式。在程序中应包括输入数据的函数。
#include <iostream>
using namespace std;
class Student
{
public:
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_value() {
cin >> num >> name >> sex >> age >> addr;
}
void display_1() {
display();
cout << "age: " << age << endl;
cout << "address: " << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
Student1 s1;
s1.get_value();
s1.display_1();
return 0;
}
4. (简答题) 修改例5.3的程序,改为用公用继承方式。上机调试程序,使之能正确运行并得到正确的结果。对这两种继承方式作比较分析,考虑在什么情况下二者不能互相代替。
#include <iostream>
using namespace std;
class Student
{
public:
void display()
{
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
}
protected:
int num;
string name;
char sex;
};
class Student1 : public Student
{
public:
void get_value() {
cin >> num >> name >> sex >> age >> addr;
}
void display_1() {
display();
cout << "age: " << age << endl;
cout << "address: " << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
Student1 s1;
s1.get_value();
s1.display_1();
return 0;
}
//派生类公用继承的情况下,类内类外皆可访问基类public成员
//派生类保护继承的情况下,类内可以访问public成员,类外不可以用派生类访问基类public成员
5. (简答题)有以下程序结构,请分析访问属性。
class A//A为基类
{public:
void f1( );
int i;
protected:
void f2( );
int j;
private:
int k;
};
class B: public A //B为A的公用派生类
{public:
void f3( );
protected:
int m;
private:
int n;
};
class C: public B //C为B的公用派生类
{public:
void f4( );
private:
int p;
};
int main( )
{A a1; //a1是基类A的对象
B b1; //b1是派生类B的对象
C c1; //c1是派生类C的对象
return 0;
}
问:
(1) 在main函数中能否用b1.i,b1.j和b1.k引用派生类B对象b1中基类A的成员?
(2) 派生类B中的成员函数能否调用基类A中的成员函数f1和f2?
(3) 派生类B中的成员函数能否引用基类A中的数据成员i,j,k?
(4) 能否在main函数中用c1.i,c1.j,c1.k,c1.m,c1.n,c1.p引用基类A的成员i,j,k,派生类B的成员m,n,以及派生类C的成员p?
(5) 能否在main函数中用c1.f1(),c1.f2(),c1.f3()和c1.f4()调用f1,f2,f3,f4成员函数?
(6) 派生类C的成员函数f4能否调用基类A中的成员函数f1,f2和派生类中的成员函数f3?
-
b1.i可以因为i是public且是公有继承,b1.j、b1.k不可以,j是保护成员,k是私有成员,类外不可访问
-
都可以因为 f1 f2一个是公用类一个是保护类,继承过来是派生类的公用成员和保护成员,而私有类还是基类的私有成员没有成为派生类的私有成员调用还得是基类成员函数调用
-
i和j可以,k不可以,因为k是基类A中的私有成员
-
c1.i可,其他的保护类、私有类都不可在类外使用
-
f1()、f3()、f4()可以,f2()不可以
-
都可以 跟2一样
6. (简答题)有以下程序结构,请分析所有成员在各类的范围内的访问权限。
class A
{public:
void f1( );
protected:
void f2( );
private:
int i;
};
class B: public A
{public:
void f3( );
int k;
private:
int m;
};
class C: protected B
{public:
void f4( );
protected:
int m;
private:
int n;
};
class D: private C
{public:
void f5( );
protected:
int p;
private:
int q;
};
int main()
{A a1;
B b1;
C c1;
D d1;
}
A类:
在main函数中用a1能调用A类成员函数f1()在A中成员函数中可以调用i,f1(),f2().
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
7. (简答题)有以下程序,请完成下面工作:
① 阅读程序,写出运行时输出的结果。
② 然后上机运行,验证结果是否正确。
③ 分析程序执行过程,尤其是调用构造函数的过程。
#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;
int 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(){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;
}
8. (简答题)有以下程序,请完成下面工作:
① 阅读程序,写出运行时输出的结果。
② 然后上机运行,验证结果是否正确。
③ 分析程序执行过程,尤其是调用构造函数和析构函数的过程。
#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;
}
9. (简答题)分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
① 在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
② 在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务), 在Teacher_Cadre类中还包含数据成员wages(工资)。
③ 对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④ 在类体中声明成员函数,在类外定义成员函数。
⑤ 在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
#include<iostream>
using namespace std;
class Teacher {
public:
Teacher(char* p, int a, char s, char* ad, int pn, char* t);
void display();
private:
char name[10];
int age;
char sex;
char address[20];
int phonenum;
char title[10];
};
class Cadre {
public:
Cadre(char* p, int a, char s, char* ad, int pn, char* pos);
char* get_post();
private:
char name[10];
int age;
char sex;
char address[20];
int phonenum;
char post[10];
};
class Teacher_Cadre : public Teacher, public Cadre {
public:
Teacher_Cadre(char* p, int a, char s, char* ad, int pn, char* t, char* pos, int w) :Teacher(p, a, s, ad, pn, t), Cadre(p, a, s, ad, pn, pos)
{
wages = w;
}
void display_all();
private:
int wages;
};
Teacher::Teacher(char* p, int a, char s, char* ad, int pn, char* t)
{
strcpy_s(name, p);
age = a;
sex = s;
strcpy_s(address, ad);
phonenum = pn;
strcpy_s(title, t);
}
Cadre::Cadre(char* p, int a, char s, char* ad, int pn, char* pos)
{
strcpy_s(name, p);
age = a;
sex = s;
strcpy_s(address, ad);
phonenum = pn;
strcpy_s(post, pos);
}
char* Cadre::get_post()
{
return post;
}
void Teacher::display()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << sex << endl;
cout << "地址:" << address << endl;
cout << "手机号码:" << phonenum << endl;
cout << "职称:" << title << endl;
}
void Teacher_Cadre::display_all()
{
Teacher::display();
cout << "职务:" << get_post() << endl;
cout << "薪水:" << wages << endl;
}
int main()
{
char name[10] = "小明";
char address[20] = "西安工业大学";
char title[10] = "老师";
char post[10] = "校长";
Teacher_Cadre tc(name, 21, 'm', address, 110120119, title, post, 666666);
tc.display_all();
return 0;
}
10. (简答题) 将本章5.8节中的程序片段加以补充完善,成为一个完整的程序。在程序中使用继承和组合。在定义Professor类对象prof1时给出所有数据的初值,然后修改prof1的生日数据,最后输出prof1的全部最新数据。
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
Teacher(){}
Teacher(int n, string nam, char s)
{
num = n;
name = nam;
sex = s;
}
void display()
{
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
}
protected:
int num;
string name;
char sex;
};
class BirthDate
{
public:
BirthDate(){}
BirthDate(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
void display1() {
cout << "birthdate: " << year << " " << month << " " << day << endl;
}
protected:
int year;
int month;
int day;
};
class Professor : public Teacher
{
public:
Professor(int n, string nam, char s, BirthDate bd) {
num = n; name = nam; sex = s; birthday = bd;
}
void show() {
display();
birthday.display1();
}
BirthDate birthday;
};
int main()
{
BirthDate bd(2001, 5, 25);
Professor prof1(101, "xzh", 'm', bd);
BirthDate nbd(2002, 5, 15);
prof1.birthday = nbd;
prof1.show();
return 0;
}