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

Lab#7

Uploaded by

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

Lab#7

Uploaded by

Ayesha Afzal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIVERSITY of Education, LAHORE

Faculty of Computer Science & IT

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

 A default constructor Time()which is used to initialize value of hour(int), min(int) with


0.
 A Parametrized constructor Time(arguments) which is used to initialize value of hour,
min from main function.
 A member function void setTime(int a, int b); which is used to set value of hour and min.
 Two member function int get_hour(),int get_min(), which is used to get value of hour
and min.
 A member function void disPlay_Time(); which is used to print value of hour
and min.
Create a class date which has three data members month (int), day(int), year(int t),.

 A default constructor date()which is used to initialize value of month (int), day(int),


year(int t),. with 0.
 A Parametrized constructor date(arguments) which is used to initialize value of month
(int), day(int), year(int t), from main function.
 A member function void setDate (int a, int b, int c); which is used to set value of month
(int), day(int), year(int t),
 Three member function int get_Day(),int get_month(), int get_year(), which are used to
get value of month (int), day(int), year(int t).
 A member function void printDate (); which is used to print value of month (int),
day(int), year(int t).
Create a class event which has three data members string eventName, Time eventTime; Date
eventDay;

 A default constructor event ()which is used to initialize value zero.


 A Parametrized constructor Event(int hours , int minutes , int m ,int d , int y, string name
r"); which is used to initialize value of month (int), day(int), year(int t), hour, min,
eventname from main function.
 A member function void setEventData(int hours, int minutes, int m, int d, int y, string
name); which is used to initialize value of month (int), day(int), year(int t), hour, min,
eventname from main function.
 A member function void printEventData(); which is used to print value
Main function
int main()
{
Event Obj, Obj1(7,45,1,8,2020," Bukara Eid");;
Obj.printEventData();
Obj1.printEventData();
Obj.setEventData(7,45,1,8,2020," Bukara Eid");
Obj.printEventData();

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.

Here is respective Model and Code for the above example.

Instructor: HM Zahid
#include<iostream.h>

class House;

class Room
{
public:

Room()
{
};

static void createRoom_v(Room* (&room), House* hse, char* name)


{
room = new Room(hse, name);
}

Room(House* hse, char* myName)


{
cout<<"Room::ctor\n";
myHse_p = hse;

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";
}

static void initList_v(Room *(& roomsList_p)[3])


{
roomsList_p[3] = new Room[3];
}

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;

Room::createRoom_v(myRoom, this, "BedRoom");


roomsList_p[1] = myRoom;

Room::createRoom_v(myRoom, this, "Drwaing Room");


roomsList_p[2] = myRoom;
}

~House()
Instructor: HM Zahid
{
cout<<"House:dtor\n";
unsigned int i;

cout<<"Delete all the Rooms ...\n";


for(i=0; i<3; ++i)
{
if(roomsList_p[i] != NULL)
{
delete (roomsList_p[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<<"\nExample of Composition Relationship\n";


cout<<"-----------------------------------------\n\n";
House hse("Vishranti Nilaya");
Instructor: HM Zahid
cout<<"\n\nHouse details...\n";
hse.disp();

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

Name of the House :Vishranti Nilaya

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

You might also like