You are on page 1of 19

COMSATS UNIVERSITY

ISLAMABAD

SYED SHAMS HAIDER

FA19-BEE-203\ISB

LAB REPORT 10

OBJECT ORIENTED
PROGRAMING

MAAM MEHWISH MEHMOOD

ELECTRICAL ENGINEERING
LAB TASK
Polymorphism

5.1 Consider the class


class base
{
public:
virtual void iam()
{
cout << “base\n”;
}
};
a. Derive two classes from class base, and for each define iam()
to write out the
name of the class.
b. Declare objects of each class, and call iam() from them.
c. Assign the address of objects of the derived classes to base
pointers and call
iam() through the pointers.
d. Remove the virtual keyword from the base class member
function, run your code
again, and compare the results.

CODE
#include <iostream>
using namespace std;
class base//syed shams haider
{
public:
virtual void iam()
{
cout << "base\n";
}
};
class DerivedClassA : public base
{
public:
void iam()
{
cout << "Derived Class A" << endl;
}
};
class DerivedClassB : public base
{
public:
void iam()
{
cout << "Derived Class B" << endl;
}
};
int main()
{
base *ptrBase;
DerivedClassA objA;
DerivedClassB objB;

objA.iam();
objB.iam();

ptrBase = &objA;
ptrBase->iam();
ptrBase = &objB;
ptrBase->iam();
return 0;
}

DEV C++
5.2
Develop a simple payroll application. There are three kinds of
employees in the
system: salaried employee, hourly employee, and commissioned
employee. The
system takes as input an array containing employee objects, calculates
salary
polymorphically, and generates report.
Make Employee an abstract class. Declare salary() and display() as pure
virtual
functions in it. Derive salaried employee (monthly), hourly employee
(per hour basis),
and commissioned employee (bonus on completing each target) from
base class
Employee. The display() function should show employee no, employee
name, and
salary of all employees

CODE
#include<iostream>
#include<string>
using namespace std;
class Employee
{
public:
virtual void salary () = 0 ;
virtual void display() = 0 ;
};
class salariedemployee : public Employee
{
private:
long emp_num;
string name;
double sal;
public:
void salary()
{
//cout << "PROGRAMMED BY syed shams haider" << endl;
cout << "Enter employee number" << endl;
cin >> emp_num;
cout << "Enter employee name" << endl;
cin >> name;
cout << "Enter employee salary" << endl;
cin >> sal;
}
void display()
{
cout << "Employee Number is " << emp_num << endl;
cout << "Employee Name is " << name << endl;
cout << "Employee Salary is " << sal << endl;
}
};
class hourlyemployee : public Employee
{
private:
string name;
double result;
double hours;
double rate;
public:
void salary()
{
cout << "Enter employee name " << endl;
cin >> name;
cout << "Enter hourly rate of " << name << endl;
cin >> rate;
cout << "Enter hours " << name << " worked " << endl;
cin >> hours;
}
void display()
{
result = hours * rate;
cout << name << "'s Hourly salary is " << result << endl;
}
};
class commisonedemployee : public Employee
{
private:
string name;
double result;
int overtime;
double sal;
public:
void salary()
{
cout << "Enter employee name " << endl;
cin >> name;
cout << "Enter " << name << "'s salary " << endl;
cin >> sal;
cout << "Enter " << name << "'s overtime " << endl;
cin >> overtime;}
void display()
{ result = overtime * 2500;
cout << name << "'s salary is " << result + sal <<endl; }
};
int main ()
{
Employee *e[2];
salariedemployee s;
hourlyemployee h;
commisonedemployee c;
cout << " DATA FOR MONTHLY EMPLOYEE "<<endl;
e[0]=&s;
e[0]->salary();
e[0]->display();
cout<<"\n DATA FOR HOURLY EMPLOYEE "<<endl;
e[1]=&h;
e[1]->salary();
e[1]->display();
cout<<" DATA FOR COMMISIONED EMPLOYEE "<<endl;
e[2]=&c;
e[2]->salary();
e[2]->display();
system("pause");
return 0;
}

DEV C++
5.3
Create a base class called shape. Use this class to store two
double type values that
could be used to compute the area of figures. Derive two
specific classes called
triangle and rectangle from the base shape. Add to base class, a
member function
get_data() to initialize base class data members and another
member functions
display_area() to compute and display the area of figures. Mark
the display_area() as
a virtual function and redefine this function in the derived class
to suit their
requirements.(Use pure virtual function)

CODE
#include<iostream>
using namespace std;
//Syed shams haider

class Shape
{
public: double a,b;
void get_data ()
{
cin>>a>>b;
}
virtual void display_area () = 0;
};

class Triangle:public Shape


{
public: void display_area ()
{
cout<<"Area of triangle "<<0.5*a*b<<endl;
}
};

class Rectangle:public Shape


{
public: void display_area ()
{
cout<<"Area of rectangle "<<a*b<<endl;
}
};

int main()
{
Triangle t;
Shape *st = &t;
cout<<"Enter base and altitude: ";
st->get_data();
st->display_area();

Rectangle r;
Shape *sr = &r;
cout<<"Enter length and breadth: ";
sr->get_data();
sr->display_area();
return 0;
}

DEV C++
HOME TASK
Create a class hierarchy that performs conversions from one system of units to
another. Your program should perform the following conversions,
i.
Liters to Gallons,
ii.
Fahrenheit to Celsius and
iii.
Feet to Meters
The base class convert declares two variables, val1 and val2, which hold the initial
and converted values, respectively. It also defines the functions getinit() and
getconv(), which return the initial value and the converted value. These elements
of
convert are fixed and applicable to all derived classes that will inherit convert.
However, the function that will actually perform the conversion, compute(), is a
pure
virtual function that must be defined by the classes derived from convert. The
specific
nature of compute() will be determined by what type of conversion is taking place.
Three classes will be derived from convert to perform conversions of Liters to
Gallons (l_to_g), Fahrenheit to Celsius (f_to_c) and Feet to Meters (f_to_m),
respectively. Each derived class overrides compute() in its own way to perform the
desired conversion.
Test these classes from main() to demonstrate that even though the actual
conversion
differs between l_to_g, f_to_c, and f_to_m, the interface remains constant.

CODE
#include <iostream>
using namespace std;//SYED SHAMS HAIDER FA19-BEE-203
class convert {
protected:
double val1; // initial value
double val2; // converted value
public:
convert(double i) {
val1 = i;
}
double getconv() { return val2; }
double getinit() { return val1; }
virtual void compute() = 0;
};
// Liters to gallons.
class l_to_g : public convert {
public:
l_to_g(double i) : convert(i) { }
void compute() {
val2 = val1 / 3.7854;
}
};
// Fahrenheit to Celsius
class f_to_c : public convert {
public:
f_to_c(double i) : convert(i) { }
void compute() {
val2 = (val1-32) / 1.8;
}
};
int main()
{
convert *p; // pointer to base class
l_to_g lgob(4);
f_to_c fcob(70);
// use virtual function mechanism to convert
p = &lgob;
cout << p->getinit() << " liters is ";
p->compute();
cout << p->getconv() << " gallons\n"; // l_to_g
p = &fcob;
cout << p->getinit() << " in Fahrenheit is ";
p->compute();
cout << p->getconv() << " Celsius\n"; // f_to_c
return 0;
}
DEV C++
CONCLUSION:
With the help of this lab report we became familiar with the
concepts of virtual functions and polymorphism,
late/dynamic binding, abstract class and pure virtual functions.

You might also like