Mohsin Abdullah
22F-3133
Question 1:
#include <iostream>
#include <string>
using namespace std;
class PersonData {
private:
string FirstName;
string LastName;
string Address;
public:
// Accessor functions
string getFirstName() const {
return FirstName;
}
string getLastName() const {
return LastName;
}
string getAddress() const {
return Address;
}
// Mutator functions
void setFirstName(string firstName) {
FirstName = firstName;
}
void setLastName(string lastName) {
LastName = lastName;
}
void setAddress(string address) {
Address = address;
}
};
class CustomerData : public PersonData {
private:
int CustomerNumber;
bool MailingList;
public:
// Accessor functions
int getCustomerNumber() const {
return CustomerNumber;
}
bool getMailingList() const {
return MailingList;
}
// Mutator functions
void setCustomerNumber(int customerNumber) {
CustomerNumber = customerNumber;
}
void setMailingList(bool mailingList) {
MailingList = mailingList;
}
// Input customer data
void InputCustomerData() {
string firstName, lastName, address;
int customerNumber;
bool mailingList;
cout << "Enter first name: ";
cin >> firstName;
setFirstName(firstName);
cout << "Enter last name: ";
cin >> lastName;
setLastName(lastName);
cout << "Enter address: ";
cin.ignore();
getline(cin, address);
setAddress(address);
cout << "Enter customer number: ";
cin >> customerNumber;
setCustomerNumber(customerNumber);
cout << "Do you want to be on the mailing list? (1 for Yes, 0 for No): ";
cin >> mailingList;
setMailingList(mailingList);
}
// Display customer data
void DisplayCustomerData() {
cout << "First Name: " << getFirstName() << endl;
cout << "Last Name: " << getLastName() << endl;
cout << "Address: " << getAddress() << endl;
cout << "Customer Number: " << getCustomerNumber() << endl;
if (getMailingList() == 1)
{
cout << "Mailing List : Yes";
}
else
{
cout << "Mailing List : No";
}
}
};
int main() {
CustomerData customer;
// Input customer data
customer.InputCustomerData();
// Display customer data
cout << "\nCustomer Details:\n";
customer.DisplayCustomerData();
return 0;
}
Question 2:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
string gender;
Person() {
name = "Mohsin";
age = 22;
gender = "Male";
}
};
class Employed : public Person {
public:
string NIC;
Employed() {
name = "Mohsin";
NIC = "35404-0000000000-0";
}
void Employ() {
cout << "Hi, I am Employ from Employed Class" << endl;
}
};
class Unemployed : public Person {
public:
Unemployed() {
cout << "Hi, I am UnEmploy from UnEmployed Class" << endl;
}
};
class BusinessMan : public Employed {
public:
BusinessMan() {}
void display() {
cout << "Name: " << name << endl;
cout << "NIC: " << NIC << endl;
}
};
int main() {
BusinessMan businessman;
businessman.Employ();
businessman.display();
Unemployed unemployed;
// Print members of Unemployed
cout << "Name: " << unemployed.name << endl;
cout << "Age: " << unemployed.age << endl;
cout << "Gender: " << unemployed.gender << endl;
return 0;
Question 3:
#include<iostream>
using namespace std;
class vehicle
{
float speed;
float distance;
public:
vehicle()
{
}
void setspeed(float s)
{
speed = s;
}
float getspeed()
{
return speed;
}
void setdistance(float d)
{
distance = d;
}
float getdistance()
{
return distance;
}
float computeduration()
{
float dur = speed / distance;
return dur;
}
};
class wheelvehicle : public vehicle {
int wheels;
public:
wheelvehicle() :vehicle()
{
}
void setwheels(int w)
{
wheels = w;
}
int getwheels()
{
return wheels;
}
};
class wingwheels :public vehicle {
int wings;
public:
wingwheels() :vehicle()
{
}
void setwing(int w)
{
wings = w;
}
int getwings()
{
return wings;
}
};
class truck :public wheelvehicle
{
float carryinglaod;
public:
truck() :wheelvehicle()
{}
void setlaod(int x)
{
carryinglaod = x;
}
int getload()
{
return carryinglaod;
}
};
int main() {
truck t;
t.setspeed(80.5);
t.setdistance(120.7);
t.setwheels(8);
t.setlaod(5000);
cout << "Truck Details:" << endl;
cout << "Speed: " << t.getspeed() << " km/h" << endl;
cout << "Distance: " << t.getdistance() << " km" << endl;
cout << "Wheels: " << t.getwheels() << endl;
cout << "Carrying Load: " << t.getload() << " kg" << endl;
float duration = t.computeduration();
cout << "Duration: " << duration << " hours" << endl;
return 0;
}
Question 4:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string stdID;
string stdName;
string dept;
public:
Student() : stdID(""), stdName(""), dept("") {}
Student(const string& id, const string& name) : stdID(id), stdName(name), dept("") {}
Student(const string& id, const string& name, const string& department) : stdID(id), stdName(name),
dept(department) {}
Student(const string& name, const string& department) : stdID(""), stdName(name), dept(department) {}
void inputData() {
cout << "Enter Student ID: ";
cin >> this->stdID;
cout << "Enter Student Name: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, this->stdName);
cout << "Enter Department: ";
getline(cin, this->dept);
}
void displayData() const {
cout << "Student ID: " << this->stdID << endl;
cout << "Student Name: " << this->stdName << endl;
cout << "Department: " << this->dept << endl;
}
Student(const Student& other) : stdID(other.stdID), stdName(other.stdName), dept(other.dept) {}
~Student() {
cout << "Destroying Student object: " << stdID << endl;
}
};
int main() {
Student obj1;
Student obj2("12345", "Ali Hassan");
Student obj3("23456", "Sheraz Depp", "Computer Science");
Student obj4("Fahad Abbas", "Computer Science");
cout << "Object 1 Data:" << endl;
obj1.displayData();
cout << endl;
cout << "Object 2 Data:" << endl;
obj2.displayData();
cout << endl;
cout << "Object 3 Data:" << endl;
obj3.displayData();
cout << endl;
cout << "Object 4 Data:" << endl;
obj4.displayData();
cout << endl;
Student obj5 = obj4;
cout << "Object 5 (Copied from Object 4) Data:" << endl;
obj5.displayData();
cout << endl;
Student* obj6 = new Student("11111", "John Doe", "Physics");
Student* obj7 = new Student("22222", "Jane Smith", "Mathematics");
Student* obj8 = new Student("33333", "Alex Johnson", "Chemistry");
cout << "Object 6 (Dynamic) Data:" << endl;
obj6->displayData();
cout << endl;
cout << "Object 7 (Dynamic) Data:" << endl;
obj7->displayData();
cout << endl;
cout << "Object 8 (Dynamic) Data:" << endl;
obj8->displayData();
cout << endl;
cout << "Destroying Dynamic Objects..." << endl;
cout << endl;
delete obj6;
delete obj7;
delete obj8;
return 0;
}