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

Oop Lab 9

oop lab report 9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Oop Lab 9

oop lab report 9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

COMSATS UNIVERSITY OF ISLAMABAD, ISLAMABAD CAMPUS

Object Oriented Program

Lab # 09: Composition / Containership

Name Maryam Khaliq

Registration Number FA22-BEE-115

BEE-3C
Class

Ma’am Asma Jadoon


Instructor’s Name
Lab Assessment

Post Lab Total

In-Lab Data Presentation Data Analysis Writing Style


➢ LAB TASKS

1. Code the example given above with mentioning some message indicating the class
construction and destruction in the constructors and destructors of each class and
check the calling of constructors and destructors.
CODE:

#include<iostream>
#include<string>
using namespace std;
class studentRecord
{
private:
string degree;
public:
studentRecord()
{
cout<<"This is student record constructor."<<endl;
}
~studentRecord()
{
cout<<"This is student record destructor."<<endl;
}
void getdata()
{
cout<<"Enter Degree: ";
cin>>degree;
}
};
class employeeRecord
{
private:
int emp_id;
double salary;
public:
employeeRecord ()
{
cout<<"This is employee record constructor."<<endl;
}
~employeeRecord ()
{
cout<<"This is employee record destructor."<<endl;
}
void getdata()
{
cout<<"Enter Employee ID: ";
cin>>emp_id;
cout<<"Enter Salary: ";
cin>>salary;
}
};
class manager
{
private:
string title;
double dues;
employeeRecord emp;
studentRecord stu;
public:
manager(){
cout<<"This is manager constructor."<<endl;
}
~manager(){
cout<<"This is manager destructor."<<endl;
}void getdata()
{emp.getdata();
cout<<"Enter Title: ";
cin>>title;
cout<<"Enter Dues: ";
cin>>dues;
stu.getdata();
}
};
int main()
{
manager m1;
cout<<"Enter data for manager 1: "<<endl;
m1.getdata();
return 0;
}

OUTPUT:
2. Create an Address class, which contains street#, house#, city and code (all of type
char*). Create another class Person that contains an address of type Address. Give
appropriate get and set functions for both classes. Test class person in main.
CODE:

#include <iostream>
#include<string>
#include<cstring>
using namespace std;

class Address {
char houseno[20];
char street[20];
char city[20];
char code[20];

public:

void getdata() {
cout << "Enter street number:" ;
cin >> street;
cout << "Enter house number:" ;
cin >> houseno;
cout << "Enter city name:" ;
cin >> city;
cout << "Enter code:" ;
cin >> code;
}

void putdata() {
cout << "street#" << street << endl;
cout << "house#" << houseno << endl;
cout << "city:" << city << endl;
cout << "code:" << code << endl;
}
};

class Person {
Address a;

public:
void get() {
a.getdata();
}

void put() {
a.putdata();
}
};
int main() {
Person p;
p.get();
p.put();
return 0;
}

OUTPUT:

3. Write the program, which has two classes one, is Date having members (day, month,
year) and the other class is called Employee. The employee has Date class as
member as each employee has Date of joining, Date of Birth etc. a. Determine if an
employee joined the organization within last five years if the current year is 2012. b.
Determine if an Employee has age less than 40 years?

CODE:

#include <iostream>
using namespace std;

class Date {
private:
int day;
int month;
int year;

public:
Date() {
day = month = year = 0;
}

void input() {
cout << "Enter day/month/year: ";
cin >> day >> month >> year;
}

void display() {
cout << day << "/" << month << "/" << year << endl;
}

int getYear() {
return year;
}
};

class Employee {
private:
Date d,d1;

public:
Employee() {
}

void input() {
cout << "Enter date of Birth" << endl;
d.input();
cout << "Enter date of Joining" << endl;
d1.input();
}

void display() {
cout << "Enter date of Birth" << endl;
d.display();
cout << "Enter date of Joining" << endl;
d1.display();
}

int getDoBYear() {
return d.getYear();
}

int getDoJYear() {
return d1.getYear();
}
};

int main() {
Employee emps[5];
int currentYear;
int n = 0, k = 0;

for (int i = 0; i < 2; i++) {


cout << "Employee " << i + 1 << endl;
emps[i].input();
}

cout << "Enter the current year: " << endl;


cin >> currentYear;

for (int i = 0; i < 5; i++) {


if (currentYear - emps[i].getDoJYear() <= 5)
n++;
if (currentYear - emps[i].getDoBYear() <= 40)
k++;
}

cout << "No of employees joined in last 5 years: " << n << endl
<< "No of employees with age less than 40: " << k << endl;

return 0;
}

OUTPUT:

➢ HOME TASKS

• Read digits as Characters input and convert to equivalent numeral values.


CODE:

#include <iostream>
using namespace std;

class CharacterConverter {
public:
void convertAndDisplay() {
char c;
cout << "Enter a character: ";
cin >> c;
cout << "Converted value of " << c << " is " << int(c) <<endl;
}
};

int main() {
CharacterConverter converter;
converter.convertAndDisplay();
return 0;
}

OUTPUT:

➢ CONCLUSION

• The process of creating a complex object out of smaller, simpler components is


known as composition.
• The idea of object composition operates on the has-a relationship concept between
two distinct items.
• When objects from other classes are included in a class's member variables, this is
known as composition.
• Composition in an uncomplicated way by accessing the classes as a member
function in other classes.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy