赞
踩
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接
- #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?
- #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;
- }
- #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;
- }
- #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;
- }
- #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;
- }
【项目3 - 摩托车继承自行车和机动车】在下面一段类的定义中,自行车类的虚基类为车辆类,机动车类的虚基类也为车辆类,摩托车类的基类为自行车类和机动车类,类之间均为公有继承,如图所示。
下载可执行文件链接motorcar.exe.
- #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;
- }
- 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菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 ===== |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。