Lab#7
Lab#7
Lab#7
Note Identify the relationship between classes
1. Create a class Employee that has a field for storing the complete name of employee (first and
last name), a field for storing the IDentification number of employee, and another field for
storing his salary.
Provide
a) a no-argument constructor for initializing the fields to default values
b) a 3-argument constructor for initializing the fields to values sent from outside. Use strcpy
function to copy one string into another.
c) a setter function (mutator) that sets the values of these fields by getting input from user.
d) an accessor function to display the values of the fields.
Derive three classes from this employee class: Manager, Scientist, and Laborer. Manager class has an
additional data member of # of subordinates. Scientist class contains additional information about #
of publications. Laborer class is just similar to the employee class. It has no additional capabilities.
Derive a class foreman from the Laborer class that has an additional data member for storing the
percentage of quotas met by a foreman. Provide appropriate no-argument and n-argument
constructors for all the classes. Provide the overridden getter and setter functions here too to input
and output all the fields. Determine whether public, private, or protected inheritance should be used.
2. Write a base class computer that has attributes of word size (in bits(int)), memory
size(megabytes (double)) and storage size(in megabytes(double))and a speed in (mega
hurts(int). It has a default constructor to initialize. Use a parametrized constructor to
initialize input and a member function to display all data members. Create second
class a laptop which is a kind of computer but also specifies object’s length, width,
height and weight (all in integers). Use a parametrized constructor to initialize
attributes and a member function to display all data members.
Instructor: HM Zahid
3. Create a class Time which has two data members hour(int), min(int).
Instructor: HM Zahid
system("pause");
}
Instructor: HM Zahid
Target: Composition, Relationships in Composition
Composition: In real-life, complex objects are often built from smaller, simpler objects. For
example, a car is built using a metal frame, an engine, some tires, a transmission, a steering
wheel, and a large number of other parts. A personal computer is built from a CPU, a
motherboard, some memory, etc. Even you are built from smaller parts: you have a head, a
body, some legs, arms, and so on. This process of building complex objects from simpler ones is
called object composition.
To qualify as a composition, an object and a part must have the following relationship:
The part (member) is part of the object (class)
The part (member) can only belong to one object (class) at a time
The part (member) has its existence managed by the object (class)
The part (member) does not know about the existence of the object (class)
A good real-life example of a composition is the relationship between a person’s body and a
heart. Let’s examine these in more detail.
Composition relationships are part-whole relationships where the part must constitute part of the
whole object. For example, a heart is a part of a person’s body. The part in a composition can
only be part of one object at a time. A heart that is part of one person’s body can not be part of
someone else’s body at the same time.
In a composition relationship, the object is responsible for the existence of the parts. Most often,
this means the part is created when the object is created, and destroyed when the object is
destroyed. But more broadly, it means the object manages the part’s lifetime in such a way that
the user of the object does not need to get involved. For example, when a body is created, the
heart is created too. When a person’s body is destroyed, their heart is destroyed too. Because of
this, composition is sometimes called a “death relationship”.
And finally, the part doesn’t know about the existence of the whole. Your heart operates
blissfully unaware that it is part of a larger structure. We call this a unidirectional relationship,
because the body knows about the heart, but not the other way around.
Note that composition has nothing to say about the transferability of parts. A heart can be
transplanted from one body to another. However, even after being transplanted, it still meets the
requirements for a composition (the heart is now owned by the recipient, and can only be part of
the recipient object unless transferred again).
Example:
Instructor: HM Zahid
#include<iostream>
using namespace std;
class Engine
{
public:
int power;
};
class Car
{
public:
Engine e;
string company;
string color;
void show_details()
{
cout<<“Compnay is: “<<company<<endl;
cout<<“Color is: “<<color<<endl;
cout<<“Engine horse power is: “<<e.power;
}
};
int main()
{
Car c;
c.e.power = 500;
c.company = “hyundai”;
c.color = “black”;
c.show_details();
return 0;
}
Output:
Company is: Hyundai
Color is: black
Engine horse power is: 500
Relationship in Composition:
House can contain multiple rooms there is no independent life for room and any room can not
belong to two different house. If we delete the house room will also be automatically deleted.
Instructor: HM Zahid
#include<iostream.h>
class House;
class Room
{
public:
Room()
{
};
if(NULL != myHse_p)
{
name_p = new char(sizeof(strlen(myName)));
name_p = myName;
}
else
{
cout<<"Oops House itself is not Created Yet ...\n";
}
};
Instructor: HM Zahid
~Room()
{
cout<<"Room:dtor\n";
myHse_p = NULL;
delete (name_p);
};
void disp()
{
cout<< name_p;
cout<<"\n";
}
private:
House * myHse_p;
char * name_p;
};
class House
{
public:
House(char *myName)
{
cout<<"House::ctor\n";
name_p = new char(sizeof(strlen(myName)));;
name_p = myName;
Room::initList_v(roomsList_p);
Room* myRoom;
Room::createRoom_v(myRoom, this, "Kitchen");
roomsList_p[0] = myRoom;
~House()
Instructor: HM Zahid
{
cout<<"House:dtor\n";
unsigned int i;
}
delete [] roomsList_p;
delete (name_p);
}
void disp()
{
cout<<"\n\nName of the House :"<<name_p;
if(roomsList_p != NULL)
{
unsigned int i;
cout<<"\n\nRooms details...\n";
for(i=0; i<3; ++i)
{
if(NULL != roomsList_p[i])
{
roomsList_p[i]->disp();
}
}
cout<<"\n\n";
}
}
private:
char* name_p;
Room* roomsList_p[3];
};
int main()
{
cout<<"Here House itself creates the Rooms and Deletes as well, before it gets deletd...\n";
return(0);
}
Output:
Example of Composition Relationship
-----------------------------------------
House::ctor
Room::ctor
Room::ctor
Room::ctor
House details...
Rooms details...
Kitchen
BedRoom
Drwaing Room
Here House itself creates the Rooms and Deletes as well, before it gets deletd...
House:dtor
Delete all the Rooms ...
Room:dtor
Room:dtor
Room:dtor
Instructor: HM Zahid