C++程序设计谭浩强 第十一章(继承与派生)习题答案(部分有改进)

11.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;
    char name[10];
    char sex;
};

class Student1 : public Student
{
public:
    void get_value_1()
    {
        get_value();
        cin >> age >> addr;
    }
    void display_1()
    {
        cout << "age: " << age << endl;          //引用派生类的私有成员 
        cout << "address: " << addr << endl;
    }
private:
    int age;
    char addr[30];
};

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

11.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;
    char name[10];
    char sex;
};

class Student1 : private Student
{
public:
    void get_value_1()
    {
        get_value();
        cin >> age >> addr;
    }
    void display_1()
    {
        display();
        cout << "age: " << age << endl;        //引用派生类的私有成员,正确。
        cout << "address: " << addr << endl;
    }
private:
    int age;
    char addr[30];
};

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

11.3 保护继承。

#include <iostream>
using namespace std;
class Student                        //声明基类
{
public:                             //基类公用成员                
    void get_value();
    void display();
protected:                         //基类保护成员
    int num;
    char name[10];
    char sex;
};

void Student::get_value()
{
    cin >> num >> name >> sex;
}

void Student::display()
{
    cout << "num: " << num << endl;
    cout << "name: " << name << endl;
    cout << "sex: " << sex << endl;
}

class Student1 : protected Student   //声明一个保护派生类
{
public:
    void get_value_1();
    void display1();
private:
    int age;
    char addr[30];
};

void Student1::get_value_1()
{
    get_value();
    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 stud1;                      //stud1是派生类student1类的对象
    stud1.get_value_1();                 //调用派生类对象stud1的公用成员函数
    stud1.display1();                   //调用派生类对象stud1的公用成员函数
    return 0;
}

11.4 公用继承

#include <iostream>
using namespace std;
class Student                        //声明基类
{
public:                             //基类公用成员                
    void get_value();
    void display();
protected:                         //基类保护成员
    int num;
    char name[10];
    char sex;
};

void Student::get_value()
{
    cin >> num >> name >> sex;
}

void Student::display()
{
    cout << "num: " << num << endl;
    cout << "name: " << name << endl;
    cout << "sex: " << sex << endl;
}

class Student1 : public Student              //声明一个公用派生类
{
public:
    void get_value_1();
    void display1();
private:
    int age;
    char addr[30];
};

void Student1::get_value_1()
{
    get_value();
    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 stud1;                      //stud1是派生类student1类的对象
    stud1.get_value_1();                 //调用派生类对象stud1的公用成员函数get_value_1
    stud1.display1();                   //调用派生类对象stud1的公用成员函数display1
    return 0;
}
  • 基类成员在派生类中的的访问属性

11.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引用派生类中的基类A的成员i, j k?
                       只能调用  i
(2)派生类B中的成员能否调用基类A中的成员函数f1和f2?
                       能
(3)派生类B中的成员函数能否引用基类A中的数据成员i, j k?
                       只能引用  i,j
(4)能否在main函数中用c1.i, c1.j, c1.k, c1.m, c1.n, c1.p基类A的成员i, j k、派生类B的成员m, n、以及派生类C的成员p?
                       能调用  c1.i,
(5)能否在main函数中用c1.f1(), c1.f2(), c1.f3()和c1.f4()调用f1, f2, f3, f4成员函数?
                       能调用  c1.f1(),  c1.f3(), c1.f4()
(6)派生类C的成员函数f4能否调用基类A中的成员函数f1, f2和派生类中的成员函数f3?
                       能

完整程序:

#include <iostream>
using namespace std;
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;
}

11.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;
}

结果:

a=0 b=0 c=0
a=1 b=0 c=0
a=1 b=3 c=0
a=1 b=3 c=5

11.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;
}

结果:

constructing A
constructing B
constructing C
destructing C
destructing B
destructing A

11.9 分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求: 
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。 
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。 
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。 
(4)在类体中声明成员函数,在类外定义成员函数。 

(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。

#include<string>
#include <iostream>
using namespace std;
class Teacher
{
public:
    Teacher(string nam, int a, char s, string tit, string ad, string t);
    void display();
protected:
    string name;
    int age;
    char sex;
    string title;
    string addr;
    string tel;
};

Teacher::Teacher(string nam, int a, char s, string tit, string ad, string t) :
    name(nam), age(a), sex(s), title(tit), addr(ad), tel(t) { }
void Teacher::display()
{
    cout << "name:" << name << endl;
    cout << "age" << age << endl;
    cout << "sex:" << sex << endl;
    cout << "title:" << title << endl;
    cout << "address:" << addr << endl;
    cout << "tel:" << tel << endl;
}

class Cadre
{
public:
    Cadre(string nam, int a, char s, string p, string ad, string t);
    void display();
protected:
    string name;
    int age;
    char sex;
    string post;
    string addr;
    string tel;
};

Cadre::Cadre(string nam, int a, char s, string p, string ad, string t) :
    name(nam), age(a), sex(s), post(p), addr(ad), tel(t) {}

void Cadre::display()
{
    cout << "name:" << name << endl;
    cout << "age:" << age << endl;
    cout << "sex:" << sex << endl;
    cout << "post:" << post << endl;
    cout << "address:" << addr << endl;
    cout << "tel:" << tel << endl;
}

class Teacher_Cadre :public Teacher, public Cadre
{
public:
    Teacher_Cadre(string nam, int a, char s, string tit, string p, string ad, string t, float w);
    void show();
private:
    float wage;
};

Teacher_Cadre::Teacher_Cadre(string nam, int a, char s, string t, string p, string ad, string tel, float w) :
    Teacher(nam, a, s, t, ad, tel), Cadre(nam, a, s, p, ad, tel), wage(w) {}
void Teacher_Cadre::show()
{
    Teacher::display();
    cout << "post:" << Cadre::post << endl;
    cout << "wages:" << wage << endl;
}

int main()
{
    Teacher_Cadre te_ca("Wang-li", 50, 'f', "prof.", "president", "135 Beijing Road,Shanghai", "(021)61234567", 1534.5);
    te_ca.show();
    return 0;
}

11.10 在定义professor类对象prof1时给出所有数据的初值,然后修改prof1的生H数据,最后输出prof1的全部最新数据。

#include <iostream>
#include <cstring>
using namespace std;
class Teacher                                //教师类
{
public:
    Teacher(int,const char[], char);               //声明构造函数
    void display();                          //声明输出函数
private:
    int num;
    char name[20];
    char sex;
};

Teacher::Teacher(int n,const char nam[], char s)    //定义构造函数
{
    num = n;
    strcpy(name, nam);
    sex = s;
}

void Teacher::display()                      //定义输出函数
{
    cout << "num:" << num << endl;
    cout << "name:" << name << endl;
    cout << "sex:" << sex << endl;
}

class BirthDate                               //生日类
{
public:
    BirthDate(int, int, int);                   //声明构造函数
    void display();                           //声明输出函数
    void change(int, int, int);                 //声明修改函数
private:
    int year;
    int month;
    int day;
};

BirthDate::BirthDate(int y, int m, int d)       //定义构造函数
{
    year = y;
    month = m;
    day = d;
}

void BirthDate::display()                     //定义输出函数
{
    cout << "birthday:" << month << "/" << day << "/" << year << endl;
}

void BirthDate::change(int y, int m, int d)     //定义修改函数
{
    year = y;
    month = m;
    day = d;
}

class Professor :public Teacher                         //教授类
{
public:
    Professor(int,const char[], char, int, int, int, float);    //声明构造函数
    void display();                                   //声明输出函数
    void change(int, int, int);                         //声明修改函数
private:
    float area;
    BirthDate birthday;                               //定义BirthDate类的对象作为数据成员
};

Professor::Professor(int n,const char nam[20], char s, int y, int m, int d, float a) :
    Teacher(n, nam, s), birthday(y, m, d), area(a) { }          //定义构造函数

void Professor::display()                             //定义输出函数
{
    Teacher::display();
    birthday.display();
    cout << "area:" << area << endl;
}

void Professor::change(int y, int m, int d)             //定义修改函数
{
    birthday.change(y, m, d);
}

int main()
{
    Professor prof1(3012, "Zhang", 'f', 1949, 10, 1, 125.4);   //定义Professor对象prof1
    cout << endl << "original data:" << endl;
    prof1.display();   //调用prof1对象的display函数
    cout << endl << "new data:" << endl;
    prof1.change(1950, 6, 1);  //调用prof1对象的change函数
    prof1.display(); //调用prof1对象的display函数
    return 0;
}

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页