// Indirections and Inheritance.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include using namespace std; class Person { private: string firstname; string lastname; int age; public: Person(string fname, string lname, int a) :firstname(fname), lastname(lname), age(a) { cout << "Constructor Person" << endl; }; virtual ~Person() { cout << "Destructor Person" << endl; }; // shoud be virtual string GetName() const { return firstname + " " + lastname; }; int GetAge() const { return age; }; void SetName(string const& first, string const& last) { firstname = first; lastname = last; }; void SetAge(int const& newage) { age = newage; }; void Say() { cout << "I am a person and my name is " << GetName() << endl; } }; class Customer : public Person { public: Customer(string fname, string lname, int a) : Person(fname, lname, a) { cout << "Constructor Customer" << endl; }; ~Customer() { cout << "Destructor Customer" << endl; }; void Say() { cout << "I am a customer of your Bank and my name is " << GetName() << endl; //try protected }; }; int main() { Customer c1("Shaun", "Black", 26); Person & rP = c1; Person * pP = &c1; Customer & rC = c1; Customer * pC = &c1; //try virtual function rP.Say(); pP->Say(); rC.Say(); pC->Say(); //{ //Person* p = new Customer("Fiona", "Black", 24); // Why there is a memory leak (virtual destructor) //p->Say(); //delete p; //} system("pause"); return 0; }