<< Chapter < Page Chapter >> Page >

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;

}

.

The company that uses the Person class soon realizes that the class can be used for all kinds of individuals – customers, full-time employees, part-time employees, and suppliers all have names and numbers as well. Now, the company wants to define the Customer class which inherits the members of the Person class.

The class header declaration for a derived class Customer which inherits the characteristics of the Person class is as follows:

class Customer: public Person

{

……// other statements go here

}

The access modifiers and base class names following the colon in a class’s header declaration statement are called the base list. Here, the public inheritance is used since it is most common.

The Customer class contains all the members of Person because it inherits them. In other words, every Customer object has an idNum, lastName and firstName, just as a Person object does. Additionally, you can define the Customer class to include an additional data member, balanceDue, and two more functions: setBalDue() and outputBalDue().

The base class Person and the derived class Customer can be graphically represented as in the figure below.

The arrow in the above class diagram points from the derived class to the base

class.

Base class and derived class

Once you extend a base class, you can access its class member directly through objects instantiated from the derived class.

Example:

int main(){

customer cust;

cust.setField(123, “Richard”, “Leakey”);

cust.outputData( );

return 0;

}

The object cust which belongs to the class Customer can call the member functions setFields() and outputData() that belongs to the base class Person.

Example

#include<iostream.h>

#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;

}

class Customer:public Person

{

private:

double balanceDue;

public:

void setBalDue;

void outputBalDue( );

};

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:

ID#215 Name: Linda Santini

Balance due $147.95

Of course, a Customer object can use its own class’ member functions, setBalDue() and outputBalDue(). Additionally, it can use the Person functions, setFields() and outputData(), as if they were its own.

Class hierarchy

Derived classes themselves can serve as base classes for other derived classes. When you build a series of base classes and derived classes, the chain of inherited classes is known as a class hierarchy.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
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
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