Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OOP Lab Report-7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Lab#7

Lab Title
Overriding in Inheritance
1. Objectives:
In this lab we will came to know the important concept of overriding in inheritance in Object
Oriented Programming.

2. Introduction:
In Object-oriented programming, Overriding is a feature in which child class has same
function that have already present in parent class. Overriding is use to override functionality
into child class. If derived class have same function(same name, parameter) then overriding is
done from base class.

3. In-Lab Tasks: Following are the in-lab tasks:


1.3. Task#1
Write a program that declares two classes. The parent class is called Simple that has two data
members num1 and num2to 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.

Task Description:
In this task, we simply take a class name as simple in which we take two integers than
initialize than we make accessor and mutator functions and we make a few functions like
add,sub,multi,division and than display in class complex we take same name function and
than check condition and than calling all the functions from main function.

Code:
#include <iostream>
using namespace std;
class Simple
{
protected:
int num1;
int num2;
public:
Simple(){}
Simple(int n, int m) {

Object Oriented Programming Page 1


num1 = n;
num2 = m;
}
void set_num1(int n) {
num1 = n;
}
int get_num1() {
return num1;
}
void set_num2(int n) {
num2 = n;
}
int get_num2() {
return num2;
}
void add() {
std::cout << num1 + num2 << std::endl;
}
void sub() {
std::cout << num1 - num2 << std::endl;
}
void div() {
std::cout << num1 / num2 << std::endl;
}
void mul() {
std::cout << num1 * num2 << std::endl;
}

};
class Complex: public Simple
{
public:
Complex(int n, int m) {
num1 = n;
num2 = m;
}
void add() {
if (num1 > 0 && num2 > 0) {
Simple::add();
}
else {
std::cout << "Invalid argument values" <<
std::endl;
}

Object Oriented Programming Page 2


}
void sub() {
if (num1 > 0 && num2 > 0) {
Simple::sub();
}
else {
std::cout << "Invalid argument values" <<
std::endl;
}
}
void mul() {
if (num1 > 0 && num2 > 0) {
Simple::mul();
}
else {
std::cout << "Invalid argument values" <<
std::endl;
}
}
void div() {
if (num1 > 0 && num2 > 0) {
Simple::div();
}
else {
std::cout << "Invalid argument values" <<
std::endl;
}
}
};
int main()
{ // example work program
Simple test_1(3, 5);
Simple test_2(-6, 5);
Complex test_3(3, 5);
Complex test_4(-6, 5);
test_1.add();
test_2.sub();
test_3.mul();
test_4.div();
return 0;
}

Object Oriented Programming Page 3


Output:

Task#2
Suppose we are developing a program that a car dealership can use to manage its
inventory of used cars. The dealership’s inventory includes three types of automobiles: cars,
pickup trucks, and sport-utility vehicles (SUVs). Regardless of the type, the dealership keeps
the following data about each automobile:
• Make
• Year model
• Mileage
• Price
Each type of vehicle that is kept in inventory has these general characteristics, plus its own
specialized characteristics. For cars, the dealership keeps the following additional data:
• Number of doors (2 or 4)
For pickup trucks, the dealership keeps the following additional data
• Drive type (two-wheel drive or four-wheel drive)
And, for SUVs, the dealership keeps the following additional data:
• Passenger capacity
Write a program which has a single base class to keep the record of general information
regarding all automobiles further derive three classes for each type of automobile each having
its specific characteristic i.e. Number of doors, Drive type and Passenger capacity.
Define appropriate accessor and mutator functions in the base class to get data from user and
display the results, override these functions in respective derived classes. In main function,
initialize any two of objects from user and one through argument constructor.
Your Output should look like this:
We have the following car in inventory:
2007 BMW with mileage of 50000 miles having 4 doors.
Price: $15000.00
We have the following truck in inventory:
2006 Toyota with mileage of 40000 miles and 4WD drive type.
Price: $12000.00
We have the following SUV in inventory:
2005 Volvo with mileage of 30000 miles and 5 passenger capacity.
Price: $18000.00

Task Description:
In this task, we simply take a class name dealer in which take model,millage,make and
price and initialize in class and get the data from user and than display and make another
class name as car in which we take a car doors and initialize and take the data from user and
next class we take a driver type and get data from user and display and we take a passanger
from next class and get data from user and than display and calling from the main function.

Object Oriented Programming Page 4


Code:
#include <iostream>
#include <process.h>
#include <string>
using namespace std;
class dealer
{
protected:
int make;
int model;
int millage;
float price;
public:
dealer()
{
make = 0; model = 0; millage = 0; price = 0;
}
dealer(int MK,int MD,int ML,float P)
{
make = MK; model = MD; millage = ML; price = P;
}
void get_data()
{
cout << "Enter the year of vehicle: "; cin >> make;
cout << "Enter the model of vehicle: "; cin >> model;
cout << "Enter the millage of vehicle: "; cin >>
millage;
cout << "Enter the price of vehicle: "; cin >> price;
}
void disp_data()
{
cout << "We have following invertory: " <<endl;
cout << make << model <<"with milage of"<< millage
<<endl;
cout << "Price: $" << price <<endl;
}
};
class cars : public dealer
{
protected:
int door;
public:
cars():dealer()
{

Object Oriented Programming Page 5


door = 0;
}
cars(int MK,int MD,int ML,float P,int d):
dealer(MK,MD,ML,P)
{
door = d;
}
void get_data()
{
dealer::get_data(); //function overriding
cout << "For Car!" <<endl;
cout << "Enter the doors = "; cin >> door;
}
void disp_data() //function for display
{
dealer::disp_data(); //function overriding
cout << "Doors: " << door <<endl;
}
};
class trucks : public dealer
{
protected:
int drivetype;
public:
trucks(int mk,int md,int ml,float pr,int
d):dealer(mk,md,ml,pr)
{
drivetype = d;
}
void get_data()
{
dealer::get_data(); //function overriding
cout << "For truck!" <<endl;
cout << "Enter the drive type: "; cin >> drivetype;
}
void disp_data()
{
dealer::disp_data(); //function overriding
cout << "Drive type: " << drivetype <<endl;
}
};
class SUV : public dealer
{
protected:

Object Oriented Programming Page 6


int passengers;
public:
SUV(int mk,int md,int ml,float pr,int
p):dealer(mk,md,ml,pr)
{
passengers = p;}
void get_data()
{ dealer::get_data(); //function overriding
cout << "For SUV!" <<endl;
cout << "Enter the passengers: "; cin >> passengers;}
void disp_data()
{ dealer::disp_data(); //function overriding
cout << "Passengers: " << passengers <<endl;}
};
int main()
{
cars c1; //declaration of object
c1.get_data();
c1.disp_data();
trucks t(2020,20,6789,78900.78,4);
t.get_data();
t.disp_data();
SUV s(1989,89,7676868,78900.67,90);
s.get_data();
s.disp_data();
}
Output:

Object Oriented Programming Page 7


4. Post-Lab Tasks:
1.4. Task#1
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.

Task Description:
In this task we simply take a class name as Electicity and we take a units and check ka
condition than get the data from user and then display make a another class moreElectricity
and we take bill price and cost of the bill and then calling from main function.

Code:
#include <iostream>
using namespace std;
class Electricity
{
protected:
double Units;
public:
void get_data()
{
cout<<"Enter Units:";
cin>>Units;
}
double Bill(double unit)
{
double tc;
if (unit < 100)
tc = unit * 0.5;
if (unit > 300)
tc = unit * 0.6;
double purchase = 0;
if (tc > 250)
purchase = tc * 0.15;
double total_cost;
total_cost = 250 + purchase + tc;
cout<<"Total Cost: "<<total_cost;
}
};
class MoreElectricity : public Electricity
{

Object Oriented Programming Page 8


public:
double Bill(double unit, double add)
{
Electricity b;
double price = b.Bill(unit);
double total_cost = price + add;
}
};
int main()
{
MoreElectricity Elec;
Elec.get_data();
Elec.Bill(20,15);
}
Output:

Task#2
Create a class 2D having the x and y coordinates of an object. Derive a class 3D form from
2D as a base class with an additional z coordinate, override x and y coordinates of 2D class
using setdata() function. Get the coordinates for two specific points and compute the distance
between them using formula d = ((x2 - x1)2 + (y2 - y1)2 + (z2 - z1)2)1/2.

Task Description:
In this task we simply take a class name as Two we take a two double variables and then
initialize and next class is Three and then all function calling from main function and get data
from the user to find distance between 2D and 3D points.

Code:
#include <iostream>
#include <cmath>
using namespace std;
class Two {
public:
Two(double x, double y);
double distanceTo(const Two& other) const;
protected:
double x;
double y;
};
Two::Two(double x, double y) : x(x), y(y)
{}
double Two::distanceTo(const Two& other) const

Object Oriented Programming Page 9


{
return sqrt((x-other.x)*(x-other.x) + (y-other.y)*(y-
other.y));
}
class Three : public Two {
public:
Three(double x, double y, double z);
double distanceTo(const Three& other) const;
protected:
double z;
};
Three::Three(double x, double y, double z) :
Two(x, y), z(z)
{}
double Three::distanceTo(const Three& other) const
{
return sqrt((x-other.x)*(x-other.x) + (y-other.y)*(y-
other.y)
+ (z-other.z)*(z-other.z));
}
int main() {
Two p1(1,2);
Two p2(3,4);
cout << "The Distance betwen 2d points p1 and p2: "
<< p1.distanceTo(p2) << endl;
Three pp1(0,0,0);
Three pp2(1, 3, 5);
cout << "The Distance between 3d points pp1 and pp2: "
<< pp1.distanceTo(pp2) << endl;}

Output:

Conclusion:
After completing this program we came to how overriding is done between based and derived
class and use of accessor and mutator function.

Object Oriented Programming Page 10

You might also like