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

COMSATS University Islamabad: Object Oriented Programing

The lab report discusses 4 tasks completed in an Object Oriented Programming lab using C++. [Task 1] creates base and derived classes to demonstrate inheritance and accessing protected data. [Task 2] creates classes to store person and employee data and demonstrates accessing base class functions. [Task 3] creates a Date class that uses a default constructor. [Task 4] creates Teacher, Writer, and Scholar classes to demonstrate multi-level inheritance. The document provides code samples and output for each task.

Uploaded by

Souban Javed
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)
200 views

COMSATS University Islamabad: Object Oriented Programing

The lab report discusses 4 tasks completed in an Object Oriented Programming lab using C++. [Task 1] creates base and derived classes to demonstrate inheritance and accessing protected data. [Task 2] creates classes to store person and employee data and demonstrates accessing base class functions. [Task 3] creates a Date class that uses a default constructor. [Task 4] creates Teacher, Writer, and Scholar classes to demonstrate multi-level inheritance. The document provides code samples and output for each task.

Uploaded by

Souban Javed
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/ 28

LAB REPORT #08

Object Oriented Programing


DEV C++ Coding with output

Made by:
Muhammad Furqan Saad Akram

(FA18-BEE-174)

Submitted to:

Mam Mehwish Mehmood

Date:
21/11/2020

COMSATS University Islamabad


IN LAB:-

TASK 1 CODE:-

#include <iostream>

using namespace std;

class Base

protected:

int m_value;

public:

Base(int value)

: m_value(value)

void identify() { cout << "I am a Base\n"; }

};

class Derived1: public Base

public:

Derived1(int value)
: Base(value)

int getvalue1() {return m_value;}

};

class Derived2: public Derived1

public:

Derived2(int value)

: Derived1(value)

int getvalue2() {return m_value;}

};

int main()

Derived1 derived(5);

Derived2 derived1(6);

cout << "derived has value " << derived.getvalue1() << '\n';

cout << "derived has value " << derived1.getvalue2() << '\n';

return 0;
}

OUTPUT:-

TASK 2 CODE:-

#include <iostream>

#include <stdio.h>

using namespace std;

//Base Class - basicInfo

class person

{
protected:

char name[30];

int age;

char gender;

public:

void getpersonInfo(void)

cout << "Enter Name: ";

cin.getline(name,30);

cout << "Enter age: ";

cin >> age;

cout << "Enter Gender: ";

cin >> gender;

};

//Base Class - deptInfo

class empolyee

protected:

char Name[30];
char dailywages[30];

int time2dowork;

public:

void getempInfo(void)

cout << "Enter employee Name: ";

//cin.ignore(1);

fflush(stdin);

cin.getline(Name,30);

cout << "Enter dailywages: ";

fflush(stdin);

cin.getline(dailywages,30);

cout << "Enter time in hours to do work: ";

cin >> time2dowork;

};

/*final class (Derived Class)- employee*/

class teacher:private person, private empolyee

public:
void getEmployeeInfo(void){

cout << "Enter person basic info: " << endl;

//call getBasicInfo() of class basicInfo

getpersonInfo(); //calling of public member function

cout << "Enter employee's info: " << endl;

//call getDeptInfo() of class deptInfo

getempInfo(); //calling of public member function

void printEmployeeInfo(void)

cout << "person Information is: " << endl;

cout << "Basic Information...:" << endl;

cout << "Name: " << name << endl; //accessing protected data

cout << "person age: " << age << endl; //accessing protected data

cout << "Gender: " << gender << endl << endl;//accessing protected data

cout << "Employee Information...:" << endl;

cout << "Employee Name: " << Name << endl; //accessing protected data

cout << "dailywages: " << dailywages << endl; //accessing protected data

cout << "Time to do work: " << time2dowork<< endl; //accessing protected data

}
};

int main()

//create object of class employee

teacher emp;

emp.getEmployeeInfo();

emp.printEmployeeInfo();

return 0;

OUTPUT:-
TASK 3 CODE:-

#include <iostream>

#include <ctime>

std::string months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",

"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

std::string days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri",

"Sat"};

class Date{

// Private Members

private:
std::string month;

std::string day;

int date;

int year;

// Public Members

public:

// Default Constructor

Date() {

const int BASE_YEAR = 1900;

time_t timer;

tm * time;

std::time(&timer);

time = localtime(&timer);

date = time->tm_mday;

month = months[time->tm_mon];

day = days[time->tm_wday];

year = time->tm_year + BASE_YEAR;

void printDate(void) {

std::cout << "Current date "

<< this->month << " " << this->day << " "
<< this->date << " " << this->year;

// Destructor

~Date() {}

};

int main()

Date d;

d.printDate();

OUTPUT:-
TASK 4 CODE:-

#include<iostream>

#include<conio.h>//header file

using namespace std;

class Teacher{//define class

private://access specifie

char Name[30];//data member

int age;

char Address[50];

public://access specifier
void input(){ cout<<endl<<"Enter your Name "<<endl;

cin>>Name;

cout<<"Enter your age "<<endl;

cin>>age;

cout<<"Enter your Address "<<endl;

cin>>Address;

void display()

{//function

cout<<endl<<"Your Name is "<<Name<<endl;

cout<<"Your Age is "<<age<<endl;

cout<<"Your Address is "<<Address<<endl;}};

class Writer{

private://access specifierchar

char W_name[30];

char W_address[50];//data member

int No_Books;

public://access specifier
void input(){//function

cout<<"Enter Writer Name "<<endl;cin>>W_name;

cout<<"Enter Writer Address "<<endl;cin>>W_address;

cout<<"Enter No of Books written by Writer "<<endl;cin>>No_Books;}

void display(){//function

cout<<endl<<"Writer Name :"<<W_name<<endl;

cout<<"Writer Address :"<<W_address<<endl;

cout<<"No of Books "<<No_Books<<endl;}

};

class Scholar:public Teacher,public Writer//derived class

{private://access specifier

public://access specifier

void input()

{//function

Teacher::input();//access function of parant class

Writer::input();//access function of parant class

void display(){//function

Teacher::display();//access function of parant class

Writer::display();//access function of parant class


}};

int main()

{//main function

//clrscr();

Scholar S1;//Objec

S1.input();//Function

S1.display();

getch();}

OUTPUT:-

HOME TASK :-
H1 CODE:-

#include <iostream>

using namespace std;

class student

protected:

int rollno;

public:

void getnum()

cout<<"\n enter roll no.: ";

cin>>rollno;

void putnum()

cout<<"\n roll no: "<<rollno;

};

class test:public student

{
protected:

int marks;

public:

void getmarks()

cout<<"\n enter marks: ";

cin>>marks;

void putmarks()

cout<<"\n marks: "<<marks;

};

class sports :public student

protected:

int score;

public:

void getscore()

cout<<"\n enter your score: ";

cin>>score;
}

void putscore()

cout<<"\n score: "<<score;

};

class result:public test,public sports

int total;

student s;

public:

void display()

total=marks+score;

s.getnum();

s.putnum();

cout<<"\n marks: "<<marks;

cout<<"\n score: "<<score;

cout<<"\n total: "<<total;

};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

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

result obj;

obj.getmarks();

obj.getscore();

obj.putmarks();

obj.putscore();

obj.display();

return 0;

OUTPUT:-
H2 CODE:-

#include<iostream>

#include<conio.h>

using namespace std;

class localphone{

private:

long phone;

public:

void get()
{

cout<<"\n enter local phone number";

cin>>phone;

void show(){

cout<<"\n phone num is :"<<phone;

};

class natphone:public localphone

private:

int code;

public:

void get1(){

natphone::get1();

cout<<"Enter city code"<<endl;

cin>>code;

void show(){

cout<<"\n city code is :"<<code;

};
class intphone:public natphone

{private:

int country_code;

public:

void get2(){

intphone::get2();

cout<<"Enter country code"<<endl;

cin>>country_code;

void show(){

natphone::show();

cout<<"contry code is :"<<country_code;

};

int main(){

intphone obj;

obj.get();

obj.show();

return 0;

}
OUTPUT:-

H3 CODE:-

#include <iostream>

using namespace std;

class publication

private:
string title;

float price;

public:

void getdata()

string t;

float p;

cout<< "Enter title: ";

cin>> title;

cout<< "Enter price: ";

cin>> price;

void showdata()

cout<< "\n title: " << title;

cout<< "\n price: " <<price ;

};

class sales

private:

float S1, S2, S3;


public:

void getdata()

cout<< "\n enter month 1 sale: ";

cin>>S1;

cout<< "\n enter month 2 sale: ";

cin>>S2;

cout<< "\n enter month 3 sale:";

cin>>S3;

void putdata()

cout<< "\n month 1 sale:" <<S1 <<"$" ;

cout<< "\n month 2 sale:" <<S2 <<"$";

cout<< "\n month 3 sale:" <<S3 <<"$";

};

class book :public publication,public sales

private:

int pagecount;

public:
void getdata()

publication::getdata();

sales::getdata();

cout<< "EnterPages: ";

cin>>pagecount;

void putdata()

publication::showdata();

sales::putdata();

cout<< "\n book pages: " <<pagecount<<endl;

};

class tape :public publication,public sales

private:

float time;

public:

void getdata()

publication::getdata();
sales::getdata();

cout<< "\n enter playing time: ";

cin>> time;

void putdata()

publication::showdata();

sales::putdata();

cout<< "\n playing time: " << time;

};

int main(void)

book b;

tape t;

b.getdata();

t.getdata();

b.putdata();

t.putdata();

return 0;

}
OUTPUT:-

Conclusion
In this lab I learnt about class hierarchies, multiple inheritance and multilevel inheritance. How to
differentiate between public and private Inheritance and also the role of constructor and
destructors in derived classes. I implemented multilevel inheritance and multiple inheritance..

You might also like