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

OOP_Advanced_Cpp_Exercises

The document contains code implementations for various object-oriented programming concepts in C++. It covers Composition, Aggregation, and encapsulation through classes like Client, Transaction, Car, Employee, Job_Detail, Subject, and Student. Each section demonstrates how to manage relationships between objects and the lifecycle of their data.

Uploaded by

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

OOP_Advanced_Cpp_Exercises

The document contains code implementations for various object-oriented programming concepts in C++. It covers Composition, Aggregation, and encapsulation through classes like Client, Transaction, Car, Employee, Job_Detail, Subject, and Student. Each section demonstrates how to manage relationships between objects and the lifecycle of their data.

Uploaded by

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

Question 1

Pak-Wheel requested for a software to keep Client and Transaction details using Composition.
Transaction should be deleted when client is deleted.

Answer:

#include <iostream>
using namespace std;

class Transaction {
private:
string date;
string type;
float amount;

public:
Transaction(string d, string t, float a) : date(d), type(t), amount(a) {}

void showTransaction() const {


cout << "Date: " << date << "\nType: " << type << "\nAmount: " << amount << endl;
}
};

class Client {
private:
string name, contact, email;
Transaction transaction;

public:
Client(string n, string c, string e, string d, string t, float a)
: name(n), contact(c), email(e), transaction(d, t, a) {}

void showClient() {
cout << "Name: " << name << "\nContact: " << contact << "\nEmail: " << email << endl;
transaction.showTransaction();
}
};

Question 2
Implement Aggregation: Car has Client as owner, but client should remain even if car is deleted.
Answer:

#include <iostream>
using namespace std;

class Client {
public:
string name;
Client(string n) : name(n) {}
void showClient() { cout << "Client Name: " << name << endl; }
};

class Car {
private:
string model;
Client* owner;

public:
Car(string m, Client* c) : model(m), owner(c) {}

void showCar() {
cout << "Car Model: " << model << endl;
if (owner)
owner->showClient();
}
};

Question 3
Create Employee and Job_Detail classes using Composition. Job_Detail should be destroyed with
Employee.

Answer:

#include <iostream>
using namespace std;

class Job_Detail {
private:
string title, jobDescription;
float salary;
int id;
public:
Job_Detail(string t, float s, int i, string d) : title(t), salary(s), id(i), jobDescription(d) {}

void showJob() {
cout << "Title: " << title << "\nSalary: " << salary << "\nID: " << id << "\nDescription: " <<
jobDescription << endl;
}
};

class Employee {
private:
string name, fatherName, contact, email;
Job_Detail job;

public:
Employee(string n, string f, string c, string e, string jt, float s, int i, string jd)
: name(n), fatherName(f), contact(c), email(e), job(jt, s, i, jd) {}

void showEmployee() {
cout << "Name: " << name << "\nFather Name: " << fatherName << "\nContact: " << contact
<< "\nEmail: " << email << endl;
job.showJob();
}
};

Question 4
Create Subject and Student classes. Use Aggregation such that if Subject is deleted, Students still
exist.

Answer:

#include <iostream>
using namespace std;

class Subject {
public:
string name;
Subject(string n) : name(n) {}
void showSubject() { cout << "Subject: " << name << endl; }
};

class Student {
private:
string name;
Subject* enrolledSubject;

public:
Student(string n, Subject* s) : name(n), enrolledSubject(s) {}

void showStudent() {
cout << "Student: " << name << endl;
if (enrolledSubject)
enrolledSubject->showSubject();
}
};

Question 5
Client class with private variables Personal Email and Personal Detail. Private function prints full
info.

Answer:

#include <iostream>
using namespace std;

class Client {
private:
string name, clientId, contactAddress;
string personalEmail;
string personalDetail;

void printDetails() {
cout << "Name: " << name << "\nID: " << clientId << "\nContact: " << contactAddress
<< "\nEmail: " << personalEmail << "\nDetail: " << personalDetail << endl;
}

public:
Client(string n, string id, string contact, string email, string detail)
: name(n), clientId(id), contactAddress(contact),
personalEmail(email), personalDetail(detail) {}

void show() {
printDetails();
}
};

You might also like