Lab2 - Inheritance and Composition
Lab2 - Inheritance and Composition
1. Inheritance enables you to define a class (considered as a base class) and later extend it to more
specialized classes (i.e., derived classes).
2. We also refer to a base class as a parent class or a superclass and to a derived class as a child class or a
subclass. A derived class inherits accessible data fields and functions from its base class and may also
add new data fields and functions.
3. Note the following points about inheritance:
Private data fields in a base class are not accessible outside the class. Therefore, they cannot be used
directly in a derived class. They can, however, be accessed/mutated through public accessors/mutators
functions if defined in the base class.
Not all is-a relationships should be modeled using inheritance. For class A to extend class B, A should
contain more detailed information than B. Inheritance is used to model the is-a relationship. Do not
blindly extend a class just for the sake of reusing functions.
4. The constructor of a derived class first calls its base class’s constructor before it executes its own code.
The destructor of a derived class executes its own code then automatically calls its base class’s
destructor.
Exercises:
1. Answer the questions for the following:
class A
{
int a;
protected:
int s;
public:
void input(int);
void out();
};
class B :private A
{
int b;
protected:
int u;
public:
void indata(int, int);
void outdata();
};
class C : public B
{
int m;
public:
void display();
};
1
1. Name the base class and derived class of class B:
Base class:A
Derived class:C
……………………………………………………………………………………………………..
2. Name the data members that can be accessed from display() function:
m,u
……………………………………………………………………………………………………..
3. Name the member function(s) , which can be accessed from the objects of class C:
Display(),indata(int,int),outdata()
……………………………………………………………………………………………………..
4. Is the member function out() accessible by the objects of class B?
No……………………………………………………………………………………………………..
class Branch
{
char region[20];
protected:
int employees;
public:
Branch();
void haveIt();
void giveIt();
};
2
2. Write the members accessible by Branch objects:
All its members are accessible by branch object
3. Write all the member functions accessible by Author member functions;
The area includes the two bases and the surfaces of all sides of the prism.
Write a test program with main() where you must define two Prism objects initialized by two of the
constructors of the class.
Write your code of all files below as well as the result obtained. (screen shots allowed)
3
4
5
………………………………………………………………………………………………………………
Write a class Polygon that has n sides of the same length, and all its angles have the same degree (i.e., the
polygon is both equilateral and equiangular). Class Polygon contains the following:
- A private int data field named n that defines the number of sides in the polygon.
- A private double data field named side that stores the length of the side.
- A private double data field named x that defines the x-coordinate of the center of the polygon.
- A private double data field named y that defines the y-coordinate of the center of the polygon.
- A no-arg constructor that creates a regular polygon with n= 3, side =1, x= 0, and y = 0.
- A constructor that creates a regular polygon with the specified number of sides and length of side, and
centered at (0, 0).
- A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-
coordinates
- The constant function getPerimeter() that returns the perimeter of the polygon.
- The constant function getArea() that returns the area of the polygon. The formula for computing the area of a
regular polygon is
Write your code of all files below as well as the result obtained.
4. Write a class Prism again using composition. This time Prism does not inherit class Polygon.
Instead, class Prism has two private data fields: Polygon p and double height.
Write a constructor Prism with the argument list of Polygon constructor argument list and double
height.
Write the functions getArea() and getVolume() that are constant and will return the area and
the volume of the Prism object.
Write a test generator program that defines one Prism object and calculates its area and volume.
Your code of the files and result obtained place here (screen shots allowed)
6
7
8