Professional Documents
Culture Documents
Lab Objectives:
Reasons for Inheritance
Base and Derived Classes
Derived Class Constructors
Access Control
Overridden Functions
It permits code reusability. Once a base class is written and debugged, it need not be touched again,
but, using inheritance, can nevertheless be adapted to work in different situations.
Reusing existing code saves time and money and increases a program’s reliability.
Page 1 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
Inheritance can also help in the original conceptualization of a programming problem, and in the overall
design of the program.
An important result of reusability is the ease of distributing class libraries. A programmer can use a
class created by another person or company, and, without modifying it, derive other classes from it that
are suited to particular situations.
We’ll examine these features of inheritance in more detail after we’ve seen some specific instances of
inheritance at work.
If we need to display the Dimensions of a specific shape, let’s say a triangle, moreover we need to
calculate the area and display the style (isosceles triangle, right angled triangle etc.) of the triangle as
well. To do this we can add the required code in the class TwoDShape but this will be a very specific
approach and can result into a lengthy coded class TwoDShape if need to do the same for shapes other
than a triangle.
A generalized solution to this would be to apply inheritance i.e. we can create a class Triangle, derived
from class TwoDShape (now the base class), as shown in Example 01.
Page 2 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
In example 01, following the class TwoDShape in the listing is the specification for a new class, Triangle.
The first line of Triangle specifies that it is derived from TwoDShape:
Page 3 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
Page 4 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
Example 02-b:
Page 5 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
Example 03:
This program uses a new constructor in the class Triangle. Here is the parameterized constructor:
Triangle (double a, double b): TwoDShape(a,b) {}
This constructor has an unfamiliar feature: the function name following the colon. This construction causes
the Triangle (double a, double b) constructor to call the TwoDShape(a,b)constructor in the base class. In
main(), when we say
Page 6 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
Class members (which can be data or functions) can always be accessed by functions within their
own class, whether the members are private or public.
But objects of a class defined outside the class can access class members only if the members are
public
This is all we need to know if we don’t use inheritance. With inheritance, however, there is a whole raft of
additional possibilities. The question that concerns us at the moment is, can member functions of the derived
class access members of the base class? In other words, can ShowArea() in Triangle access width and
height in TwoDShape. The answer is that member functions can access members of the base class if
the members are public, or if they are protected. They can’t access private members.
We don’t want to make member data public, since that would allow it to be accessed by any function
anywhere in the program and eliminate the advantages of data hiding. A protected member, on the other
hand, can be accessed by member functions in its own class or in any class derived from its own class.
It can’t be accessed from functions outside these classes, such as main(). This is just what we want.
In example 03 what will happen if we make the member data (width and height) private?
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Access Specifier Accessible from Own Accessible from Accessible from
Class Derived Class Objects Outside Class
Public Yes Yes Yes
Protected Yes Yes No
Private Yes No No
Table 1. Inheritance and Accessibility
Page 7 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
You can use member functions in a derived class that override, that is, have the same name as those in the
base class. You might want to do this so that calls in your program work the same way for objects of both
base and derived classes
Example 04:
Who() in Base
Who() in Derived
How does the compiler know which of the two Who() functions to use? Here’s the rule:
When the same function exists in both the base class and the derived class, the function in the
derived class will be executed. (This is true of objects of the derived class.)
Objects of the base class don’t know anything about the derived class and will always use the base
class functions.
OTHER TERMS: IN SOME LANGUAGES THE BASE CLASS IS CALLED THE SUPERCLASS
AND THE DERIVED CLASS IS CALLED THE SUBCLASS. SOME WRITERS ALSO REFER TO
THE BASE CLASS AS THE PARENT AND THE DERIVED CLASS AS THE CHILD.
Page 8 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
6. ASSIGNMENTS
Lab Assignment 01: Run example 01, attach the output and specify the base and derived classes.
Lab Assignment 02: Run example 02 and specify how the objects of Triangle class are initialized to 0?
Lab Assignment 03: Run example 03 and specify, what, is the function of the following line of code:
Triangle (double a, double b): TwoDShape(a,b)
Lab Assignment 05-b: Map the following class diagram into a C++ program:
Page 9 of 10
March 28, 2016 Lab 08: Inheritance
Student Name: __________________________Roll No: ______________ Section: ______________
Author
#name:string
#email:string
#gender:char
+Author(name:string, email:string,
gender:char)
+Display() : stirng, string, char
Book
-title: string;
-price: float
-ISBN: string
-publisher: string
+Book(name:string, email:string,
gender:char): Author
+GetData(): string, float string,
string
+ShowData() : stirng, float string,
string
1. Inheritance is a way to
a. make general classes into more specific classes. b. pass arguments to objects of classes.
c. add features to existing classes without rewriting them. d. improve data hiding and encapsulation.
2. A “child” class is said to be _________ from a base class.
3. Advantages of inheritance include
a. providing class growth through natural selection. b. facilitating class libraries.
c. avoiding the rewriting of code. d. providing a useful conceptual framework.
4. Write the first line of the specifier for a class Bosworth that is publicly derived from a class Alphonso.
_____________________________________________________________________________________
5. To be accessed from a member function of the derived class, data or functions in the base class must be
public or _________.
Page 10 of 10
UML (UNIFIED MODELING LANGUAGE)
UML is different from the other common programming languages like C++, Java, COBOL etc. UML is a
pictorial language used to make software blue prints.
So UML can be described as a general purpose visual modeling language to visualize, specify, construct
and document software system. Although UML is generally used to model software systems but it is not
limited within this boundary. There are two basic types of UML diagrams.
STRUCTURALDIAGRAMS:
The structural diagrams represent the static aspect of the system. These static aspects represent those parts
of a diagram which forms the main structure and therefore stable.
These static parts are represents by classes, interfaces, objects, components and nodes. The four structural
diagrams are:
Class diagram
Object diagram
Component diagram
Deployment diagram
BEHAVIORAL DIAGRAMS:
Any system can have two aspects, static and dynamic. So a model is considered as complete when both
the aspects are covered fully.
Behavioral diagrams basically capture the dynamic aspect of a system. Dynamic aspect can be further
described as the changing/moving parts of a system.
Page 1 of 3
CLASS DIAGRAM:
Class diagrams are the most common diagrams used in UML. Class diagram consists of classes, interfaces,
associations and collaboration.
Class diagrams basically represent the object oriented view of a system which is static in nature.
Active class is used in a class diagram to represent the concurrency of the system.
Class diagram represents the object orientation of a system. So it is generally used for development
purpose. This is the most widely used diagram at the time of system construction.
HOWTODRAW CLASSDIAGRAM?
Class diagram is basically a graphical representation of the static view of the system and represents different
aspects of the application. So a collection of class diagrams represent the whole system.
The name of the class diagram should be meaningful to describe the aspect of the system.
For each class minimum number of properties should be specified. Because unnecessary
properties will make the diagram complicated.
Use notes when ever required to describe some aspect of the diagram. Because at the end of the
drawing it should be understandable to the developer/coder.
Finally, before making the final version, the diagram should be drawn on plain paper and rework
as many times as possible to make it correct.
The following class diagram has been drawn considering all the points mentioned above:
Page 2 of 3
VISIBILITY
To specify the visibility of a class member (i.e. any attribute or function), these notations must be placed
before the member's name
- Indicates private
+ Indicates public
# Indicates protected
Page 3 of 3