Assignment 3 Without Output

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

PART # B

#include <iostream>

using namespace std;

class Date {

private:

int day;

int month;

int year;

public:

Date(int d, int m, int y) : day(d), month(m), year(y) {}

bool withinLastFiveYears(int currentYear) {

return (currentYear - year <= 5);

bool ageLessThan40(int currentYear) {

return (currentYear - year < 40);

};

class Employee {
private:

Date dateOfJoining;

Date dateOfBirth;

public:

Employee(int dojDay, int dojMonth, int dojYear, int dobDay, int dobMonth, int dobYear)

: dateOfJoining(dojDay, dojMonth, dojYear), dateOfBirth(dobDay, dobMonth, dobYear) {}

bool joinedWithinLastFiveYears(int currentYear) {

return dateOfJoining.withinLastFiveYears(currentYear);

bool ageLessThan40(int currentYear) {

return dateOfBirth.ageLessThan40(currentYear);

};

class Base {

public:

virtual void iam() {

cout << "Base" << endl;

};

class Derived1 : public Base {

public:

void iam() {

cout << "Derived1" << endl;

};

class Derived2 : public Base {

public:

void iam() {

cout << "Derived2" << endl;


}

};

int main() {

int currentYear = 2012;

Employee employee1(10, 1, 2010, 5, 6, 1980);

Employee employee2(25, 7, 2013, 20, 4, 1975);

cout << "Employee one joined within last five years: "

<< (employee1.joinedWithinLastFiveYears(currentYear) ? "Yes" : "No") << endl;

cout << "Employee one has age less than 40: "

<< (employee1.ageLessThan40(currentYear) ? "Yes" : "No") << endl;

cout << "Employee two joined within last five years: "

<< (employee2.joinedWithinLastFiveYears(currentYear) ? "Yes" : "No") << endl;

cout << "Employee two has age less than 40: "

<< (employee2.ageLessThan40(currentYear) ? "Yes" : "No") << endl;

Base* basePtr;

Base baseObj;

Derived1 derived1Obj;

Derived2 derived2Obj;

basePtr = &baseObj;

basePtr->iam();

basePtr = &derived1Obj;

basePtr->iam();

basePtr = &derived2Obj;

basePtr->iam();

return 0;

Output:
#include <iostream>

using namespace std;

class Shape

protected:

double dim1, dim2;

public:

virtual void get_data() = 0; // Pure virtual function

virtual void display_area() = 0; // Pure virtual function

};

class Triangle : public Shape

public:

void get_data() {
cout << "Enter base and height of triangle: ";

cin >> dim1 >> dim2;

void display_area() {

cout << "Area of Triangle is: " << 0.5 * dim1 * dim2 << endl;

};class Rectangle : public Shape

{ public:

void get_data() {

cout << "Enter length and breadth of rectangle: ";

cin >> dim1 >> dim2;

void display_area() {

cout << "Area of Rectangle is: " << dim1 * dim2 << endl;

};int main() {

Triangle t;

t.get_data();

t.display_area();

Rectangle r;

r.get_data();

r.display_area();

Output:
#include <iostream>

#include <cstring>

using namespace std;

class Address {

private:

char* street;

char* house;

char* city;

char* code;

public:

Address(const char* s, const char* h, const char* c, const char* cd)

: street(nullptr), house(nullptr), city(nullptr), code(nullptr) {

setStreet(s);

setHouse(h);

setCity(c);

setCode(cd);
}

~Address() {

delete[] street;

delete[] house;

delete[] city;

delete[] code;

const char* getStreet() const {

return street;

void setStreet(const char* s) {

delete[] street;

street = new char[strlen(s) + 1];

strcpy(street, s);

const char* getHouse() const {

return house;

void setHouse(const char* h) {

delete[] house;

house = new char[strlen(h) + 1];

strcpy(house, h);

const char* getCity() const {

return city;

void setCity(const char* c) {

delete[] city;
city = new char[strlen(c) + 1];

strcpy(city, c);

const char* getCode() const {

return code;

void setCode(const char* cd) {

delete[] code;

code = new char[strlen(cd) + 1];

strcpy(code, cd);

};

class Person {

private:

Address address;

public:

Person(const Address& a) : address(a) {}

const Address& getAddress() const {

return address;

void setAddress(const Address& a) {

address = a;

};

int main() {

Address address("Comsats", "150", "Bani Gala", "150000");

Person person(address);

cout << "Original Address: " << endl;

cout << "Street: " << person.getAddress().getStreet() << endl;


cout << "House: " << person.getAddress().getHouse() << endl;

cout << "City: " << person.getAddress().getCity() << endl;

cout << "Code: " << person.getAddress().getCode() << endl;

Address newAddress("COMSATS ISLAMABAD", "050", "PARK ROAD", "0000");

person.setAddress(newAddress);

cout << "\nModified Address: " << endl;

cout << "Street: " << person.getAddress().getStreet() << endl;

cout << "House: " << person.getAddress().getHouse() << endl;

cout << "City: " << person.getAddress().getCity() << endl;

cout << "Code: " << person.getAddress().getCode() << endl;

return 0;

Output:

#include <iostream>

#include <string>

using namespace std;

class Teacher {

protected:

string teacherName;

int age;

string address;
public:

Teacher() {

cout << "Teacher constructor called." << endl;

~Teacher() {

cout << "Teacher destructor called." << endl;

void inputTeacher() {

cout << "Enter Teacher's Name: ";

getline(cin, teacherName);

cout << "Enter Teacher's Age: ";

cin >> age;

cin.ignore(); // Ignore the newline character in the input buffer

cout << "Enter Teacher's Address: ";

getline(cin, address);

void displayTeacher() {

cout << "Teacher's Name: " << teacherName << endl;

cout << "Teacher's Age: " << age << endl;

cout << "Teacher's Address: " << address << endl;

};class Author {

protected:

string authorName;

string address;

int numOfBooks;

public:

Author() {

cout << "Author constructor called." << endl; }


~Author() {

cout << "Author destructor called." << endl;

}void inputAuthor() {

cout << "Enter Author's Name: ";

getline(cin, authorName);

cout << "Enter Author's Address: ";

getline(cin, address);

cout << "Enter Number of Books Written: ";

cin >> numOfBooks;

cin.ignore(); // Ignore the newline character in the input buffer

}void displayAuthor() {

cout << "Author's Name: " << authorName << endl;

cout << "Author's Address: " << address << endl;

cout << "Number of Books Written: " << numOfBooks << endl;

};class Scholar : public Teacher, public Author {

public:

Scholar() {

cout << "Scholar constructor called." << endl;

} ~Scholar() {

cout << "Scholar destructor called." << endl;

}void inputScholar() {

inputTeacher();

inputAuthor();

}void displayScholar() {

displayTeacher();

displayAuthor();

};int main() {
Scholar scholar;

cout << "Enter Scholar's Details: " << endl;

scholar.inputScholar();

cout << "\nScholar's Details: " << endl;

scholar.displayScholar(); return 0;

Output:

#include <iostream>

#include <string>

using namespace std;

class Teacher {

protected:

string teacherName;

int age;

string address;

public:

void inputTeacher() {

cout << "Enter Teacher's Name: ";

getline(cin, teacherName);

cout << "Enter Teacher's Age: ";

cin >> age;

cin.ignore(); // Ignore the newline character in the input buffer

cout << "Enter Teacher's Address: ";


getline(cin, address);

void displayTeacher() {

cout << "Teacher's Name: " << teacherName << endl;

cout << "Teacher's Age: " << age << endl;

cout << "Teacher's Address: " << address << endl;

};

class Author {

protected:

string authorName;

string address;

int numOfBooks;

public:

void inputAuthor() {

cout << "Enter Author's Name: ";

getline(cin, authorName);

cout << "Enter Author's Address: ";

getline(cin, address);

cout << "Enter Number of Books Written: ";

cin >> numOfBooks;

cin.ignore(); // Ignore the newline character in the input buffer

} void displayAuthor() {

cout << "Author's Name: " << authorName << endl;

cout << "Author's Address: " << address << endl;

cout << "Number of Books Written: " << numOfBooks << endl;

};
class Scholar : public Teacher, public Author {

public:

void inputScholar() {

inputTeacher();

inputAuthor();

void displayScholar() {

displayTeacher();

displayAuthor();

};int main() {

Scholar scholar;

cout << "Enter Scholar's Details: " << endl;

scholar.inputScholar();

cout << "\nScholar's Details: " << endl;

scholar.displayScholar();

return 0;

Output:
#include <iostream>

#include <string>

using namespace std;

class DateTime {

private:

int day;

int month;

int year;

int hour;

int minute;
int second;public:

DateTime(int d, int m, int y, int h, int min, int sec)

: day(d), month(m), year(y), hour(h), minute(min), second(sec) {}

void setDateTime(int d, int m, int y, int h, int min, int sec) {

day = d;

month = m;

year = y;

hour = h;

minute = min;

second = sec;

} void displayDateTime() {

cout << "Date: " << day << "/" << month << "/" << year << endl;

cout << "Time: " << hour << ":" << minute << ":" << second << endl;

};int main() {

DateTime Watch(1, 1, 2022, 12, 0, 0);

cout << "Initial Watch Settings: " << endl;

Watch.displayDateTime();

int day, month, year, hour, minute, second;

cout << "\nEnter new Watch settings: " << endl;

cout << "Day: ";

cin >> day;

cout << "Month: ";

cin >> month;

cout << "Year: ";

cin >> year;

cout << "Hour: ";

cin >> hour;

cout << "Minute: ";


cin >> minute;

cout << "Second: ";

cin >> second;

Watch.setDateTime(day, month, year, hour, minute, second);

cout << "\nUpdated Watch Settings: " << endl;

Watch.displayDateTime();

return 0;

Output:

#include <iostream>

#include <string>

using namespace std;

class Person {
protected:

string name;

int age;

string gender;

public:

void setName(string name) {

this->name = name;

} void setAge(int age) {

this->age = age;

}void setGender(string gender) {

this->gender = gender;

}string getName() {

return name;

int getAge() { return age;

}string getGender() {

return gender;

};class Employee {

protected:

string employerName;

double dailyWages;public:

void setEmployerName(string employerName) {

this->employerName = employerName;

}void setDailyWages(double dailyWages) {

this->dailyWages = dailyWages;

}string getEmployerName() {

return employerName;
}double getDailyWages() {

return dailyWages;

};class Teacher : public Person, public Employee {

private:

string grade;

public:

void setGrade(string grade) {

this->grade = grade;

string getGrade() { return grade;

};int main() {

Teacher teacher;

// Set data using setter functions

teacher.setName("NOUMAN");

teacher.setAge(20);

teacher.setGender("Male");

teacher.setEmployerName("aps School");

teacher.setDailyWages(99.9);

teacher.setGrade("Grade 2"); // Display data using getter functions

cout << "Name: " << teacher.getName() << endl;

cout << "Age: " << teacher.getAge() << endl;

cout << "Gender: " << teacher.getGender() << endl;

cout << "Employer: " << teacher.getEmployerName() << endl;

cout << "Daily Wages: " << teacher.getDailyWages() << endl;

cout << "Grade: " << teacher.getGrade() << endl;

return 0;
}

Output:

#include <iostream>

using namespace std;

class Base {

protected:

int baseData1;

int baseData2;

public:

void setBaseData(int data1, int data2) {

baseData1 = data1;

baseData2 = data2;

} void displayBaseData() {

cout << "Base Data 1: " << baseData1 << endl;

cout << "Base Data 2: " << baseData2 << endl;

};class Derived1 : public Base {


protected:

int derivedData;

public:

void setDerivedData(int data) {

derivedData = data;

} void displayDerivedData() {

cout << "Derived Data: " << derivedData << endl;

};class Derived2 : public Derived1 {

public:

void accessBaseAndDerived1() {

displayBaseData();

displayDerivedData();

};int main() {

Derived2 obj;

obj.setBaseData(10, 20);

obj.setDerivedData(30);

obj.accessBaseAndDerived1();

return 0;

Output:
#include <iostream>

using namespace std;

class Person {

int id;

string name;

string addr;

public :

void getdata() {

cout << "Enter ID Number : ";

cin >> id;

cout << "\nEnter Name : ";

cin >> name;

cout << "\nEnter Address : ";

getline(cin,addr);

void showdta(){
cout<<"\nID No.: "<<id;

cout<<"\nName: "<<name;

cout<<"\nAddress: "<<addr;

}; // end of person class

class Student :public Person{

long int rno;

float m1,m2,m3;

public :

void getdata() {

Person ::getdata();

cout << "\nEnter Roll no : ";

cin>>rno;

cout<<"Enter Marks in three subjects:";

cin>>m1>>m2>>m3;

void showdata(){

Person::showdata();

cout<<endl<<"RollNo:"<<rno;

cout<<endl<<"Marks obtained are :"

<<"Subject-1="<<m1

<<" Subject -2 ="<<m2

<<" Subject -3 = "<<m3;

};//end of student class

int main()

Student s;

s.getdata();
s.showdata();

return 0;

Output:

#include <iostream>

#include <string>

class publication {

protected:

std::string title;

float price;

public:

// Default constructor

publication() {

title = "";

price = 0.0;

} // Function to get data from the user

void getdata() {

std::cout << "Enter the title: ";

std::getline(std::cin >> std::ws, title);

std::cout << "Enter the price: ";


std::cin >> price;

} // Function to display data

void putdata() const {

std::cout << "Title: " << title << std::endl;

std::cout << "Price: $" << price << std::endl;

};

class book : public publication {

private:

int page_count;

public:

// Default constructor

book() : publication() {

page_count = 0;

} // Function to get data from the user

void getdata() {

publication::getdata();

std::cout << "Enter the page count: ";

std::cin >> page_count;

} // Function to display data

void putdata() const {

publication::putdata();

std::cout << "Page Count: " << page_count << std::endl;

};

class tape : public publication {

private:

float playing_time;

public:

// Default constructor
tape() : publication() {

playing_time = 0.0;

} // Function to get data from the user

void getdata() {

publication::getdata();

std::cout << "Enter the playing time (in minutes): ";

std::cin >> playing_time;

} // Function to display data

void putdata() const {

publication::putdata();

std::cout << "Playing Time: " << playing_time << " minutes" << std::endl;

};int main() {

book b;

tape t;

std::cout << "Enter the details of the book:\n";

b.getdata();

std::cout << "\nEnter the details of the tape:\n"; t.getdata();

std::cout << "\nBook Details:\n";

b.putdata();

std::cout << "\nTape Details:\n";

t.putdata();

return 0;

Output:
#include <iostream>

class Computer {

protected:

int wordsize;

int memorysize;

int storagesize;

int speed;

public:

// Default constructor

Computer() {

wordsize = 0;

memorysize = 0;

storagesize = 0;

speed = 0;

} // Constructor to initialize all components

Computer(int wordsize, int memorysize, int storagesize, int speed) {

this->wordsize = wordsize;
this->memorysize = memorysize;

this->storagesize = storagesize;

this->speed = speed;

}// Function to display data members

void display() {

std::cout << "Word Size: " << wordsize << " bits\n";

std::cout << "Memory Size: " << memorysize << " megabytes\n";

std::cout << "Storage Size: " << storagesize << " megabytes\n";

std::cout << "Speed: " << speed << " megahertz\n";

};class Laptop : public Computer {

private:

int length;

int width;

int height;

int weight;

public:

// Default constructor

Laptop() : Computer() {

length = 0;

width = 0;

height = 0;

weight = 0;

} // Constructor to initialize all components

Laptop(int wordsize, int memorysize, int storagesize, int speed, int length, int width, int height, int weight)

: Computer(wordsize, memorysize, storagesize, speed) {

this->length = length;

this->width = width;

this->height = height;
this->weight = weight;

// Function to display data members

void display() {

Computer::display();

std::cout << "Length: " << length << " units\n";

std::cout << "Width: " << width << " units\n";

std::cout << "Height: " << height << " units\n";

std::cout << "Weight: " << weight << " units\n";

};int main() {

// Create a Computer object

Computer computer(64, 2222, 111, 1500);

std::cout << "Computer:\n";

computer.display();

std::cout << "\n"; // Create a Laptop object

Laptop laptop(64, 2222, 111, 1500, 1, 90, 12, 21);

std::cout << "Laptop:\n";

laptop.display();

return 0;

Output:

You might also like