[英]C++ Inheritance not taking an enum
我試圖找出基本的繼承。 但是我似乎無法獲得枚舉。 它只是以笑臉回應而已。
老實說,我不知道我在做什么錯,所以恐怕我只需要把我所有的代碼扔給你。
#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[type]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.