gpt4 book ai didi

C++:派生类, "no matching constructor"错误

转载 作者:行者123 更新时间:2023-11-30 03:42:30 32 4
gpt4 key购买 nike

我已经为这项任务工作了一段时间。以下是说明:

You are to design an abstract class called Employee whose members are as given below (make them protected):

Data members: char *name, long int ID

Two constructors: A Default constructor // intitialize data memebrs to the default values and a copy constructor

Methods: setPerson (char *n, long int id) //allows user to set information for each person A function called Print () // should be a virtual function, that prints the data attributes of the class. and a destructor

Also define two classes that derived from class Employee, called Manager and Secretary. Each class should inherit all members from the base class and has its own data members and member functions as well. The Manager should have a data member called degree for his/her undergraduate degree (e.g. diploma, bachelor, master, doctor), the Secretary should have her contract (can be a Boolean value 1/0 for permanent/temporary).

All member functions of derived class should be overrided from their base class.

Write the following main() to test your classes

int main() {
Employee * p = new Manager(“Bruce Lee”, 0234567, “Dr.”);
P.print();
Secretary p2;
p2.setPerson(“Wilma Jones”, 0341256, “permanent”);
delete p;
p = & p2;
p.Print();
return 0;
}

这是我到目前为止所想出的一切,但我很确定它充满了错误,而且我的参数和变量类型都不对。

#include <iostream>
using namespace std;


class Employee{
protected:
char *name;
long int ID;
public:
Employee();
Employee(Employee&);
void setPerson(char * n, long int eID) {
name = n;
ID = eID; };
virtual void Print(){
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl; };
};

class Manager: public Employee {
protected:
char *degree;
public:
void setPerson(char * n, long int eID, char * d){
name = n;
ID = eID;
degree = d;
};
void Print() {
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Degree: " << degree << endl;
};
};

class Secretary: public Employee {
protected:
bool contract;
public:
void setPerson(char * n, long int eID, string c){
name = n;
ID = eID;
if (c == "permanent") contract = true;
else contract = false;
};
void Print(){
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Contract: " << contract << endl;
};
};

int main() {
Employee * P = new Manager("Bruce Lee", 0234567, "Dr.");
P.Print();
Secretary P2;
P2.setPerson("Wilma Jones", 0341256, "permanent");
delete P;
P = & P2;
P.Print();
return 0;
}

我在第 62 行(主要代码的第一行)出现错误:

No matching constructor for initialization of Manager

我试过阅读类似的问题,但它们对我帮助不大。我认为最令人困惑的事情是契约(Contract)是一个 bool 值和 char 参数的使用。任何指导都表示赞赏。

最佳答案

你得到的错误非常简单:你没有任何Manager(或Employee)的构造函数接受一个字符串,整数(? ), 和字符串作为参数。

关于C++:派生类, "no matching constructor"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778291/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com