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

Lab Paper Final Term 20011598-138

This document contains a student's lab paper submission for an OOP class. It includes 5 questions with code snippets and outputs. The questions cover topics like inheritance, polymorphism, and calculating areas of shapes using derived classes.

Uploaded by

Zain Afzaal
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)
46 views

Lab Paper Final Term 20011598-138

This document contains a student's lab paper submission for an OOP class. It includes 5 questions with code snippets and outputs. The questions cover topics like inheritance, polymorphism, and calculating areas of shapes using derived classes.

Uploaded by

Zain Afzaal
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/ 31

Name: Zain AfzaaL

Class: BSSE Section A


Roll No: 20011598-138
Submitted to: Prof. Muhammad Jabaar
Date: 9 August 2021
Subject: OOP

LAB Paper
Question 1:
Output:
Question 2:
Output:

Description:
In this program we have take two times one is initialized and one is
not initialized. Then we added both times and stored it in another
data member and displayed all the three times.
Question 3:
#include <iostream>

#define MAX_SIZE 100 // Maximum size of the array


using namespace std;

class Check
{
private:
int arr[MAX_SIZE]; // Declares an array of size 100
int num; // Total number of elements in array
int i, j, k;
public:
void Getdata()
{
//Enter size of array
cout << "Enter size of the array : ";
cin >> num;

//Reading elements of array


cout << "Enter elements in array : ";
for (i = 0; i < num; i++)
{
cin >> arr[i];
}
}

void Duplicate()
{
// Finding all duplicate elements in array

for (i = 0; i < num; i++)


{
for (j = i + 1; j < num; j++)
{
//If any duplicate found */
if (arr[i] == arr[j])
{
// Delete the current duplicate element
for (k = j; k < num; k++)
{
arr[k] = arr[k + 1];
}

//Decrement size after removing duplicate


element
num--;

// If shifting of elements occur then don't


increment j
j--;
}
}
}
}

void ShowData()
{
// Print array after deleting duplicate elements

cout << "\nArray elements after deleting duplicates :


";
for (i = 0; i < num; i++)
{
cout << "\t" << arr[i];
}
}
};

int main()
{
Check C1;

C1.Getdata();
C1.Duplicate();
C1.ShowData();
}

Output:

Description:
In this program we have taken an array and deleted
duplicate array elements by using function and also
sorted elements by another function and displayed
the sorted array.
Question 4:
#include <iostream>
#include <cstring>
using namespace std;
class staff{
public:
string name;
long double cnic;
long double ph_no;
int emp_code;
void information(){
cout<<"enter name :";
cin>>name;
cout<<"enter CNIC :";
cin>>cnic;
cout<<"enter phone number :";
cin>>ph_no;
cout<<"enter employ code :";
cin>>emp_code;
}
void show(){
cout<<"Name is :"<<name<<endl;
cout<<"CNIC is :"<<cnic<<endl;
cout<<"phone number is
:"<<ph_no<<endl;
cout<<"Employ code is
:"<<emp_code<<endl;
}
};
class teacher:virtual public staff{
public:
string subject;
int clas;
void information(){
cout<<"which subject do you teach :";
cin>>subject;
cout<<"which class do you teach :";
cin>>clas;
}
void show(){
cout<<"Subject you teach is
:"<<subject<<endl;
cout<<"class you teach is
:"<<clas<<endl;
}

};
class officer:virtual public staff{
public:
int grade;
int room_no;
void information(){
cout<<"what is your grade :";
cin>>grade;
cout<<"what is your room number :";
cin>>room_no;
}
void show(){
cout<<"Your grade is
:"<<grade<<endl;
cout<<"your room number is
:"<<room_no<<endl;
}

};
class typist:public staff{
public:
int speed;
string language;
void information(){
cout<<"what is your typing speed :";
cin>>speed;
cout<<"what is your language :";
cin>>language;
}
void show(){
cout<<"your typing speed is
:"<<speed<<" per minute "<<endl;
cout<<"your language is
:"<<language<<endl;
}

};
class regular:public typist{
public:
long double salary;
int hours;
void information(){
cout<<"How much is your
salary"<<endl;
cin>>salary;
cout<<"for how many hours do you
work"<<endl;
cin>>hours;
}
void show(){
cout<<"your salary is"<<endl;
cout<<salary<<"$"<<endl;
cout<<"you work for"<<endl;
cout<<hours<<" hours"<<endl;
}
};
class visiting:public typist{
public:
string daily;
int time;
void information(){
cout<<"Do you visit daily"<<endl;
cin>>daily;
cout<<"timing of your visit"<<endl;;
cin>>time;
}
void show(){
cout<<" your Daily visit "<<endl;
cout<<daily<<endl;
cout<<"timing of your visit is"<<endl;
cout<<time<<endl;
}
};
int main(){
officer o;
teacher t;
regular r;
visiting v;
char choice;
int ch;
do{

cout<<"1=>press 1 if you are staff person


:"<<endl;
cout<<"2=>press 2 if you are teacher :"<<endl;
cout<<"3=>press 3 if you are officer :"<<endl;
cout<<"4=>press 4 if you are regular :"<<endl;
cout<<"5=>press 5 if you are typist :"<<endl;
cout<<"6=>press 6 if you are visiting :"<<endl;

cin>>ch;
if(ch==1){
t.staff::information();
cout<<"\n---------------------------------------------
---------------------\n\n";
t.staff::show();
}
else if(ch==2){
t.information();
cout<<"\n---------------------------------------------
----------------------\n\n";

t.show();
}
else if(ch==3){
o.information();
cout<<"\n---------------------------------------------
-----------------------\n\n";

o.show();
}
else if(ch==4){
r.information();
cout<<"\n---------------------------------------------
------------------------\n\n";

r.show();
}
else if(ch==5){
r.typist::information();
cout<<"\n---------------------------------------------
------------------------\n\n";

r.typist::show();
}
else if(ch==6){
v.information();
cout<<"\n---------------------------------------------
-----------------------\n\n";

v.show();
}
cout<<"do you want to do it again y/n"<<endl;
cin>>choice;
}while(choice=='y');
return 0;
}
Output:

Description:
In this program we have taken 5 classes using
inheritance. All classes have the functions as
shown I the diagram. If you want to maintain
data then the menu is shown as you can see in
the output.

Question 5:
#include <iostream>
#include <cstring>
#include <iomanip>
/* run this program using the console pauser or add your own
getch, system("pause") or input loop */
using namespace std;

class shape{
protected:
double height;
double width;
double base1;
double base2;
double result;
char ch;
public:

virtual
~shape(){}
virtual void
area(){}
virtual void
Display(){}
void get(){

cout<<"Whose
Area You Want To Find? Enter T or R :"<<endl;
cin>>ch;
if(ch=='T'){

cout<<"Enter
Height"<<endl;

cin>>height;

cout<<"Enter
Base 01"<<endl;

cin>>base1;

cout<<"Enter
Base 02"<<endl;

cin>>base2;}
else
if(ch=='R'){
cout<<"Enter
Height"<<endl;

cin>>height;

cout<<"Enter
Width"<<endl;

cin>>width;
}
else

cout<<"Error"<<endl;
}

};
class Rectangle: virtual public shape{
public:
void area(){
get();
if(ch=='R')

result=height*wi
dth;
}
void Display(){
if (ch=='R'){

cout<<"Area
Of Rectangle "<<endl;

cout<<result<<endl;}
}
};
class Trapezoid: virtual public shape{
public:
void area(){
get();
if(ch=='T')
result=height*(ba
se1+base2/2);
}
void Display(){
if(ch=='T'){

cout<<"Area
Of Trapezoid "<<endl;

cout<<result<<endl;}
}
};

int main(int argc, char** argv) {


Rectangle obj1;
Trapezoid obj2;
obj1.area();
obj1.Display();
obj2.area();
obj2.Display();
return 0;
}

Output:

Description:
In this program we have created a class shape and
calculated the area of the shapes by using polymorphism
and displayed the area by using functions.
Question 6 (a):

Output:
Question 6 (b):

Output:
Question 6 (c):

Output:

THE END

You might also like