You are viewing a single comment's thread. Return to all comments →
C++, took me a while to figure out those darn IDs... eventually realized I just had to start them at 1 and not 0 lol
const int NUM_MARKS = 6; class Person { public: Person(){ age = 0; name = "None"; } virtual ~Person(){} virtual void getdata(){ cin >> name >> age; } virtual void putdata(){ cout << name << " " << age; } string name; int age; }; class Professor : public Person { public: static int ID; Professor(){ cur_ID = ID++; } ~Professor(){ publications = 0; cur_ID = 0; } void getdata(){ Person::getdata(); cin >> publications; } void putdata(){ Person::putdata(); cout << " " << publications << " " << cur_ID << endl; } private: int publications; int cur_ID; }; int Professor::ID = 1; class Student : public Person { public: static int ID; Student(){ for(int i = 0; i < NUM_MARKS; i++){ marks[i] = 0; } sum = 0; cur_ID = ID++; } ~Student(){ for(int i = 0; i < NUM_MARKS; i++){ marks[i] = 0; } sum = 0; cur_ID = 0; } void getdata(){ Person::getdata(); //cin >> name >> age; for (int i = 0; i < NUM_MARKS; i++) { cin >> marks[i]; sum += marks[i]; } } void putdata(){ Person::putdata(); cout << " " << sum << " " << cur_ID << endl; } private: int marks[NUM_MARKS] ; int sum; int cur_ID; }; int Student::ID = 1;
Seems like cookies are disabled on this browser, please enable them to open this website
Virtual Functions
You are viewing a single comment's thread. Return to all comments →
C++, took me a while to figure out those darn IDs... eventually realized I just had to start them at 1 and not 0 lol