You are on page 1of 10

Lab 7: Relationship between Objects II: Multiple

Inheritance, Has-A Relationship or Composition,


Shallow and Deep Copy
Student:
(Write your name, registration number and section here.)
Friday, 25 June 2021

Contents
1 Introduction 7

2 Experimental Setup 7

3 Data and Methods 8

4 Results and Analysis 9

5 Conclusion 10

1
Objectives and Deliverables
The objectives of this lab are as follows:
1. To understand and use multiple inheritance or creating Is-A relationship
between different classes.

2. To understand and use composition or creating Has-A relationship be-


tween different classes.
3. To understand the difference between shallow and deep copy.

4. To overload the copy constructor to implement deep copying.


The deliverables or tasks for this lab session are:
• Task 1: Create the class hierarchy given in Fig. 1 by providing imple-
mentation for each class. The attributes and member functions of each
class are given below. Notice that there are not set functions in any class
for changing the attributes. The attributes can only be set using the
overloaded constructors of these classes.

//myclasses.h
#include <cstring>
#ifndef MYCLASSES_H_INCLUDED
#define MYCLASSES_H_INCLUDED
using namespace std;
class person{
private:
string name;
int age;
public:
person(string fullName,int years);
string getName() const;
int getAge() const;
void print() const;
};
class student: public person{
private:
string reg_no;
public:
student(string fullName, int years, string regNo);
string getRegNo() const;
void print() const;
};
class faculty: public person{
private:
string course;
public:
faculty(string fullName, int years, string mycourse);
string getCourse() const;
void print() const;

2
};
class teachingAssistant: public student, public faculty{
public:
teachingAssistant(string fullName, int years, string regNo, string mycourse);
void print() const;
};
#endif // MYCLASSES_H_INCLUDED

In the above classes we use the string class to store the name, course
and regno attributes. In order to be able to use the string class, you
need to include the cstring header file. Instantiate objects of these classes
by writing the following code in the main program.

#include <iostream>
#include <cstring>
#include "myclasses.h"
using namespace std;

int main(){
person p("Yasir Hameed", 20);
student s("Ikhlaq Ahmad",17,"18PWMCT0899");
faculty f("Muhammad Yousaf", 38, "Applied Physics");
teachingAssistant ta("Zaheer ul Islam", 21, "18PWMCT0999","Data Structures");
p.print();
s.print();
f.print();
ta.print();
return 0;
}

Your output should look like Fig. 2. In your report comment on any
errors that you might encounter while implementing this class hierarchy,
and also mention how you were able to remove those errors.
• Task 2: Implement the following three classes, i.e., engine, transmis-
sion and the car class. The car class has a Has-A relationship with the
engine and transmission classes as shown in Fig. 3.

class engine{
private:
int cyls; //no. of cylinders
int disp; //engine displacement
int mod; //model or year
public:
engine(int cylinders, int displacement, int model);
int getDisplacement() const;
int getCylinders() const;
int getModel() const;
void changeModel(int newModel);

3
};
class transmission{
private:
int ngears; //number of gears
bool transType; //true for manual, false for auto
public:
transmission(int gears, bool isManual);
int numberOfGears() const;
bool isManual() const;
};
class car{
private:
int modelYear; //model
engine *e; //pointer to engine object
transmission *t; //pointer to transmission object
public:
car(int model, engine *carEngine, transmission *carTransmission);
~car();
engine* getEngine() const;
transmission* getTransmission() const;
int getModel() const;
};

Once you are done implementing these classes, then in your main pro-
gram, first create objects of classes engine and transmission using the
new operator. Then create a car object c1 using the engine and trans-
mission objects that you already created. Print the displacement and
model of the c1 car object’s engine on the console screen. Create a copy
of the car object c1 by invoking the copy constructor. Let’s name the
copy car object as c2. Access the engine of c2 car object and change its
model to a different year, e.g., 2010. Now, print the model of the c1 car
object’s engine on the console screen once again. You will observe that
the model of the c1 car object’s engine has changed, although you never
changed it. You only changed the model of the c2 car object’s engine.
Can you explain why? How can you fix such problems? Recall our discus-
sion of shallow and deep copy. Apply your proposed fix to this problem
and then run your code once again to see if the problem has been fixed.
For your convenience and guidance, sample output of the required imple-
mentation is given in Fig. 4.

4
Figure 1: Multiple class inheritance

Figure 2: The required output of your program that demonstrates multiple


inheritance as explained in task 1.

5
Figure 3: Composition or Has-A relationship between the car, the engine and
the transmission classes.

Figure 4: The sample output of the program that demonstrates the Has-A
relationship between the car, engine and transmission classes

6
1 Introduction
(Write a concise overview of the topics covered in this lab. Provide an outline of the report in the end.)

2 Experimental Setup
(Briefly provide the details of the hardware and software used for performing the lab.)

7
3 Data and Methods
(Describe of any raw data that you used in your program. Describe your method or procedure or whatever you did in a step by step
manner. Write it in past tense. Describe the features of the program you developed.)

8
4 Results and Analysis
(Describe how did you use your code or program to analyze or process the raw data. What were the results of that processing? Comment
on the appropriateness of your results. Do you need to make any changes in your program to improve your results? Describe the errors
that your compiler flagged? What caused those errors and how did you remove them?)

9
5 Conclusion
(In light of the stated objectives for the lab and your own results, what are your conclusions? Did you achieve the objectives?)

Appendix
(Paste your entire code here. Use 10 point font and single line spacing. You can add additional pages if required.)

10

You might also like