You are on page 1of 15

Submitted By: AREEBA QAYYUM KHAN

Student ID: 221343


Class: BS CS
Subject: Object Oriented Programming Theory
Submitted to: Sir Abdul Basit Kazmi
Semester: Summer-23
Solution of Quiz:

#include<iostream>

using namespace std;

class Rectangle

protected:

int height, width;

public:

void set_width(int w)

width = w;

void set_height(int h)

height = h;

int get_area()

return width * height;

int get_perimeter()

return 2 * (width + height);

}
};

class Square : public Rectangle

public:

int length;

Square()

Square(int length){

set_width(length);

set_height(length);

};

class Cube : public Square

public:

int depth;

Cube()

Cube(int length) : Square(length)

{}

int volume()

return width * width * width;

int get_surfaceArea()
{

return 6 * width * width;

};

int main()

Rectangle a1;

Rectangle p1;

int length, depth, width, height;

cout<<"Enter the width of a side of the first rectangle"<<endl;

cin>>width;

p1.set_width(width);

cout<<"Enter the height of a side of the first rectangle"<<endl;

cin>>height;

p1.set_height(height);

cout<<endl;

cout<<"Enter the width of a side of the second rectangle"<<endl;

cin>>width;

a1.set_width(width);

cout<<"Enter the height of a side of the second rectangle"<<endl;

cin>>height;

a1.set_height(height);

cout<<endl;

cout<<"Enter the length of the side of the Square"<<endl;

cin>>length;

Square s1(length);

cout<<endl;

cout<<"Enter the length of the side of the cube"<<endl;


cin>>length;

Cube c1(length);

cout<<endl;

cout<<"\t\t\t\t==========********=========="<<endl;

cout<<"The area of first rectangle is equal to:"<<p1.get_area()<<endl;

cout<<"The perimeter of the first rectangle is equal to:"<<p1.get_perimeter()<<endl;

cout<<endl;

cout<<"The area of second rectangle is equal to:"<<a1.get_area()<<endl;

cout<<"The perimeter of the second rectangle is equal to:"<<a1.get_perimeter()<<endl;

cout<<endl;

cout<<"The area of the square is equal to:"<<s1.get_area()<<endl;

cout<<"The perimeter of the square is equal to:"<<s1.get_perimeter()<<endl;

cout<<endl;

cout<<"The surface area of the cube is equal to:"<<c1.get_surfaceArea()<<endl;

cout<<"The depth of the cube is equal to:"<<c1.volume()<<endl;

return 0;

}
Output:

Solution for Assignment:

#include<iostream>

using namespace std;

#include<string.h>

class Person

protected:

string name;

string Address;

string CNIC;

string CellNo;

int static count;

public:

Person()

cout<<"Inside Person's Non-Parameterized Constructor"<<endl;


count++;

Person(string n, string a, string cnic, string cell) : name(n), Address(a), CNIC(cnic), CellNo(cell)

cout<<"Inside Person's Parameterized Constructor"<<endl;

~Person()

cout<<"Inside Person's Destructor"<<endl;

count--;

void set_name(string n)

name = n;

void set_Address(string a)

Address = a;

void set_CNIC(string cnic)

CNIC = cnic;

void set_CellNo(string cell)

CellNo = cell;

string get_name()

{
return name;

string get_Address()

return Address;

string get_CNIC()

return CNIC;

string get_CellNo()

return CellNo;

static void get_count()

cout<<"The count for people now is:"<<count<<endl;

};

int Person :: count = 0;

class Teacher : public Person{

public:

string TeacherID;
Teacher()

cout<<"Inside Teacher's Non-Parameterized Constructor"<<endl;

count++;

Teacher(string n, string a, string cnic, string cell, string teacher) : Person(n,a,cnic,cell)

TeacherID = teacher;

cout<<"Inside Teacher's Parameterized Constructor"<<endl;

~Teacher()

cout<<"Inside Teacher's Destructor"<<endl;

count--;

void set_TeacherID(string teacher)

TeacherID = teacher;

string get_TeacherID()

return TeacherID;

static void get_count()

cout<<"The count for teachers now is:"<<count<<endl;

void print()

{
cout<<"NAME:"<<name<<endl;

cout<<"ADDRESS:"<<Address<<endl;

cout<<"CNIC"<<CNIC<<endl;

cout<<"CELL NUMBER:"<<CellNo<<endl;

cout<<"TEACHER ID:"<<TeacherID<<endl;

};

class Student : public Person

public:

string StudentID;

Student()

cout<<"Inside Student's Non-Parameterized Constructor"<<endl;

count++;

Student(string n, string a, string cnic, string cell, string student) : Person(n,a,cnic,cell){

StudentID = student;

cout<<"Inside Student's Parameterized Constructor"<<endl;;

~Student()

cout<<"Inside Student's Destructor"<<endl;

count--;

void set_StudentID(string student)

StudentID = student;

}
string get_StudentID()

return StudentID;

static void get_count()

cout<<"The count for students now is:"<<endl;

cout<<count<<endl;

void print()

cout<<"NAME:"<<name<<endl;

cout<<"ADDRESS:"<<Address<<endl;

cout<<"CNIC:"<<CNIC<<endl;

cout<<"CELL NUMBER:"<<CellNo<<endl;

cout<<"STUDENT ID:"<<StudentID<<endl;

};

int main()

Person p1; //Object for Person Class with Non-Parameterized Constructor(PNP)

Teacher t1; // Object for Teacher Class with Non-Parameterized Constructor(PNP)

p1.get_count(); //count for person will be incremented

t1.get_count(); //count for teacher will be incremented

p1.get_count(); //count for person will be incremented

Student s1; //Object for Student Class with Non-Parameterized Constructor(PNP)

t1.get_count(); //count for teacher will be incremented


p1.get_count(); //count for person class will be incremented

Person p2("John", "123 Main St", "123456789", "123-456-7890");// 2nd Object for Person Class with
Parameterized Constructor(PP)

p2.get_count(); //count for person will be incremented

Teacher t2("Alice", "456 Elm St", "987654321", "987-654-3210", "T123");// 2nd Object for Teacher
Class with Parameterized Constructor(PP)

t2.get_count(); //count for teacher will be incremented

p2.get_count(); //count for person will be incremented

Student s2("Bob", "789 Oak St", "543216789", "543-216-7890", "S456");// 2nd Object for Student
Class with Parameterized Constructor(PP)

t2.get_count(); //count for teacher will be incremented

p2.get_count(); // count for person will be incremented

// calling print function to print info in data members for Teacher

cout<<"Teacher's Details:"<<endl;

t2.print();

cout<<endl;

// calling print function to print info in data members for Student

cout<<"Student's Details:"<<endl;

s2.print();

// Using accessors to update the info in data members for Person

p1.set_name("Faiza");

p1.set_CNIC("198-231-4526");

p1.set_Address("45 Queen St");

p1.set_CellNo("550383632");
// Using accessors to update the info in data members for Student

t1.set_name("Ali");

t1.set_CNIC("218-221-7865");

t1.set_Address("234 Charles St");

t1.set_CellNo("321564789");

t1.set_TeacherID("901233");

// Using accessors to update the info in data members for Teacher

s1.set_name("Timothee");

s1.set_CNIC("102-541-0987");

s1.set_Address("321 King St");

s1.set_CellNo("8970274921");

s1.set_StudentID("221000");

cout<<endl;

// Using mutators to display the info in data members for Person

cout<<"Updated Person's Details:"<<endl;

cout<<"Name:"<<p1.get_name()<<endl;

cout<<"CNIC:"<<p1.get_CNIC()<<endl;

cout<<"Address:"<<p1.get_Address()<<endl;

cout<<"Cell Number:"<<p1.get_CellNo()<<endl;

cout<<endl;

// Using mutators to display the info in data members for Teacher

cout<<"Updated Teacher's Details:"<<endl;

cout<<"Name:"<<t1.get_name()<<endl;

cout<<"CNIC:"<<t1.get_CNIC()<<endl;
cout<<"Address:"<<t1.get_Address()<<endl;

cout<<"Cell Number:"<<t1.get_CellNo()<<endl;

cout<<"Teacher's ID:"<<t1.get_TeacherID()<<endl;

cout<<endl;

// Using mutators to display the info in data members for Student

cout<<"Updated Student's Details:"<<endl;

cout<<"Name:"<<s1.get_name()<<endl;

cout<<"CNIC"<<s1.get_CNIC()<<endl;

cout<<"Address:"<<s1.get_Address()<<endl;

cout<<"Cell Number:"<<s1.get_CellNo()<<endl;

cout<<"Student's ID:"<<s1.get_StudentID()<<endl;

return 0;

}
Output:

You might also like