2013级C++第12周(春)项目——成员的访问属性、多重继承

来源:互联网 发布:信息过滤软件 编辑:程序博客网 时间:2024/06/06 03:04

课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接

第一部分 程序阅读 

1、阅读程序,分析类中成员的访问属性
#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;}
(1)在main函数中,能否用b1.i,b1.j和b1.k引用派生类中的基类A的成员i, j k?
(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?
2、阅读程序,请分析所有成员在各类的范围内的访问权限
#include <iostream>using namespace std;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 n;private:    int p;};class D: private C{public:    void f5();protected:    int q;private:    int r;};int main(){    A a1;    B b1;    C c1;    D d1;    return 0;}

第2部分 实践项目

【项目1 - 长颈鹿类对动物类的继承】理解基类中成员的访问限定符和派生类的继承方式
  请在下面的程序中要求的位置写下注释,声明相应的语句在语法上是否正确,为什么。在第一个程序中给出了示例,其他位置请仿照完成。在上机时,可以编译程序加以验证,阅读错误给出的英文提示,并加以理解。
(1)public继承方式下
#include <iostream>using namespace std;class Animal    //动物类{public:    Animal() {}    void eat(){        cout << "eat\n";    }protected:    void play()    {        cout << "play\n";    }private:    void drink()    {        cout << "drink\n";    }};class Giraffe: public Animal   //长颈鹿类{public:    Giraffe() {}    void StrechNeck()    {        cout << "Strech neck \n";    }private:    void take()    {        eat();        // 正确,公有继承下,基类的公有成员对派生类可见        drink();      // _______________        play();       // _______________    }};int main(){    Giraffe gir;      //定义派生类的对象    gir.eat();        // 正确,公有继承下,基类的公有成员对派生类对象可见    gir.play();       // _______________    gir.drink();      // _______________    gir.take();       // _______________    gir.StrechNeck(); // _______________    Animal ani;    ani.eat();        // _______________    ani.play();       // _______________    ani.drink();      // _______________    ani.take();       //错误,派生类的成员对基类对象(不论访问属性)不可见    ani.StrechNeck(); // _______________    return 0;} 

(2)private继承方式下
#include <iostream>using namespace std;class Animal{public:    Animal() {}    void eat()    {        cout << "eat\n";    }protected:    void play()    {        cout << "play\n";    }private:    void drink()    {        cout << "drink\n";    }};class Giraffe: private Animal{public:    Giraffe() {}    void StrechNeck()    {        cout << "Strech neck \n";    }    void take()    {        eat();     // _______________        drink();   // _______________        play();    // _______________    }};int main(){    Giraffe gir;    gir.eat();    // _______________    gir.play();   // _______________    gir.drink();  // _______________    return 0;}

(3)protected继承方式下
#include <iostream>using namespace std;class Animal{public:    Animal() {}    void eat()    {        cout << "eat\n";    }protected:    void play()    {        cout << "play\n";    }private:    void drink()    {        cout << "drink\n";    }};class Giraffe: protected Animal{public:    Giraffe() {}    void StrechNeck()    {        cout << "Strech neck \n";    }    void take()    {        eat();    // _______________        drink();  // _______________        play();   // _______________    }};int main(){    Giraffe gir;    gir.eat();   // _______________    gir.play();  // _______________    gir.drink(); // _______________    return 0;}

【项目2 - 教师兼干部类】(第11章习题9)分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求: 
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。 
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。 
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。 
(4)在类体中声明成员函数,在类外定义成员函数。 
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。


【项目3 - 摩托车继承自行车和机动车】在下面一段类的定义中,自行车类的虚基类为车辆类,机动车类的虚基类也为车辆类,摩托车类的基类为自行车类和机动车类,类之间均为公有继承,如图所示。

    下载可执行文件链接motorcar.exe.

(1)根据上面各类间关系的描述,补全下面程序段中空缺的代码;
(2)实现程序中声明的成员函数,注意相应操作中的动作发生的条件不能满足时应给出提示。
(3)运行程序,享受开摩托的过程。(可以下载可执行文件motorcar.exe,先运行再编程。不必申请驾照,这个摩托车很安全。)
(4)在报告中,请用自己的话写清楚使用虚基类解决什么问题?
#include <iostream>#include<conio.h>#include <windows.h>using namespace std;enum vehicleStaus {rest, running};  //车辆状态:泊车、行进class vehicle //车辆类{protected:    int maxSpeed;//最大车速    int currentSpeed;//当前速度    int weight;//车重    vehicleStaus status; //rest-泊车状态;running-行进状态public:    vehicle(int maxS, int w); //构造函数,初始时,当前速度总为0且处在停车状态    void start();  //由rest状态到running, 初速为1    void stop(); //由running状态到rest, 当前速度小于5时,才允许停车    void speed_up();  //加速,调用1次,速度加1    void slow_down(); //减速,调用1次,速度减1,速度为0时,停车};class bicycle :_____(1)_________//(1)自行车类的虚基类为车辆类{protected:    double height; //车高public:    bicycle(int maxS=10, int w=50, int h=0.7);   //定义构造函数};class motorcar : ______(2)__________//(2)机动车类的虚基类也为车辆类{protected:    int seatNum; //座位数    int passengerNum; //乘客人数public:    motorcar(int maxS=150, int w=1500, int s=5, int p=1);   //定义构造函数    void addPassenger(int p=1);   //增加搭载的乘客,超员要拒载,有人下车时,p为负数。当然车上乘客至少有1个(司机)。只有车停稳后才能上下客。};class motorcycle: ______(3)_________ //(3)摩托车类的基类为自行车类和机动车类{public:        motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7);//定义构造函数    void show(); //显示摩托车的运行状态};int main( ){    motorcycle m;    bool end=false;    while (!end)    {        cout<<"请操作:1-启动  2-加速  3-减速  4-有人上车  5-有人下车  6-停车 0-结束"<<endl;        char keydown= _getch(); //_getch()返回键盘上读取的字符        switch(keydown)        {        case '1':            cout<<"选中的操作是1-启动\t";            m.start();            break;        case '2':            cout<<"选中的操作是2-加速\t";            m.speed_up();            break;        case '3':            cout<<"选中的操作是3-减速\t";            m.slow_down();            break;        case '4':            cout<<"选中的操作是4-有人上车\t";            m.addPassenger();            break;        case '5':            cout<<"选中的操作是5-有人下车\t";            m.addPassenger(-1);            break;        case '6':            cout<<"选中的操作是6-停车\t";            m.stop();            break;        case '0':            end=true;            break;        }        m.show();        cout<<endl;        Sleep(200);  //要包含头文件<windows.h>    }    return 0;}

【项目4】日期时间类
  定义一个日期类Date,数据成员包括年、月、日,SetDate(int y,int m,int d)和PrintDate()函数分别用于设置日期和显示日期;再定义一个时间类Time,数据成员包括时、分、秒,SetTime(int h,int m,int s)和PrintTime()函数分别用于设置时间和显示时间,在此基础上再定义一个日期时间类TimeDate,充分利用已有的两个类中提供的方法,实现日期和时间的设置和显示,请实现类,下面是用于测试的主函数及参考运行结果。
int main(){    TimeDate dt_a,dt_b(2010,4,16,9,30,0);    cout<<"dt_a: ";    dt_a.PrintDate_Time();    cout<<endl;    cout<<"dt_b: ";    dt_b.PrintDate_Time();    dt_a.SetTime(20,00,00);    dt_a.SetDate(2008,8,7);    cout<<endl;    cout<<"dt_after uptate: ";    dt_a.PrintDate_Time();    return 0;}




================= 迂者 贺利坚 CSDN博客专栏=================|== IT学子成长指导专栏 专栏文章的分类目录(不定期更新) ==||== C++ 课堂在线专栏  贺利坚课程教学链接(分课程年级) ==||== 我写的书——《逆袭大学——传给IT学子的正能量》    ==|===== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =====


0 0
原创粉丝点击