/* * virtual.cpp * * Created on: 29.05.2013 * Author: trifon */ #include #include using namespace std; #include "student.h" #include "employee.h" #include "intern.h" /* struct SmartPerson { Person* person; enum { PERSON, STUDENT, EMPLOYEE } type; void print() const { if (type == PERSON) person->print(); if (type == STUDENT) ((Student const*)person)->print(); if (type == EMPLOYEE) ((Employee const*)person)->print(); } }; int main1() { Person* pp = NULL; char c; cin >> c; switch (c) { case 's':pp = new Student("Иван", "1", 40000, 5.30);break; case 'e':pp = new Employee("Мария", "2", "разработчик", 1000); break; default: pp = new Person; } if (pp != NULL) { if (c == 's') ((Student*)pp)->print(); if (c == 'e') ((Employee*)pp)->print(); } return 0; } int main2() { SmartPerson sp = {NULL, SmartPerson::PERSON}; char c; cin >> c; switch (c) { case 's': sp.person = new Student("Иван", "1", 40000, 5.30); sp.type = SmartPerson::STUDENT; break; case 'e': sp.person = new Employee("Мария", "2", "разработчик", 1000); sp.type = SmartPerson::EMPLOYEE; break; } if (sp.person != NULL) { sp.print(); } return 0; } */ class Demonstrator : public Employee { }; int main() { Person* pp = NULL; char c; cin >> c; switch (c) { case 's':pp = new Student("Иван", "1", 40000, 5.30);break; case 'e':pp = new Employee("Мария", "2", "разработчик", 1000); break; default: pp = new Person; } //pp->show(); pp->print(); // динамично Person& ap = *pp; ap.print(); // динамично Person p = ap; p.print(); // статично ap.Person::print(); // статично pp->Person::print(); // статично pp = new Demonstrator; pp->print(); pp = new Intern("Иван Иванов", "1", 40001, 6.00, "Демонстратор", 100, 5); pp->print(); cout << sizeof(Person) << endl; cout << typeid(pp).name() << endl; cout << typeid(*pp).name() << endl; cout << typeid(ap).name() << endl; delete pp; Employee* pe = new Intern; delete pe; }