C ++继承没有枚举

时间:2014-03-14 04:11:26

标签: c++ inheritance enums

我试图找出基本的继承。但我似乎无法得到枚举。它只是笑脸而没有任何反应。

老实说,我不知道自己做错了什么,所以我担心我不得不把所有代码扔给你。

#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;
}

1 个答案:

答案 0 :(得分:1)

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

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