0% found this document useful (0 votes)
17 views

Lab2 - Inheritance and Composition

This document provides code examples and exercises on inheritance and composition in C++. It includes: 1. Code for base classes A and Publishers and derived classes B, C, and Author with questions about accessibility of members. 2. Code for a Prism class that inherits from a Polygon base class, with private height data and constructors. 3. A request to write the Prism and Polygon classes with header and implementation files, including a test program. 4. A final exercise to write the Prism class using composition rather than inheritance, with a Polygon and height property, related functions, and test program.

Uploaded by

Ali Makki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab2 - Inheritance and Composition

This document provides code examples and exercises on inheritance and composition in C++. It includes: 1. Code for base classes A and Publishers and derived classes B, C, and Author with questions about accessibility of members. 2. Code for a Prism class that inherits from a Polygon base class, with private height data and constructors. 3. A request to write the Prism and Polygon classes with header and implementation files, including a test program. 4. A final exercise to write the Prism class using composition rather than inheritance, with a Polygon and height property, related functions, and test program.

Uploaded by

Ali Makki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lebanese University Programming with C++

Faculty of Engineering LAB 2: Inheritance and Composition Semester V

Ali Makki 6478


Theory revision:

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

2. Answer the questions for the following:


class Publishers
{
char p[20];
double turnover;
protected:
void registers();
public:
Publishers();
void enter();
void display();
};

class Branch
{
char region[20];
protected:
int employees;
public:
Branch();
void haveIt();
void giveIt();
};

class Author : private Branch, public Publishers


{
int authorCode;
char authorName[20];
int amount;
public:
Author();
void start();
void display();
};
1. Write the data members accessible by Author objects:

int employees; int authorCode;char authorName[20];int amount;

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;

Functions: void registers();void enter();void display();void haveIt();void giveIt();void start();void


display();

Data members: int employees; int authorCode;char authorName[20];int amount;

3. Provide the inheritance of the following:


Demonstrate your knowledge for header files and implementation files
Write a class Polygon and use it as a public base class.
Create a class Prism that inherits the class Polygon. A new data member is private double height that stores
the height of the prism.
Provide the needed constructors (a default constructor, two constructors with parameter lists) and define the
constant function getArea() that returns the area of the prism.

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

Reference to the class Polygon

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

You might also like