实验5,继承与派生

一、实验目的和要求

1、掌握派生类的定义方法和派生类构造函数的定义方法。

2、掌握在不同继承方式的情况下,基类成员在派生类中的访问权限。

3、掌握在多继承方式的情况下,构造函数与析构函数的调用时机与顺序。

二、实验内容

1、调试下列程序,并在对程序进行修改后再调试,指出调试中的出错原因。

  1. //sy5_1.cpp
  2. #include<iostream>
  3. using namespace std;
  4. class A
  5. {
  6. public:
  7. void seta(int i){a=i;}
  8. int geta(){ return a;}
  9. public:
  10. int a;
  11. };
  12. class B: public A
  13. {
  14. public:
  15. void setb(int i){b=i;}
  16. int getb(){ return b;}
  17. void show(){ cout<< "A::a="<<a<< endl;}
  18. public:
  19. int b;
  20. };
  21. int main()
  22. {
  23. B bb;
  24. bb.seta( 6);
  25. bb.setb( 3);
  26. bb.show();
  27. cout<< "A::a="<<bb.a<< endl;
  28. cout<< "B::b="<<bb.b<< endl;
  29. cout<< "A::a="<<bb.geta()<< endl;
  30. cout<< "B::b="<<bb.getb()<< endl;
  31. return 0;
  32. }

按下列要求对程序进行修改,然后调试,对出现的错误分析其原因。

(1)将派生类B的继承方式改为private时,会出现哪些错误和不正常现象?为什么?

        答:将派生类B的继承方式改成private时,会使得main()函数中的B的对象bb不能调用class A中的seta()和a,因为private继承后,class A中的成员函数和数据成员变成了class B中的private部分。

(2)将派生类B的继承方式改为protected时,会出现哪些错误和不正常现象?为什么?

        答:将派生类B的继承方式改成protected时,会使得main()函数中的B的对象bb不能调用class A中的seta()和a,因为protected继承后,class A中的成员函数和数据成员变成了class B中的protected部分。

(3)将派生类B的继承方式恢复为public后,再将A中数据成员int型变量a的访问权限改为private时,会出现哪些错误和不正常现象?为什么?

        答:基类A的数据成员的访问权限改成private时,会使得除了基类A以外的部分不能调用class A中的数据成员a。

(4)派生类B的继承方式仍为public,将类A中数据成员int型变量a的访问权限改为protected时,会出现哪些错误和不正常现象?为什么?

        答:将基类A的数据成员的访问权限改成protected时,会使得除了基类A和公有继承了A的派生类B以外不能调用class A中的数据成员a。

2、重写教材中的Li4_10.cpp程序,给每个类增加一个析构函数,并使类之间的关系如附图1所示,再写出程序的输出结果。(sy5_2.cpp)

  1. #include<iostream>
  2. using namespace std;
  3. class Base1
  4. {
  5. public:
  6. Base1(){ cout<< "constructing Base1"<< endl;}
  7. ~Base1(){ cout<< "destructing Base1"<< endl;}
  8. };
  9. class Base2
  10. {
  11. public:
  12. Base2(){ cout<< "constructing Base2"<< endl;}
  13. ~Base2(){ cout<< "destructing Base2"<< endl;}
  14. };
  15. class Derived1: public Base2, virtual public Base1
  16. {
  17. public:
  18. Derived1(){ cout<< "constructing Derived1"<< endl;}
  19. ~Derived1(){ cout<< "destructing Derived1"<< endl;}
  20. };
  21. class Derived2: public Base2, virtual public Base1
  22. {
  23. public:
  24. Derived2(){ cout<< "constructing Derived2"<< endl;}
  25. ~Derived2(){ cout<< "destructing Derived2"<< endl;}
  26. };
  27. class Derived3: public Derived1, virtual public Derived2
  28. {
  29. public:
  30. Derived3(){ cout<< "constructing Derived3"<< endl;}
  31. ~Derived3(){ cout<< "destructing Derived3"<< endl;}
  32. };
  33. int main()
  34. {
  35. Derived3 obj;
  36. return 0;
  37. }

运行结果:


3、利用继承性与派生类啦管理学生和教师档案。假设要管理下述几类人员的如下一些数据。

    teacher(教师)类:姓名、性别、年龄、职称、担任课程;

    student(学生)类:姓名、性别、年龄、学号、系别;

    gradstudent(研究生)类:姓名、性别、年龄、学号、系别、导师。

要求每个类只设立构造函数以及显示类对象数据的成员函数。编写主函数,说明有关类对象,并对其类成员函数进行简单实用。(sy5_3.cpp)

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class Person 
  5. {
  6.   public:
  7.   //构造函数 
  8.  Person( string n, string s, int a){name=n,sex=s,age=a;} 
  9.   //显示类的对象数据的成员函数
  10.   void Display(){ cout<< "name:"<<name<< endl<< "sex:"<<sex<< endl<< "age:"<<age<< endl;} 
  11.   private:
  12.   string name;
  13.   string sex;
  14.   int age; 
  15. };
  16. class Student: public Person
  17. {
  18.   public:
  19.   Student( string n, string s, int a, string nu, string d):Person(n,s,a),number(nu),department(d){}
  20.   void Display(){Person::Display(); cout<< "number:"<<number<< endl<< "department:"<<department<< endl<< endl;} 
  21.   private
  22.   string number;
  23.   string department; 
  24. };
  25. class Teacher : public Person
  26. {
  27.   public:
  28.   Teacher( string n, string s, int a, string t, string c):Person(n,s,a),title(t),course(c){}
  29.   void Display(){Person::Display(); cout<< "title:"<<title<< endl<< "course"<<course<< endl<< endl;} 
  30.   private
  31.   string title;
  32.   string course; 
  33. };
  34. class Gradstudent: public Person
  35. {
  36.   public:
  37.   Gradstudent( string n, string s, int a, string nu, string d, string m):Person(n,s,a),number(nu),department(d),master(m){}
  38.   void Display(){Person::Display(); cout<< "number:"<<number<< endl<< "department:"<<department<< endl<< "master:"<<master<< endl<< endl;} 
  39.   private
  40.   string number;
  41.   string department; 
  42.   string master; 
  43. };
  44. int main()
  45. {
  46.  
  47.   Student  s("aa","男",22,"003","xxx");
  48.   Teacher  t("dd","男",50,"xx","xxxxx");
  49.   Gradstudent g("bb","男",24,"001","xxx","dd");
  50.  
  51.  s.Display();
  52.  t.Display();
  53.  g.Display();
  54.   
  55.   return 0;
  56. }

运行结果:


4、试写出所能想到的所有形状(包括二维的和三维的),生成一个形状层次类结构。生成的层次结构以Shape作为基类,并由此派生出TwoDimShape类和ThreeDimShape类。它们的派生类是不同形状类,定义层次结构中的每一个类,并用函数main()进行测试。(sy5_4.cpp)

  1. #include<iostream.h>
  2. const double pai= 3.14;
  3. class Shape
  4. { public: double area()const{ return 0.0;}
  5. void display(){};
  6. };
  7. class twoDimShape: virtual public Shape
  8. {};
  9. class threeDimShape: virtual public Shape
  10. {};
  11. //三角形类
  12. class Triangle: public twoDimShape
  13. { public:Triangle( double myg, double myd){gao=myg;di=myd;} double area ()const { return (di*gao)/ 2;}
  14. void display(){ cout<< "Area of Square is";}
  15. private: double gao,di;
  16. };
  17. //正方形类
  18. class Square: public twoDimShape
  19. { public:Square( double myb){bianchang =myb;}
  20. double area ()const { return bianchang*bianchang;}
  21. void display(){ cout<< "Area of Square is";}
  22. private: double bianchang;
  23. };
  24. //正方体
  25. class Squarer: public threeDimShape
  26. { public:Squarer( double myb){bianchang =myb;}
  27. double area ()const { return bianchang*bianchang*bianchang;} void display(){ cout<< "Superficial area of Cone is";} private: double bianchang;
  28. };
  29. //球
  30. class Ball: public threeDimShape
  31. { public:Ball( double myr){r =myr;}
  32. double area ()const { return ( 4*pai*r*r*r)/ 3;}
  33. void display(){ cout<< "Superficial area of Ball is";} private: double r;
  34. };
  35. int main()
  36. { double a,b,c,d,e;
  37. cout<< "请输入三角形的高和底 "<< endl;
  38. cin>>a>>b;
  39. Triangle t(a,b);
  40. double area;
  41. area=t.area();
  42. t.display();
  43. cout<<area<< endl;
  44. //
  45. cout<< "请输入正方形的边长 "<< endl;
  46. cin>>c;
  47. Square s(c);
  48. area=s.area();
  49. s.display();
  50. cout<<area<< endl;
  51. //
  52. cout<< "请输入正方体的边长 "<< endl;
  53. cin>>d;
  54. Squarer sr(d);
  55. area=sr.area();
  56. sr.display();
  57. cout<<area<< endl;
  58. //
  59. cout<< "请输入球的半径 "<< endl;
  60. cin>>e;
  61. Ball br(e);
  62. area=br.area();
  63. br.display();
  64. cout<<area<< endl;
  65. return 0;
  66. }

运行结果:


三、分析与讨论

1、通过对实验内容中第1题的调试,总结不同继承方式的情况下,基类成员在派生类中的访问权限。

    答:当继承方式是public,protected和private时,基类成员(public部分)可以被派生类调用;而只有当继承方式是public时,main函数中才能用派生类对象调用基类成员;

2、解释实验内容中第2题的运行结果,总结多继承方式的情况下,构造函数与析构函数的调用时机与顺序。虚基类的构造函数与普通基类的构造函数在调用时有什么不同?

    答:析构函数的调用顺序与构造函数的调用顺序刚好相反。虚基类虽然被多次继承,但是其构造函数只会被调用一次。

3、如果希望附图1中的Base1、Base2均有两个,如何修改程序?

      答:把Base 1的virtual去掉。

四、实验小结

        通过实验我们了解了派生类的定义方法和派生类构造函数的定义方法,还知道了在不同继承方式的情况下,基类成员在派生类中的访问权限,以及学习了多继承方式的情况下,构造函数与析构函数的调用时机与顺序。

猜你喜欢

转载自blog.csdn.net/jyf1182813178/article/details/80844341
今日推荐