燕山大学-面向对象程序设计实验-实验6 派生与继承:多重派生-实验报告

CSDN的各位友友们你们好,今天千泽为大家带来的是
燕山大学-面向对象程序设计实验-实验5 派生与继承:单重派生-实验报告,
接下来让我们一起进入c++的神奇小世界吧,相信看完你也能写出自己的 实验报告!
本系列文章收录在专栏 燕山大学面向对象设计报告中 ,您可以在专栏中找到其他章节
如果对您有帮助的话希望能够得到您的支持和关注,我会持续更新的!

实验六 派生与继承—多重派生

🚩6.1 实验目的

1.理解多重派生的定义;

2.理解多重派生中构造函数与析构函数的调用顺序;

3.理解多重派生中虚拟基类的作用;

🚩6.2 实验内容

🚩6.2.1程序阅读

1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。

class CBase1

{

public:

       CBase1(int a)

              :a(a)

       {

              cout<<"base1 structure..."<<endl;

       }

       ~CBase1()

       {

              cout<<"base1 destructure..."<<endl;

       }

       void print()

       {

              cout<<"a="<<a<<endl;

       }

protected:

       int a;

};

class CBase2

{

public:

       CBase2(int b)

              :b(b)

       {

              cout<<"base2 structure..."<<endl;

       }

       ~CBase2()

       {

              cout<<"base2 destructure..."<<endl;

       }

       void print()

       {

              cout<<"b="<<b<<endl;

       }

protected:

       int b;

};

class CDerive : public CBase1, public CBase2

{

public:

       CDerive()

       {

              cout<<"derive structure..."<<endl;

       }

       ~CDerive()

       {

              cout<<"derive destructure..."<<endl;

       }

       void print()

       {

              CBase1::print();

              CBase2::print();

              b1.print();

              b2.print();

              cout<<"c="<<c<<endl;

       }

private:

       CBase1 b1;

       CBase2 b2;

       int c;

};

void main()

{

       CDerive d;

       d.print();

}

问题一:改正以上程序中存在的错误,并分析该程序的输出结果。

答:(1)没有引用头文件,且未声明命名空间。
(2)CBase1和CBase2类缺少默认的构造函数。
分析:main函数创建CDerive类对象d时,CDerive类继承了CBase1和CBase2类,因此输出了前两行。在CDerive类中又有两个成员对象b1和b2,再次调用CBase1和CBase2的构造函数,故有3、4行。随后,CDerive构造函数中有输出第5行内容。6-10行为CDerive::print()函数中内容,由于在调用时均为赋值,因此变量内容不确定。后5行为析构,与构造过程相反。

2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。

#include "iostream"

class CBase

{

public:

       CBase(int a)

              :a(a)

       {

       }

       int a;

};

class CDerive1 : public CBase

{

public:

       CDerive1(int a)

              :CBase(a)

       {

       }

};

class CDerive2 : public CBase

{

public:

       CDerive2(int a)

              :CBase(a)

       {

       }

};

class CDerive : public CDerive1,public CDerive2

{

public:

       CDerive(int a,int b)

              :CDerive1(a),CDerive2(b)

       {

       }

};

void main()

{

       CDerive d(1,2);

       cout<<d.a<<endl;

}

问题一:在不改变原有程序意图的前提下,分别用三种方法改正以上程序,并使程序正确输出。

答:(1)方法一:使用虚继承

#include <iostream>
 
using namespace std;
 
 
 
class CBase   {
 
  public:
 
      CBase(int a)
 
          :a(a)
 
      {
 
      }
 
      int a;
 
};
 
 
 
class CDerive1 : virtual public CBase{
 
  public:
 
      CDerive1(int a)
 
          :CBase(a)
 
      {
 
      }
 
};
 
 
 
class CDerive2 :virtual public CBase{
 
  public:
 
      CDerive2(int a)
 
          :CBase(a)
 
      {
 
      }
 
};
 
 
 
class CDerive : public CDerive1,public CDerive2{
 
  public:
 
      CDerive(int a,int b)
 
          :CDerive1(a),CDerive2(b),CBase(b)
 
      {
 
      }
 
};
 
 
 
int main(){
 
  CDerive d(1,2);
 
  cout<<d.a<<endl;
 
  return 0;
 
}

2)添加输出函数print()

#include <iostream>
 
using namespace std;
 
 
 
class CBase {
 
    public:
 
        CBase(int a)
 
            :a(a)
 
        {
 
        }
 
        int a;
 
        int print(){
 
            return a;
 
        }
 
};
 
 
 
class CDerive1 : public CBase{
 
    public:
 
        CDerive1(int a)
 
            :CBase(a)
 
        {
 
        }
 
   
 
};
 
 
 
class CDerive2 :public CBase{
 
    public:
 
        CDerive2(int a)
 
            :CBase(a)
 
        {
 
        }
 
 
 
};
 
 
 
class CDerive : public CDerive1,public CDerive2{
 
    public:
 
        CDerive(int a,int b)
 
            :CDerive1(a),CDerive2(b)
 
        {
 
        }
 
        int print(){
 
            return CDerive1::print();
 
        }
 
};
 
 
 
int main(){
 
    CDerive d(1,2);
 
    cout<<d.print()<<endl;
 
    return 0;
 
}

(3)获取变量时限定作用域

#include <iostream>
 
using namespace std;
 
 
 
class CBase {
 
    public:
 
        CBase(int a)
 
            :a(a)
 
        {
 
        }
 
        int a;
 
 
 
};
 
 
 
class CDerive1 : public CBase{
 
    public:
 
        CDerive1(int a)
 
            :CBase(a)
 
        {
 
        }
 
   
 
};
 
 
 
class CDerive2 :public CBase{
 
    public:
 
        CDerive2(int a)
 
            :CBase(a)
 
        {
 
        }
 
 
 
};
 
 
 
class CDerive : public CDerive1,public CDerive2{
 
    public:
 
        CDerive(int a,int b)
 
            :CDerive1(a),CDerive2(b)
 
        {
 
        }
 
        int getA(){
 
            return CDerive1::a;
 
        }
 
};
 
 
 
int main(){
 
    CDerive d(1,2);
 
    cout<<d.getA()<<endl;
 
    return 0;
 
}

🚩6.2.2 程序设计

1.建立普通的基类building,用来存储一座楼房的层数、房间数以及它的总平方数。建立派生类house,继承building,并存储卧室与浴室的数量,另外,建立派生类office,继承building,并存储灭火器与电话的数目。设计一主函数来测试以上类的用法。

答:

#include<iostream>
 
using namespace std;
 
 
 
class building{
 
    protected:
 
        int floor;
 
        int room_num;
 
        int area;
 
    public:
 
        building(int a,int b,int c):floor(a),room_num(b),area(c)
 
        {
 
        }
 
        void buildingInfo(){
 
            cout<<"* 楼层:"<<floor<<endl;
 
            cout<<"* 房间数:"<<room_num<<endl;
 
            cout<<"* 面积:"<<area<<endl;
 
        }
 
};
 
 
 
class house:public building{
 
    private:
 
        int sleep_num;
 
        int wash_num;
 
    public:
 
        house(int a,int b,int c,int d,int e):building(a,b,c),sleep_num(d),wash_num(e)
 
        {
 
        }
 
        void houseInfo(){
 
            cout<<"住宅:"<<endl;
 
            buildingInfo();
 
            cout<<"* 卧室数量:"<<sleep_num<<endl;
 
            cout<<"* 浴室数量:"<<wash_num<<endl;
 
        }
 
};
 
 
 
class office:public building{
 
    private:
 
        int fire_num;
 
        int phone_num;
 
    public:
 
        office(int a,int b,int c,int d,int e):building(a,b,c),fire_num(d),phone_num(e)
 
        {
 
        }
 
        void officeInfo(){
 
            cout<<"办公室:"<<endl;
 
            buildingInfo();
 
            cout<<"* 灭火器数量:"<<fire_num<<endl;
 
            cout<<"* 电话数量:"<<phone_num<<endl;
 
        }
 
};
 
 
 
int main(){
 
    house h(10,5,300,2,1);
 
    office o(5,10,500,20,10);
 
    h.houseInfo();
 
    o.officeInfo();
 
    return 0;
 
}

🚩6.3思考题

1.按照下图的类层次结构编写程序,定义属于score的对象c1以及类teacher的对象t1,分别输入每个数据成员的值后再显示出这些数据。

答:

#include<iostream>
 
#include<cstring>
 
#include<string>
 
using namespace std;
 
 
 
class person{
 
    protected:
 
        string name;
 
        int id;
 
    public:
 
        person(string a,int b):name(a),id(b)
 
        {
 
        }
 
        void personInfo(){
 
            cout<<"* 姓名:"<<name<<endl;
 
            cout<<"* ID:"<<id<<endl;
 
        }
 
};
 
 
 
class teacher:public person{
 
    private:
 
        string degree;
 
        string dep;
 
    public:
 
        teacher(string a,int b,string c,string d):person(a,b),degree(c),dep(d)
 
        {
 
        }
 
        void teacherInfo(){
 
            cout<<"教师信息:"<<endl;
 
            personInfo();
 
            cout<<"* 学历:"<<degree<<endl;
 
            cout<<"* 部门:"<<dep<<endl;
 
        }
 
};
 
 
 
class student:public person{
 
    private:
 
        int old;
 
        int sno;
 
    public:
 
        student(string a,int b,int c,int d):person(a,b),old(c),sno(d)
 
        {
 
        }
 
        void studentInfo(){
 
            cout<<"学生信息:"<<endl;
 
            personInfo();
 
            cout<<"* 年龄:"<<old<<endl;
 
            cout<<"* 学号:"<<sno<<endl;
 
        }
 
};
 
 
 
class stud{
 
    protected:
 
        string addr;
 
        string tel;
 
    public:
 
        stud(string a,string b):addr(a),tel(b)
 
        {
 
        }
 
        void studInfo(){
 
            cout<<"* 住址:"<<addr<<endl;
 
            cout<<"* 电话:"<<tel<<endl;
 
        }
 
};
 
 
 
class score:public student,public stud{
 
    private:
 
        double math;
 
        double eng;
 
    public:
 
        score(string a,int b,int c,int d,string e,string f,double s1,double s2):student(a,b,c,d),stud(e,f),math(s1),eng(s2)
 
        {
 
        }
 
        void scoreInfo(){
 
            studentInfo();
 
            studInfo();
 
            cout<<"* 数学成绩:"<<math<<endl;
 
            cout<<"* 英语成绩:"<<eng<<endl;
 
        }
 
};
 
 
 
int main(){
 
    score a("张三",10086,20,2020123456,"四川省成都市犀安路999号","15866668888",88.8,98.7);
 
    a.scoreInfo();
 
    teacher b("李四",10010,"博士","计算机学院");
 
    b.teacherInfo();
 
    return 0;
 
}

本篇文章就到这里啦,祝你学习进步!

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
燕山大学python机器学习实验是一门针对机器学习算法的实践课程,旨在通过实践让学生掌握Python编程语言以及机器学习算法的应用。该实验燕山大学的教学资源为基础,通过理论学习和实际动手操作相结合的方式,学生可以通过这门实验课程深入了解机器学习的核心概念和常用算法。 在实验中,学生将使用Python编程语言,并应用到机器学习的实际案例中。实验内容可能涵盖数据准备、特征工程、模型训练和评估等方面。学生将学习如何从数据集中提取有用的特征,并选择合适的算法进行训练和测试。通过手动调整参数、交叉验证等方式,学生可以优化模型的性能,并评估模型的泛化能力。此外,学生还可以学习到如何使用常见的机器学习工具和库,如Scikit-Learn、TensorFlow、Keras等。 通过参与燕山大学python机器学习实验,学生可以不仅仅学到理论知识,更重要的是掌握如何将机器学习算法应用到实际问题中。学生将通过实践培养数据分析和解决问题的能力,提升编程技能,同时也为未来的专业发展打下坚实的基础。燕山大学python机器学习实验是培养学生综合能力的重要环节,有助于学生构建科学的思维方式和严谨的工作态度。通过实践,学生将更好地理解机器学习的理论知识,并掌握实际应用的技能,为日后从事相关领域的工作打下良好的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千泽.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值