1.
题目:
- 定义名为CPerson的类,该类拥有属性:char *name、char sex和int age;
- 为CPerson类设计相应的构造函数、析构函数和成员函数,能通过成员函数设置和获取数据成员;
- 由类CPerson派生出子类CEmployee,为该类增加两个新的数据成员,分别用于表示部门(department)和薪水(salary);
- 要求派生类CEmployee的构造函数显式调用基类CPerson的构造函数;
- 为派生类CEmployee增加需要的成员函数;
- 在主程序中定义CEmployee的对象,观察构造函数与析构函数的调用顺序;
- 编写完整的程序实现并测试。
问题: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++中,类的成员(数据成员和函数成员)可以使用public
、protected
和private
三种访问修饰符进行访问控制。
public
成员:可以被类的外部访问,也可以被派生类访问。protected
成员:不能被类的外部访问,但可以被派生类访问。通常用于对类的派生类提供对基类成员的访问权限。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;
}
舒服了;