We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C++
- Classes
- Virtual Functions
- Discussions
Virtual Functions
Virtual Functions
Sort by
recency
|
440 Discussions
|
Please Login in order to post a comment
include
include
include
include
include
using namespace std;
class Person { protected: int age; string name; int cur_id;
public: Person(int cur_id) : cur_id(cur_id) {} virtual void getdata() = 0; virtual void putdata() = 0; virtual ~Person() = default; // Virtual destructor for proper cleanup };
class Professor : public Person { private: int publications; static int idCounter;
public: Professor() : Person(++idCounter) {}
};
int Professor::idCounter = 0;
class Student : public Person { private: int marks[6]; static int idCounter;
public: Student() : Person(++idCounter) {}
};
int Student::idCounter = 0; int main(){
}
The below solution is a bit convoluted, but I think it makes the most sense,
Person
probabaly shouldn't be an abstract base class, and by defining its virtual functions they can be explicitly called in the overrides.