OOP Lab Report-7
OOP Lab Report-7
OOP Lab Report-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.
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) {
};
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;
}
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.
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
{
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
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.