zoukankan      html  css  js  c++  java
  • 2013级C++第12周(春)项目——成员的訪问属性、多重继承

    课程首页在: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
    ";
        }
    protected:
        void play()
        {
            cout << "play
    ";
        }
    private:
        void drink()
        {
            cout << "drink
    ";
        }
    };
    class Giraffe: public Animal   //长颈鹿类
    {
    public:
        Giraffe() {}
        void StrechNeck()
        {
            cout << "Strech neck 
    ";
        }
    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
    ";
        }
    protected:
        void play()
        {
            cout << "play
    ";
        }
    private:
        void drink()
        {
            cout << "drink
    ";
        }
    };
    class Giraffe: private Animal
    {
    public:
        Giraffe() {}
        void StrechNeck()
        {
            cout << "Strech neck 
    ";
        }
        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
    ";
        }
    protected:
        void play()
        {
            cout << "play
    ";
        }
    private:
        void drink()
        {
            cout << "drink
    ";
        }
    };
    class Giraffe: protected Animal
    {
    public:
        Giraffe() {}
        void StrechNeck()
        {
            cout << "Strech neck 
    ";
        }
        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-启动 "; m.start(); break; case '2': cout<<"选中的操作是2-加速 "; m.speed_up(); break; case '3': cout<<"选中的操作是3-减速 "; m.slow_down(); break; case '4': cout<<"选中的操作是4-有人上车 "; m.addPassenger(); break; case '5': cout<<"选中的操作是5-有人下车 "; m.addPassenger(-1); break; case '6': cout<<"选中的操作是6-停车 "; 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菜鸟起飞铺跑道。和学生一起享受快乐和激情的大学 =====


  • 相关阅读:
    623. Add One Row to Tree 将一行添加到树中
    771. Jewels and Stones 珠宝和石头
    216. Combination Sum III 组合总数三
    384. Shuffle an Array 随机播放一个数组
    382. Linked List Random Node 链接列表随机节点
    向github项目push代码后,Jenkins实现其自动构建
    centos下安装Jenkins
    python提取批量文件内的指定内容
    批处理实现:批量为文件添加注释
    python抓取每期双色球中奖号码,用于分析
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6848990.html
Copyright © 2011-2022 走看看