//--------------------------------------------------------------------------- //Derives class Children from base class Parents //(means produces a lot of parent and child objects, //setting or changing various parameters for some of them) //Adds member function 'facial_features' and 'getchild_no' //By Dr Jan Pajak, at ... on .... //--------------------------------------------------------------------------- #include #include #include #include using namespace std; //------------------------- class Parents { public: Parents(); //default constructor ~Parents(); //default destyructor Parents(int); //parametrised constructor Parents(const Parents&); //copy constructor void Mariage_no(){facial_features++;} int getfacial_features(){return facial_features;} //return facial_features static int spouse_number; //data variable for all objects of the class private: int facial_features; int* ptrInteger; //some heap value used for demo purposes }; //--------------------------------------------------------------------------- class Children:public Parents { public: Children(); //constructor ~Children(); //destructor Children(int); //initial child_no Children (int, int); //initial child_no, facial_features void facial_features(int); //set facial_features int getchild_no(){return child_no;} static int children_counting; private: int child_no; }; //--------------------------------------------------------------------------- Parents::Parents() { //default constructor Parents::spouse_number++; facial_features = 0; ptrInteger = new int; cout<<"\nParents Constructor for parent no.: " < #pragma hdrstop //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char* argv[]) { Parents mother_1; Parents mother_2(20); mother_1.Mariage_no(); Parents mother_3=mother_1;//invokes copy constructor to copy mother_1 to mother_3 foo (mother_1); //invokes copy constructor to copy mother_1 to child_param //pass-by-value Parents* ptrParents = new Parents(); //put a parent on the heap //use ptrParents here ... delete ptrParents; //free heap memory Parents father; Parents father_2(10); //initialise facial_features Children baby; Children baby2(5); //initialize child_no=5 Children baby3(10, 5); //initialise facial_features, child_no=5 baby.facial_features(9); //set baby child_no to 9 //output objects and there data members cout<<"\nfather facial_features "<