<< Chapter < Page Chapter >> Page >

#include<string.h>

class Person

{

private:

int idnum;

char lastName[20];

char firstName[15];

public:

void setFields(int, char[], char[]);

void outputData( );

};

void Person::setFields(int num, char last[], char first[])

{

idnum = num;

strcpy(lastName, last);

strcpy(firstName, first);

}

void Person::outputData( )

{

cout<<“ID#”<<idnum<<“ Name: “<<firstName<<“ “<<lastName<<endl;

}

// derived class

class Employee:public Person

{

private:

int dept;

double hourlyRate;

public:

void setFields(int, char[], char[], int, double);

};

void Employee::setFields(int num, char last[], char first[], int dept, double sal)

{

Person::setFields(num, last, first);

dept = dep;

hourlyRate = sal;

}

int main()

{

Person aPerson;

aPerson.setFields(123, “Kroening”, “Ginny”);

aPerson.outputData();

cout<<endl<<endl;

Employee worker;

worker.Person::setFields(777,”John”, “Smith”);

worket.outputData();

worker.setFields(987,”Lewis”, “Kathy”, 6, 23.55);

worker.outputData();

return 0;

}

The output of the above program:

ID # 123 Name: Ginny Kroening

ID # 777 Name: John Smith

ID # 987 Name: Kathy Lewis

In the above program, when you use the Employee class to instantiate an Employee object with a statement such as Employee worker; and then the statement worker.setFields(); uses the child function with the name setFields(). When used with a child class object, the child class function overrides the parent class version. On the other hand, the statement worker.outputData(); uses the parent class function because no child class function has the name outputData().

Overriding a base class member functions with a derived member function demonstrates the concept of polymorphism. Recall that polymorphism permits the same function name to take many forms.

Constructors and destructors in derived classes

When you derive one class from another class, you can think of any instantiated object of the derived class as having two portions:

  • the base class portion and
  • the derived class portion.

During the instantiating process, the base class portion of the object is instantiated, and then the derived class portion of the object is instantiated.

So, two constructors execute for a single derived class object: the base class constructor and the derived class constructor.

When a derived class object instantiates, constructors begin executing at the top of the class hierarchy. First, the base constructor executes, then any indirect base class’s constructors execute. Finally, the derived class’ constructor executes.

When an object is destroyed, class destructors are executed in the reverse order. First, the derived class’s destructor is called, then the destructors for any indirect base classes, and finally, the destructor for the base class. Figure below illustrates this process using a class hierarchy with four levels.

The order of construction makes sense, since it allows base classes to perform any initialization on class members that may be used by derived classes. And the order of destruction ensures that any base class members required by derived classes are not destroyed until all objects of any derived classes are destroyed first.

Execution of constructors and destructors in a class hierarchy.

Example

#include<iostream.h>

#include<string.h>

class Person

{

private:

int idnum;

char lastName[20];

char firstName[15];

public:

Person();

void setFields(int, char[], char[]);

void outputData( );

};

Person::Person(){

cout<<“Base class constructor call “<<endl;

}

void Person::setFields(int num, char last[], char first[])

{

idnum = num;

strcpy(lastName, last);

strcpy(firstName, first);

}

void Person::outputData( )

{

cout<<“ID#”<<idnum<<“ Name: “<<firstName<<“ “<<lastName<<endl;

}

class Customer:public Person

{

private:

double balanceDue;

public:

Customer();

void setBalDue;

void outputBalDue( );

};

Customer::Customer(){

cout<<“Derived constructor called”<<endl;

}

void Customer::setBalDue(double bal)

{

balanceDue = bal;

}

void Customer::outputBalDue()

{

cout<<“Balance due $”<<balanceDue<<endl;

}

int main()

{

Customer cust;

cust.setFields(215, “Santini”, “Linda”);

cust.outputData();

cust.setBalDue(147.95);

cust.outputBalDue();

return 0;

}

The output of the above program is:

Base class constructor called

Derived class constructor called

ID #215 Name: Linda Santini

Balance due $147.95

The output shows that both the constructor of Person class and the constructor of Customer class involve in creating the object Cust.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask