









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










void print_by_ref(const Person& person) { person.print(); } int main() { Person mouse(''Mickey'', ''Disney World'', arts); Teacher einstein(''Albert Einstein'', ''USA'', physics, professor); Student plato(''Plato'', ''Greece'', philosophy); plato.enroll_course(COMP151); print_by_ref(mouse); print_by_ref(einstein); print_by_ref(plato); }
------ Person details ------ Name: Mickey Addr: Disney World Dept: 0 ------ Person details ------ Name: Albert Einstein Addr: USA Dept: 1 ------ Person details ------ Name: Plato Addr: Greece Dept: 2 Oops!
----- Person details ----- Name: Mickey Addr: Disney World Dept: 0 ----- Teacher details ----- Name: Albert Einstein Addr: USA Dept: 1 Rank: Full Professor ----- Student details ----- Name: Plato Addr: Greece Dept: 2 Enrolled in: COMP
Person::print()
print_by_ref(plato)
Student::print()
print_by_ref(einstein)
Teacher::print().
class Person { string name; string address; Department dept; public: virtual void print() const; … }; void Person::print() const { cout << '‘--- Person details ---'' << endl; cout << ''Name: '' << name << endl; cout << ''Addr: '' << address << endl; cout << ''Dept: '' << dept << endl; }
void print_by_ptr(const Person* person) { person->print(); }
void print_by_value(Person person) { person.print(); } /* wrong use */
#include ''people.hpp'' // class Person { ... virtual void print() const; } // class Teacher: public Person { ... virtual void print() const; } // class Student: public Person { ... virtual void print() const; } // class PG Student: public Student { ... virtual void print() const; } void print_by_ref(const Person& person) { person.print(); } void print_by_ptr(const Person* person) { person->print(); } int main() { const int N = 4; Person p(' 'Mickey'', ''Disney World'', arts); Teacher t(''Albert Einstein'', ''USA'', physics, professor); Student s(''Plato'', ''Greece'', philosophy); s.enroll_course(''COMP151''); PG Student g('‘John'', ''HK'', computer sci, ''AI''); g.enroll_course(''COMP527''); Person* x[N]; x[0] = &p; x[1] = &t; x[2] = &s; x[3] = &}; for (int j = 0; j < N; ++j) // by pointer to the base class x[j]->print(); cout << endl; for (int j = 0; j < N; ++j) // by pointer to the base class print_by_ptr(x[j]); cout << endl; for (int j = 0; j < N; ++j) // by reference to the base class print_by_ref(*(x[j])); }
main() { Base base; X x; Y y; Base* A[3]; A[0] = &base; A[1] = &x; A[2]= &y; for (int j = 0; j<3; j++) // Pointers A[j]->Display(); cout << endl; for (int j = 0; j<3; j++) // Passing references Display_by_ref(*(A[j])); cout << endl; for (int j = 0; j<3; j++) // Passing pointers Display_by_pntr(A[j]); } OUTPUT ------ Inside Base::Display Inside Base::Display Inside Base::Display Inside Base::Display Inside Base::Display Inside Base::Display Inside Base::Display Inside Base::Display Inside Base::Display
public: virtual void Display() const; } ; class X: public Base { public: virtual void Display() const; }; class Y: public Base { public: virtual void Display() const; }; void Base::Display() const { cout << ''Inside Base::Display\n''; } void X::Display() const { cout << ''Inside X::Display\n''; } void Y::Display() const { cout << ''Inside Y::Display\n''; } void Display_by_val(Base b) { b.Display(); } void Display_by_ref(const Base& b) { b.Display(); } void Display_by_pntr(Base* b) { b->Display(); }
Output ------ Inside Base::Display Inside X::Display Inside Y::Display Inside Base::Display Inside Base::Display Inside Base::Display Inside Base::Display Inside X::Display Inside Y::Display Inside Base::Display Inside X::Display Inside Y::Display