Syed Shams Haider 203 Lab Report 07
Syed Shams Haider 203 Lab Report 07
ISLAMABAD
FA19-BEE-203\ISB
LAB REPORT 07
OBJECT ORIENTED
PROGRAMING
ELECTRICAL ENGINEERING
LAB TASK
5.1. Imagine a publishing company that markets both book and audio-
cassette versions of its works. Create a class publication that stores the
title and price of a publication. a. from this class derive two classes: i.
book, which adds a page count and ii. tape, which adds a playing time in
minutes. iii. each of these three classes should have getdata() function
to get its data from the user at the keyboard and a putdata() function
to display its data. b. Write a main() program to test the book and tape
class by creating instances of them, asking the user to fill in their data
with getdata() and then displaying the data with putdata().
Code
#include <iostream>
#include <string>
using namespace std;
class publication
{ private: string title; float
price; public:
void getdata()
{
cout << "\nEnter title: "; cin >> title;
cout << "Enter price: "; cin >> price;
}
void putdata()
const
{ cout << "\nTitle: " << title; cout <<
"\nPrice: " << price;
} };
class book : private publication
{ private:
int pages; public:
void getdata()
{
publication::getdata();
cout << "Enter number of pages: "; cin >> pages;
}
void putdata() const
{
publication::putdata();
cout << "\nPages: " << pages;
} };
class tape : private publication
{ private:
float time; public: void getdata()
{
publication::getdata();
cout << "Enter playing time: "; cin >> time;
}
void putdata() const
{
publication::putdata();
cout << "\nPlaying time: " << time;
} };
int main()
{
book book1; tape tape1;
book1.getdata(); tape1.getdata();
book1.putdata(); tape1.putdata();
cout << endl; return 0;
}
DEV C++
OUTPUT
5.2.Write a class Person that has attributes of id, name
and address. It has a constructor to initialize, a member
function to input and a member function to display data
members. Create
another class Student that inherits Person class. It has
additional attributes of roll number and marks. It also has
member function to input and display its data members.
CODE
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
int id;
string
name,add;
Person();
void input()
{
cout<<"Enter
Name:"; cin>>name;
cout<<"\nEnter ID: ";
cin>>id;
cout<<"\nEnter Address: ";
cin>>add;
}
void display()
{
cout<<"\nName: "<<name;
cout<<"\nAddress "<<add;
cout<<"\nID: "<<id;
}
};
class student:public Person
{
public:
int rn,m;
void
input1()
{
input();
cout<<"\nEnter Roll Number: ";
cin>>rn;
cout<"\nEnter marks: ";
cin>>m;
}
void show1()
{
display();
cout<<"\nRoll Number:
"<<rn; cout<<"\nMarks: "<<m;
}
};
int main()
{
student
obj;
obj.input1();
obj.show1();
}
DEV C++
5.3
Write a base class Computer that contains data members
of wordsize(in bits), memorysize (in megabytes),
storagesize (in megabytes) and speed (in megahertz).
Derive a Laptop class that is a kind of computer but also
specifies the object’s length, width, height, and weight.
Member functions for both classes should include a
default constructor, a constructor to inialize all
components and a function to display data members
CODE
#include <iostream>
using namespace std;
class Computer
{
protected:
int wordSize;
int memorySize;
double storageSize;
int speed;
public:Computer()
{}
Computer(int,
int, double, int);
void show();
};
class Laptop:public Computer
{ private:
double lenght, width, height;
double weight;
public:Laptop()
{
DEV C++
OUTPUT
CODE
#include<iostream>
using namespace std;
class student
{
public:
int r; string n;
void getdata()
{
cout<<"Enter roll# &
name"<<endl; cin>>r>>n;
}
void putdata()
{
cout<<"Roll Number: "<<r;
cout<<"\nName: "<<n;
}
};
class test:public student
{
public:
int
t1,t2,t3,s1,s2,a,f;
void getmarks()
{
cout<<"\nEnter marks of tests, sessionals and assignment"<<endl;
cin>>t1>>t2>>t3>>s1>>s2>>a;
}
void putmarks()
{
cout<<"\nTest Marks:
"<<t1<<endl<<t2<<endl<<t3;
cout<<"\nSessional1 Marks: "<<s1<<endl;
cout<<"\nSessional2 Marks: "<<s2;
cout<<"\nAssignment Marks: "<<a;
}
void finalresult()
{
f=t1+t2+t3+s1+s2+a;
cout<<"\nTotal marks: "<<f;
}
};
int main()
{
test obj; obj.getdata(); //obj.putdata(); obj.getmarks();
obj.putdata(); obj.putmarks(); obj.finalresult();
}
DEV C++
OUTPUT
6.2
Write a program that declares two classes. The parent class is called
Simple that has two data members num1 and num2 to store two
numbers. It also has four member functions. The add() function adds
two numbers and displays the result. The sub() function subtracts two
numbers and displays the result. The mul() function multiplies two
numbers and displays the result. The div() function divides two
numbers and displays the result. The child class is called Complex that
overrides all four functions. Each function in the child class checks the
value of data members. It calls the corresponding member function in
the parent class if the values are greater than 0. Otherwise it displays
error message
CODE
#include <iostream>using namespace std;
class Simple
{
protected:
int a,b;
public:Simple()
{ a=b=0; }
void in()
{ cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
void add()
{
cout<<"a + b= "<<a+b<<endl;
}
void sub()
{
cout<<"a - b= "<<a-b<<endl;
}
void mul()
{
cout<<"a * b= "<<a*b<<endl;
}
void div()
{
cout<<"a / b="<<a/b<<endl;
}
};
class Complex:public Simple
{
public:void add()
{ if(a<=0 || b<=0)
cout<<"Inavlid vlaues."<<endl;
else
Simple::add();
}
void sub()
{ if(a<=0 || b<=0)
cout<<"Inavlid vlaues."<<endl;
else
Simple::sub();
}
void mul()
{
if(a<=0 || b<=0)
cout<<"Inavlid vlaues."<<endl;
else
Simple::mul();
}void div()
{
if(a<=0 || b<=0)
cout<<"Inavlid values"<<endl;
else
Simple::div();
}
};
int main ()
{Complex obj;
obj.add();
obj.in();
obj.add();
obj.sub();
obj.mul();
obj.div();
return 0;
DEV C++
OUTPUT
6.3
An electricity board charges the following rates to domestic users to
discourage large consumption of energy. For the first 100 units − 50 P
per unit Beyond 100 units − 60 P per unit If the total cost is more than
Rs.250.00 then an additional surcharge of 15% is added on the
difference. Define a class Electricity in which the function Bill computes
the cost. Define a derived class More_Electricity and override Bill to add
the surcharge.
CODE
#include<iostream>
#include<conio.h>
using namespace std;
class electricity
{ protected:
float unit;
float cost;
public:
void bill()
{
cout<<"\n enter the no. of units"<<endl;
cin>>unit;
if(unit<=100)
{
cost=0.50*unit;
cout<<"cost up to 100 unit is Rs."<<cost<<endl;
} else
{
if(unit>300)
{
cost=0.60*unit;
cout<<"Beyond 300 units is Rs"<<cost<<endl;
}
}
}
};
class more_electricity:public electricity
{
float
surcharge,diff,total_
cost;
public:
void bill()
{
electricity::bill();
if(cost>250.00)
{
diff=cost-250;
surcharge=diff*0.15
;
total_cost=cost+sur
charge;
cout<<" Bill amount with surcharge is Rs"<<total_cost;
} else
{
cout<<"Bill amout is Rs."<<cost;
}
}
};
int main()
{
more_electricity me;
me.bill();
getch();
}
DEV C++
OUTPUT
CONCLUDE
We are able to declare the derived classes along with the
access of base class members. They should learn the purpose of
protected Access
Specifier and working with derived class constructors.