1、北邮信通C+上机实验七 第八章8.3 实验题一、基础题1 要求:(1)上机调试以下有语法错误的程序,输出结果如图8-1,对有注释标记的语句进行注释。(2)添加适当的语句,上机验证构造函数和析构函数执行的次序和次数。#include using namespace std;class base int a,b; public: base (int i,int j) a=i; b=j; void move(int x,int y) a+=x; b+=y ; void show() couta=a,b=bendlendl; ;class derived1:public base int x,y; p
2、ublic: derived1 (int i,int j,int k, int l): base (i,j) x=k; y=l; void show() coutx=x,y=yendlendl; void fun() move(3,5); void f1() base:show(); ; void main() base e(1,2); e.show(); derived1 d(3,4,5,6); d.fun(); d. base:show(); d. derived1:show(); d.f1(); 运行结果:图8-1 第1题的输出2上机调试下列有语法错误的程序,然后使其输出如图8-2的结果
3、。#include using namespace std; class base public: void disp() coutbase classendl; ; class derived1:public base public: void disp () coutderived1 classendl; ; class derived2:public base public: void disp () coutderived2 classendl; ; void main() base obj1, *p; derived1 obj2; derived2 obj3; coutendl; o
4、bj1. disp (); obj2. disp (); obj3. disp (); coutdisp (); p=&obj2; p-disp (); p=&obj3; p-disp (); 其运行结果:图8-2 第2题的输出3阅读以下程序,写出程序的运行结果#include using namespace std; class base int a; public: void seta(int x) a=x; void showa() cout a endl; ; class derived1 int b; public: void setb(int x) b=x; void showb(
5、) cout b endl; ; class derived3:public base,public derived1 int c; public: void setc(int x,int y,int z) c=z; seta(x); setb(y); void showc() cout c endl; ; void main() derived3 A; A.seta(10); A.showa(); A.setc(11,22,33);A.showc(); 运行结果: 10 33 4. 上机调试并修改程序中的语法错误,并分析运行结果#include using namespace std ; c
6、lass person / char *name; int age; char add; public: person()coutperson class constructorendl; person()coutperson class destructorendl; ; class student:public person / char *department; / int level; / public: student()coutstudent class constructorendl; student()coutstudent class destructorendl;class
7、 teacher:public person / char *major; float salary; public: teacher()coutteacher class constructorendl; teacher()coutteacher class destructorendl; ;void main() student s1; / teacher t1; / 5分析程序的结果,并总结多重继承的用途#include using namespace std; class base int base_a; public: void set_base_a(int x) base_a=x;
8、 void show_base_a() coutbase_a=base_aendl; ; class derived1 int derived_b; public: void set_derived_b(int x) derived_b=x; void show_derived_b() coutderived_b=derived_bendl; ; class derived_base_d1:public base,derived1 int derived_c; public: void set_derived_c(int x,int y) derived_c=x; set_derived_b(
9、y); void show_derived_c() show_base_a(); show_derived_b(); coutderived_c=derived_cendl; ; void main() derived_base_d1 obj; obj.set_base_a(13); obj.set_derived_c(16,30); obj.show_derived_c(); obj.show_base_a(); 二、编程题1编写一个程序,定义一个汽车类(Cer),它具有一个需要传递参数的构造函数,类中的数据成员有:车型model、座位seat和车重weight,并放在保护段中;由父类私有派
10、生出两个类,分别是JEEP类和bus类。要求每个类都有相关数据的输出。#include using namespace std;class carpublic: void get() coutmodel; coutseat; coutweight; void show() coutcar model:model seat:seat weight:weightendl; protected: char model; int seat; double weight;class jeep:public carpublic: void get() coutmodel; coutseat; coutwe
11、ight; void show() coutcar model:model seat:seat weight:weightendl; ;class bus :public carpublic: void get() coutmodel; coutseat; coutweight; void show() coutcar model:model seat:seat weight:weightendl; ;void main() jeep a; a.get(); a.show(); bus b; b.get(); b.show();2设计一个父亲类father、母亲类mather、子女类child
12、,其主要数据是姓名、子女使用父亲的姓,最后输出子女的姓名和父母的姓名。#include#includestringusing namespace std; class father private : string m; protected:string x; public: void geti() cinxm; void show1() cout父亲的姓名为:xmname; void show2() cout母亲的姓名为:namename; void show3() cout子女的姓名为:xnameendl; ; void main() father f;mother m;child c;
13、cout请分别输入父亲的姓和名:endl; f.geti();cout请输入母亲的名字:endl;m.get1();cout请输入子女的名字:endl;c.get2();f.show1();m.show2();c.show3(); 3编写一个程序,已有若干名学生数据,这些数据包括学号、姓名、语文成绩、英语成绩、和数学成绩,要求设计不同的成员函数求各门课程的平均分。#include #include using namespace std;class studentpublic: int num; char name20; int chinese; int math; int english;
14、int average() int t=(chinese+math+english)/3; return t; ; ; 4建立一个简单的大学管理系统,其中有学生、教师和教辅人员。类的继承关系如图8-3所示。每个类定义一个相对于特定的不同的输出函数print(),输出各类的数据成员。#include#includestringusing namespace std; class uni private: string name; public: uni() name=北京邮电大学; void print() cout学校:nameendl; ; class stu:public uni priv
15、ate:string sname; int xh; public:stu() sname=张三; xh=1; void print1() cout学生姓名:snameendl学号:xhendl; ; class tea:public uni private:string tname; public:tea() tname=李四; void print2() cout教师姓名:tnameendl; ; class ass:public tea private:string aname; public:ass() aname=王五; void print4() cout教辅人员姓名:anameen
16、dl; ; void main() ass a;stu s; a.print(); s.print1(); a.print2(); a.print4();图8-3 编程第4题的继承关系5编一个程序,其中有三个类,分别为中国银行,浦发银行和招商银行,每个银行都包含一个用于存储用户存款的成员,要求用面向对象的编程方法求出用户在三个银行总的存款金额并显示出来。#include#includestringusing namespace std; class zgyh protected: int money1; public: void zgyh1() cinmoney1; ; class pfyh
17、protected:int money2; public:void pfyh1() cinmoney2; ; class zsyh protected:int money3; public:void zsyh1() cinmoney3; ; class z:public zgyh,public pfyh,public zsyh public:void show() cout总存款金额为:money1+money2+money3endl; ; void main() z zje;cout请输入存入中国银行的金额:endl;zje.zgyh1();cout请输入存入浦发银行的金额:endl;zje.pfyh1();cout请输入存入招商银行的金额:endl;zje.zsyh1();cout总金额为:endl;zje.show(); 2.#include #include using namespace std;class fatherpublic: char xing10; char ming15; void father(char a10,char b15) strcpy(xing,a); strcpy(ming,b); ; class child:public father ; class mother ; void main()
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1