gpt4 book ai didi

C++继承不采用枚举

转载 作者:行者123 更新时间:2023-11-28 07:01:34 25 4
gpt4 key购买 nike

我正在尝试找出基本的继承。但我似乎无法获得枚举。它只是以笑脸回应,什么也没有。

老实说,我不知道我做错了什么,所以恐怕我不得不把我所有的代码都扔给你。

#include <iostream>
#include <string>

using namespace std;

enum Discipline {
COMPUTER_SCIENCE, Computer_SCIENCE_AND_INNOVATION
};

const string DISCIPLINE_STRINGS[2] = { "Computer Science",
"Computer Science and Innovation",
};

class Person {
public:
Person(){cout << "Person object created using the default Person constructor\n";};
Person(const string& name, Discipline type){pName = name; pType = type;};
~Person(){cout << "Person object destroyed\n";};
string pName;
string pType;
};

class Faculty: public Person {
public:
Faculty();
Faculty(const string& name, Discipline type) {pName = name; pType = type; cout << "Faculty object created using the alternative Faculty constructor\n";};
~Faculty(){cout << "Faculty object destroyed!";};
string getName(){return pName;};
string getDepartment(){return pType;};
};

class Student: public Person {
public:
Student();
Student(const string& name, Discipline type) {pName = name; pType = type; cout << "Student object created using the alternative Student constructor\n";};
~Student(){cout << "Student object destroyed!";};
string getMajor(){return pType;};
string getName(){return pName;};
};



int main()
{
Faculty prof("Name1", COMPUTER_SCIENCE);
Student stu(" Name2", Computer_SCIENCE_AND_INNOVATION);

cout << endl << "I, " << stu.getName() << ", am majoring in " << stu.getMajor() << "." << endl;
cout << "I am taking CSI 240 with Prof. " << prof.getName() << ", who teaches "
<< prof.getDepartment() << " courses." << endl << endl;
system ("pause");
return 0;
}

最佳答案

您正在打印枚举而不是实际的字符串。您应该使用枚举索引到 DISCIPLINE_STRINGS

当您设置类型字符串时,请执行以下操作:pType = DISCIPLINE_STRINGS[类型]

关于C++继承不采用枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22395944/

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