LAB#07
Overriding in Inheritance
LAB OBJECTIVE:
To familiarize the students with the important concept of overriding in inheritance in Object
Oriented Programming .
INTRODUCTION:
Overriding:
Function overriding is a feature that allows us to have a same function in child class which is
already present in the parent class. A child class inherits the data members and member functions
of parent class, but when you want to override functionality in the child class then you can use
function overriding. You have learned that a derived class can inherit both public and protected
members (both variable and functions) from a base class. However, the derived class can also
redefine the inherited member function. If the derived class defines a member function with has
the same signature (number and type of parameters) as the base class, then the derived class is
overriding the base class's member function.
Accessing the overridden function:
To access the overridden function of the base class from the derived class, scope resolution
operator:: is used. For example, If you want to access getData() function of the base class, you
can use the following statement in the derived class
IN-LAB TASKS
Task#01
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.
Code:
#include<iostream>
using namespace std;
class Simple
{
protected:
int n1,n2;
public:
Simple(){
n1=n2=0;
}
Simple(int x,int y){
n1=x;
n2=y;
}
void add(){
cout<<"\nSum is "<<n1+n2<<endl;
}
void mul(){
cout<<"\nproduct is "<<n1*n2<<endl;
}
void sub(){
cout<<"\ndifference is "<<n1-n2<<endl;
}
void div(){
cout<<"\nquotient is "<<n1/n2<<endl;
}
};
class Complex:public Simple
{
public:
Complex(int x,int y):Simple(x,y){
}
void add(){
if(n1>0&&n2>0){
Simple::add();
}
else{
cout<<"\n Error";
}
}
void mul(){
if(n1>0&&n2>0){
Simple::mul();
}
else{
cout<<"\n Error";
}
}void div(){
if(n1>0&&n2>0){
Simple::div();
}
else{
cout<<"\n Error";
}
}void sub(){
if(n1>0&&n2>0){
Simple::sub();
}
else{
cout<<"\n Error";
}
}
};
int main(){
int d,e;
cout<<"\nEnter two integers\n";
cin>>d>>e;
Complex C(d,e);
C.add();C.div();C.mul();C.sub();
}
console window
Task#02
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.
Code:
#include<iostream>
#include<string>
using namespace std;
class automobiles
{
protected:
string make;
int model;
int mileage;
int price;
public:
void get(){
cout<<"\ncar name";cin>>make;
cout<<"\nmodel ";cin>>model;
cout<<"\nmileage ";cin>>mileage;
cout<<"\nprice ";cin>>price;
}
void show(){
cout<<"\n"<< model<<" model "<<make <<" with mileage of
"<<mileage<<" and price is "<<price;
}
};
class cars:public automobiles
{
private:
int no_doors;
public:
void get(){
automobiles::get();
cout<<"\nnumber of doors are ";cin>>no_doors;
}
void show(){
automobiles::show();
cout<<" and number of doors are "<<no_doors;
}
};\
class truck:public automobiles
{
private:
string type;
public:
void get(){
automobiles::get();
cout<<"\nType of driving is ";cin>>type;
}
void show(){
automobiles::show();
cout<<" drive type is "<<type;
}
};
class SUV:public automobiles
{
private:
int capacity;
public:
void get(){
automobiles::get();
cout<<"\nseats are ";cin>>capacity;
}
void show(){
automobiles::show();
cout<<" capacity of passenger "<<capacity;
}
};
int main(){
SUV S;
cars C;
truck T;
C.get();T.get();S.get();
system("cls");
cout<<"\nWe have following cars in inventory :";
C.show();
cout<<"\nWe have following truck in inventory :";
T.show();
cout<<"\nWe have following SUVs in inventory :";
S.show();
}
console window
AFTER USING “CLS”
POST-LAB TASKS
Task# (statement)
Task description
Task code (with proper comments)
Code:
#include<iostream>
console window
Task#
Code:
#include<iostream>
console window
Conclusion:
NOTE:font: Times New Roman, Heading font 14, statement font 12