LAB MANUAL
Program 2: A Simple Program using
Inheritance

The first three
lines in the programs are line comments
// Filename : program02.cpp
// Purpose : To explain Inheritance
// Class declaration & definition
The forth line
is preprocessor directive
#include <iostream>
The fifth line
in the program is using statement
using namespace std;
The next
line is namespace declaration
namespace secs
{
We are declaring
our own namespace named as secs. All
our class definition in future will be declared inside this namespace. This
will make a partition of the C++ classes. While all existing standard classes
are inside std namespace, which is
default for C++, our classes will be inside secs
namespace, which will be default for Sobhasaria Engineering College. Any
student, or group of student, if desire, may create his/her own namespace, in
which he/she may keep all his/her work.
Why are we
making partition of classes?
Simple, if
we do not partition, we may not use two different classes of the same name
inside a single namespace. Any two or more students may wish to create their
own classes with the same name. If, all these classes are declared inside a
single namespace, there will be conflict and all will be unusable. Hence, the
global namespace is divided into smaller namespaces to provide isolated room for
individual programmer or group of programmers.
If you
require using some library (class or object) present in a namespace other than
yours, either prefix the name of the namespace like std::cout
or use the using namespace std; line before using library.
A question
may arise here, which one method should I prefer?
This will
depend on your requirement. If you require using a single or a few classes or
object, appending namespace before class or object is better, but, if you are
going to use a lot of classes or objects from some namespace, using statement
is better as it needs to be written only once while namespace needs to be appended
with each usage individually.
However,
there may be some scenario where appending namespace will become the only
choice, which you come to know with experience.
Next few
lines are class definition
class Vehicle {
private:
int wheels;
float weight;
public:
void message(void)
{
cout << "Vehicle
message\n";
}
};
Here, the
first line declares Vehicle as a new class and its definition
begins with opening brace {. The keyword private followed by a column : instructs the compiler that the following
members are private, i.e., these will be visible and accessible only inside the
class itself. Next two lines are variable declarations, which declare a variable wheels of int
type and another weight of type float.
On the next
line, the keyword public followed by a column :
informs the compiler that the member following hereafter will be public in
scope, i.e., they will be visible and accessible from outside the class also.
Next four lines are declaration of a function or method, that implements behavior.
Here, the name of the function is message, (to indicate that this is the
message, which is understood by the objects of this class) which neither
accepts any argument nor returns any value. It just displays a message on the
output device.
The last
line is having a closing brace followed by semicolon };
indicating that the class definition ends here.
Next is the
declaration of another class Car which is declared to be a child class (derived
class) of the class Vehicle. This is inheriting the attributes and behaviors
of the parent class (base class) using public keyword indicating that
all the public members (data members and member functions) of the parent class
will be public in this class also. Private members are not inherited at all.
There is another scope protected, and if some member is declared
protected in parent class, it will be inherited and will remain protected in
the child class also.
If we use the keyword private or protected here instead of
public, all the public and protected members of the parent class will be
converted to the specified scope, i.e., private or protected.
class Car : public Vehicle {
private:
int passenger_load;
public:
void message(void)
{
cout << "Car
message\n";
}
};
In addition
to inheriting the public members of the parent, it is also declaring its own
members, passenger_load as an int data in private scope and a member
function message as taking no argument and returning no value.
What is
this? The member function "message" is already declared in the parent
class "Vehicle" in public scope and hence inherited in this class
from parent. Why are we declaring it again? Will it not conflict with the
existing inherited function?
This is a
feature of C++, which is called function overriding. If a derived class
needs to implement a behavior differently from the one implemented already in
the base class, it may redefine the existing member function in its own way. It
will not conflict with the existing function and will be the default behavior of
the object of the new class. However, if someone needs to use the function from
base class, he/she may do so by prefixing a pair of columns ::
before the name of the function during the call.
However, if
the function is not overridden, the function declared already in the base class
will be automatically available here, as you can see in next class Truck,
which is not overriding the message function and hence, when message is
called with an object of the class Truck, the function declared in the Vehicle
class is executed.
class Truck : public Vehicle {
private:
int passenger_load;
float payload;
public:
int passengers(void)
{
return passenger_load;
}
};
Here, the
class Truck is being declared similarly with the only exception that it
is declaring two private data members and a new public function named as
passengers which takes no argument but return the value of passenger_load. Hence, the class Truck
have two function now, first message inherited from parent class Vehicle
and second passengers declared its own.
The next
class Boat is also inherited from the same parent class vehicle
similarly but it is overriding the message function as well as declaring
its own member function passengers similar to class Truck.
class Boat : public Vehicle {
private:
int passenger_load;
public:
int passengers(void)
{
return passenger_load;
}
void message(void)
{
cout << "Boat
message\n";
}
};
Next is the }; closing brace indication the end of the
namespace secs. Here the declaration
and definition of all the functionality is completed.
Now we are
going to use the functionality defined so far in the main function.
// main program
using namespace secs;
int main()
{
Vehicle
bicycle;
Car nano;
Truck tata;
Boat myboat;
bicycle.message();
nano.message();
tata.message();
myboat.message();
system("pause");
return 0;
}
The first
line here is a line comment.
The second
line is using statement indicating that we are going to use the secs namespace.
In the body
of the main function, we have declared four objects of classes Vehicle, Car,
Truck, and Boat in the first four lines and then we are passing message
to each object (calling message function) followed by a call to system
to pause the execution before returning the control back to the operation
system so that we can view the output of the program.
On running
(executing) the above program, we receive the out as: -
Vehicle
message
Car message
Vehicle message
Boat message
Press any key to continue . . .
The first
line of output is caused by the message of the object bicycle
which is an object of the class Vehicle.
The second
line is due to message of the object car belonging to the class Car.
The line is
from Truck which does not have its own message function hence the
function declared in the parent class Vehicle gets executed to cause the
output similar to bicycle.
The forth
line is by the message of the class Boat.
The fifth
line is caused by the pause command of the Windows or DOS operating system.
I have not
used some function of the derived classes here, but you may use them
similarly if want to do so.
By now, I think
you have got some initial idea of inheritance, function overriding, private and
public scope, namespace, and message passing. Though, you
are not expected to be expert in these concepts by now, yet, you are expected
to be familiar with these concepts. In the next programs, I will assume that
you have understood the concepts explained here and hence, these will be used
without further explanations.
You can
play with C++ by changing various lines in your way and see the effect on the
program.
Home of Learning Center Index of Lab
Manual Previous Program Next Program