<< 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.

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