user2954467
user2954467

Reputation: 95

C++ Inheritance not taking an enum

I'm trying to figure out basic inheritance. But I seem unable to get the enum to take. It just responds with a smiley face and nothing.

I honestly don't know what I'm doing wrong so I'm afraid I'll have to just throw all my code at you.

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

Upvotes: 0

Views: 82

Answers (1)

thisisdog
thisisdog

Reputation: 948

You are printing out the enum instead of the actual string. You should use the enum to index into DISCIPLINE_STRINGS.

When you set the type string do this: pType = DISCIPLINE_STRINGS[type]

Upvotes: 1

Related Questions