2024 4 2 c++作业,继承

文章讲述了如何在C++中定义和派生类CPerson和CEmployee,包括构造函数、析构函数的设计,以及如何确保派生类的构造函数显式调用基类的构造函数。实例代码展示了如何创建CEmployee对象,观察构造函数和析构函数的调用顺序,并解决了关于构造函数参数和访问修饰符的问题。
摘要由CSDN通过智能技术生成

1.

题目:

  1. 定义名为CPerson的类,该类拥有属性:char *name、char sex和int age;
  2. 为CPerson类设计相应的构造函数、析构函数和成员函数,能通过成员函数设置和获取数据成员;
  3. 由类CPerson派生出子类CEmployee,为该类增加两个新的数据成员,分别用于表示部门(department)和薪水(salary);
  4. 要求派生类CEmployee的构造函数显式调用基类CPerson的构造函数;
  5. 为派生类CEmployee增加需要的成员函数;
  6. 在主程序中定义CEmployee的对象,观察构造函数与析构函数的调用顺序;
  7. 编写完整的程序实现并测试。

问题:1.派生的子类是如何派生的

解决:

// 基类
class Animal {
    // eat() 函数
    // sleep() 函数
};


//派生类
class Dog : public Animal {
    // bark() 函数
};在网上找到的例子,

问题2:.要求派生类CEmployee的构造函数显式调用基类CPerson的构造函数;这个要求是什么意思。

解决:询问ai大致是在CEployee中的构造函数调用CPerson的构造函数

例子:CEmployee(const std::string& n, int a, double s) : CPerson(n, a), salary(s) {
                                                    
问题3:构造函数的格式有点模糊了

解决:

构造函数:MyClass(int n, string str) { num = n; name = str; cout << "对象已创建,num = " << num << ", name = " << name << endl; }

构造函数的调用:MyClass obj1(10, "Object 1");

问题四:CEmployee(const std::string& n, int a, double s) : CPerson(n, a), salary(s) 问题2中的例子是什么意思,

解决:是将参数n传给CPerson的两个成员,将参数s传给CEmployee的成员salary。

问题五:观察构造函数与析构函数的调用顺序,析构函数有什么用。

解决:类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行,析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。摘自菜鸟。

例子:class Line { public: void setLength( double len ); double getLength( void );

Line(); // 这是构造函数声明

~Line(); // 这是析构函数声明 private: double length; };

问题六:const std::string& n这是什么

解决:std::string&n是引用一个字符常量

问题七:如何在子函数中调用构造函数

解决:例子:CEmployee emp("John", 5000); emp.display();

初始版本:

//main
#include <iostream>
#include "CPerson.h"

int main (){
    CEmployee emp("sam","male",19,"computer science ",1000);
    emp.display();
    return 0;

}

//class CPerson.h 
#include <iostream>
#include <string.h>

class CPerson {
    
    char *name;
    char *sex;
    int age;
};

class CEmployee:public CPerson //派生类
{
       char *department;
       int salary;

  public :
     CEmployee(const std::string&n,const std::string&s,int a,const std::string&m,int x):CPerson(n,s,a),department(m),salary(x){};
     

     void display(){
            std::cout<<"the name :"<<name<<std::endl<<"the sex:"<<sex<<std::endl<<"the age:"<<age<<std::endl<<"the department:"<<department<<std::endl<<"the salary:"<<salary<<std::endl;
     };
     ~CEmployee();

};

报错:17    126    C:\c++\2024 3 29\CPerson.h    [Error] no matching function for call to 'CPerson::CPerson(const string&, const string&, int&)'

解决:原因为在原来的class CPerson {
    
    char *name;
    char *sex;
    int age;
};CPerson类中没有设置构造函数应该加上:

public: // 构造函数 CPerson(const std::string& n, const std::string& s, int a) : name(n), sex(s), age(a) {} // 析构函数 ~CPerson() { // 可选:释放资源 } };

问题八:在原来的CPerson类中变量的定义报错

7    14    C:\c++\2024 3 29\main.cpp    [Error] 'std::string CPerson::name' is private

解决:需要在成员前加上protected

在C++中,类的成员(数据成员和函数成员)可以使用publicprotectedprivate三种访问修饰符进行访问控制。

  1. public成员:可以被类的外部访问,也可以被派生类访问。
  2. protected成员:不能被类的外部访问,但可以被派生类访问。通常用于对类的派生类提供对基类成员的访问权限。
  3. private成员:不能被类的外部访问,也不能被派生类访问。通常用于隐藏实现细节,提供封装性。来自ai;

加上即可;

最终版:

#include <iostream>
#include <string.h>

class CPerson {
protected:
    std::string name;
    std::string sex;
    int age;
    
    public:
    // 构造函数
    CPerson(const std::string& n, const std::string& s, int a) : name(n), sex(s), age(a) {}

    // 析构函数
    ~CPerson() {
        // 可选:释放资源
    }
};


class CEmployee:public CPerson //派生类
{
       std::string department;
       int salary;

  public :
     CEmployee(const std::string&n,const std::string&s,int a,const std::string&m,int x):CPerson(n,s,a),department(m),salary(x){
     
     };
     

     void display(){
            std::cout<<"the name :"<<name<<std::endl<<"the sex:"<<sex<<std::endl<<"the age:"<<age<<std::endl<<"the department:"<<department<<std::endl<<"the salary:"<<salary<<std::endl;
     };
     

};

//main
#include <iostream>
#include "CPerson.h"

int main (){
    CEmployee emp("sam","male",19,"computer science ",1000);
    emp.display();
    return 0;

}

舒服了;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值