0% found this document useful (0 votes)
33 views

Cplusplus Lab Sheet 04

The document discusses a lab sheet on inheritance, virtual functions, and polymorphism in C++. It covers topics like defining inheritance, base and derived classes, types of inheritance, pointers to objects and derived classes, the "this" pointer, and runtime polymorphism using virtual functions. It then lists 10 programs that demonstrate these topics through examples involving pointers, inheritance, polymorphism, and abstract classes.

Uploaded by

abhindraabhi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Cplusplus Lab Sheet 04

The document discusses a lab sheet on inheritance, virtual functions, and polymorphism in C++. It covers topics like defining inheritance, base and derived classes, types of inheritance, pointers to objects and derived classes, the "this" pointer, and runtime polymorphism using virtual functions. It then lists 10 programs that demonstrate these topics through examples involving pointers, inheritance, polymorphism, and abstract classes.

Uploaded by

abhindraabhi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

PROGRAMMING IN C++ - CSE2036 LAB SHEET 04

Module 4 Inheritance, Lab Programming/Problem 08 Hours


Virtual evaluation/assignment Solving
Functions,
Polymorphism
Topics:
Inheritance, Pointers, Virtual Functions, Polymorphism:
Define inheritance, base and derived Classes, types of inheritance: Single, multilevel, multiple
inheritance, Multi-Path inheritance, Pointers to objects and derived classes, “this” pointer, Run
time polymorphism: Virtual functions and pure virtual functions.

For all the following program execution open-source GNU g++ compiler was used with suitable
Integrated Development Environment (IDE) such as Visual Studio Code on windows operating
system. For any queries about the content contact [email protected]

Table 1: Traceability between Programs and Topics

Program Number Topics Demonstrated


Program No 1 Revision of Pointers
Program No 2 Application of Pointers to solve problem
Program No 3 Pointers to objects
Program No 4 Simple Inheritance
Program No 5 Multilevel Inheritance
Program No 6 Multiple Inheritance
Program No 7 Pointers to objects and derived classes
Program No 8 This pointer
Program No 9 Polymorphism: Virtual Functions
Program No 10 Polymorphism: Pure Virtual Functions, Abstract
Classes.
1 Revision example on Pointers. Create pointers to various data types available in C++
and alter the pointed value using dereferencing operation. Then print the altered
values.

#include <iostream>
using namespace std;
int main()
{
// Declare Data Types
char a = 'a';
int b = 10;
float c = 10.10;
double d = 14.1234;
long e = 12345678;

//Print the initialized values as they are


cout << "a is " << a << endl;
cout << "b is " << b << endl;
cout << "c is " << c << endl;
cout << "d is " << d << endl;
cout << "e is " << e << endl;

// Declare Pointers
char *pointerToChar;
int *pointerToInt;
float *pointerToFloat;
double *pointerToDouble;
long *pointerToLong;

// Point the Pointers to Respective Data Types


pointerToChar = &a;
pointerToInt = &b;
pointerToFloat = &c;
pointerToDouble = &d;
pointerToLong = &e;

// Print size of data types.


cout << "size of char is: " << sizeof(char) << endl;
cout << "size of int is: " << sizeof(int) << endl;
cout << "size of float is: " << sizeof(float) << endl;
cout << "size of double is: " << sizeof(double) << endl;
cout << "size of long is: " << sizeof(long) << endl;

// Now print size of pointers. The value will be same.


cout << "size of char pointer is: " << sizeof(pointerToChar) << endl;
cout << "size of int pointer is: " << sizeof(pointerToInt) << endl;
cout << "size of float pointer is: " << sizeof(pointerToFloat) << endl;
cout << "size of int pointer is: " << sizeof(pointerToDouble) << endl;
cout << "size of float pointer is: " << sizeof(pointerToDouble) << endl;

// Dereference the pointers and assign different values.


*pointerToChar = 'b';
*pointerToInt = 15;
*pointerToFloat = 15.15;
*pointerToDouble = 14.87654321;
*pointerToLong = 87654321;

// Print the altered values of variables


cout << "Now a is " << a << endl;
cout << "Now b is " << b << endl;
cout << "Now c is " << c << endl;
cout << "Now d is " << d << endl;
cout << "Now e is " << e << endl;
}

Output:
2. Revision example on pointers. Item price of given item is 295 cents (100 cents is
1$). Prompt the user to enter the number of items he/she wants to buy. If the items
to be bought is greater than 25 units, give discount of 07%. Calculate the gross
purchase price. Solve exercise 02 using pointers.

// Dereferencing pointers
// Calculates the purchase price for a given quantity of items
#include <iostream>
#include <iomanip>
int main()
{
int unit_price = 295;
int count;
int discount_threshold = 25;
double discount = 0.07; // Item unit price in cents
// Number of items ordered
// Quantity threshold for discount
// Discount for quantities over discount_threshold
int *pcount = &count; // Pointer to count
std::cout << "Enter the number of items you want: ";
std::cin >> *pcount;
std::cout << "The unit price is " << std::fixed << std::setprecision(2)
<< "$" << unit_price / 100.0 << std::endl;
// Calculate gross price
int *punit_price = &unit_price; // Pointer to unit_price
int price = (*pcount * *punit_price); // Gross price via pointers
auto *pprice = &price; // Pointer to gross price
// Calculate net price in US$
double net_price;
double *pnet_price = nullptr;
pnet_price = &net_price;
if (*pcount > discount_threshold)
{
std::cout << "You qualify for a discount of "
<< static_cast<int>(discount * 100.0) << " percent.\n";
*pnet_price = price * (1.0 - discount) / 100.0;
}
else
{
net_price = *pprice / 100.0;
}
std::cout << "The net price for " << *pcount
<< " items is $" << net_price << std::endl;
}
Output when items > 25:
Output when items < 25:
3. Create a Class person with attributes { name, age, designation and salary}. Create
an array of objects of type person. Using pointer of type person iterate through the
array once setting the values accepted via input stream and then iterate again
displaying the set attributes of the objects. Array size can be assumed as 3.

#include <iostream>
using namespace std;
class Person
{
private:
std::string name;
int age;
std::string designation;
float salary;

public:

//set the data using setData


void setData()
{
cout << "Enter the name:" << endl;
cin >> name;
cout << "Enter the age:" << endl;
cin >> age;
cout << "Enter the designation:" << endl;
cin >> designation;
cout << "Enter the salary:" << endl;
cin >> salary;
}

//Display the data using displayDetails


void displayDetails()
{
cout << " Name of the employee is: " << name << endl;
cout << " Age of the employee is: " << age << endl;
cout << " Designation of the employee is: " << designation << endl;
cout << " Salary of the employee is: " << salary << endl;
}
};
int main()
{
int i = 0;
Person listofPersons[3];
Person *pointerToPerson;
pointerToPerson = listofPersons;
//Iterate through the array setting the values
for (i = 0; i < 3; i++)
{
pointerToPerson->setData();
pointerToPerson++;
}
// Reinitialize the pointer to start of array again
// display data of each object of array
pointerToPerson = listofPersons;
for (i = 0; i < 3; i++)
{
pointerToPerson->displayDetails();
pointerToPerson++;
}
}

Output:
4. WAP to demonstrate inheritance. In the program Light Motor Vehicle should be
inherited from Class Vehicle. Vehicle should have attributes { Make, Year of
manufacture, Registration number, Mileage}. The inherited class Light Motor Vehicle
should have additional attribute {Infant Passengers Count}. Both base and inherited
classes should have constructors to set the values and also they should have setData()
functions. There should be displayData() function in base and derived classes to
display the attribute values.

#include <iostream>
using namespace std;
class Vehicle
{
protected:
std::string make;
int yearOfManufature;
std::string registrationNum;
float mileage;

public:
Vehicle(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal, float mileageVal)
: make{makeVal}, yearOfManufature{yearOfManufatureVal},
registrationNum{registrationNumVal}, mileage{mileageVal}
{
}
void setData(std::string makeVal, int yesrOfManufatureVal, std::string
registrationNumVal, float mileageVal)
{
make = makeVal;
yearOfManufature = yesrOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
}
void displayData()
{
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufature is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
}
};
class LightMotorVehicle : public Vehicle
{
int infantPassengers;

public:
LightMotorVehicle(std::string makeVal, int yesrOfManufatureVal,
std::string registrationNumVal, float mileageVal, int
infantPassengerVal)
: Vehicle{makeVal, yesrOfManufatureVal, registrationNumVal,
mileageVal}, infantPassengers{infantPassengerVal}
{
}
void setData(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal, float mileageVal, int infantPassengerVal)
{
make = makeVal;
yearOfManufature = yearOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
infantPassengers = infantPassengerVal;
}
void displayData()
{
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufacture is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
cout << "The number of infants allowed: " << infantPassengers << endl;
}
};

int main()
{
Vehicle v1("TVS", 1999, "KA02C1234", 70.0);
LightMotorVehicle lmv("TataIndigo", 2013, "KA04C3922", 13.5, 2);
v1.displayData();
lmv.displayData();
v1.setData("Honda", 2003, "KA04C1564", 90);
lmv.setData("HondaCivic", 2009, "KA05C5678", 23, 2);
v1.displayData();
lmv.displayData();
}

Output:
5 WAP to demonstrate Multi Level Inheritance. In the program Light Motor Vehicle
should be inherited from Class Vehicle. Super Utility Vehicle should inherit from Light
Motor Vehicle. Vehicle should have attributes { Make, Year of manufacture,
Registration number, Mileage}. The inherited class Light Motor Vehicle should have
additional attribute {Infant Passengers Count}. The Super Utility Vehicle derived from
Light Motor Vehicle should have {Infotainment-Manufacturer}. All three classes
should have constructors to set the values and also they should have setData()
functions. There should be displayData() function in all three classes to display the
attribute values.
#include <iostream>
using namespace std;
class Vehicle
{
protected:
std::string make;
int yearOfManufature;
std::string registrationNum;
float mileage;

public:
Vehicle(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal, float mileageVal)
: make{makeVal}, yearOfManufature{yearOfManufatureVal},
registrationNum{registrationNumVal},
mileage{mileageVal}
{
}
void setData(std::string makeVal, int yesrOfManufatureVal, std::string
registrationNumVal, float mileageVal)
{
make = makeVal;
yearOfManufature = yesrOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
}
void displayData()
{
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufature is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
}
};
class LightMotorVehicle : public Vehicle
{
protected:
int infantPassengers;
public:
LightMotorVehicle(std::string makeVal, int yesrOfManufatureVal,
std::string registrationNumVal, float mileageVal, int
infantPassengerVal)
: Vehicle{makeVal, yesrOfManufatureVal, registrationNumVal,
mileageVal},
infantPassengers{infantPassengerVal}
{
}
void setData(std::string makeVal, int yearOfManufatureVal,
std::string registrationNumVal, float mileageVal, int
infantPassengerVal)
{
make = makeVal;
yearOfManufature = yearOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
infantPassengers = infantPassengerVal;
}
void displayData()
{
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufacture is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
cout << "The number of infants allowed: " << infantPassengers << endl;
}
};

class SuperUtilityVehicle : public LightMotorVehicle


{
std::string infotainmentManufacturer;

public:
SuperUtilityVehicle(std::string makeVal, int yesrOfManufatureVal,
std::string registrationNumVal, float mileageVal, int
infantPassengerVal,
std::string infotainmentManufacturer)
: LightMotorVehicle{makeVal, yesrOfManufatureVal, registrationNumVal,
mileageVal, infantPassengerVal},
infotainmentManufacturer{infotainmentManufacturer}

{
}
void setData(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal,
float mileageVal, int infantPassengerVal, std::string
infotainmentManufacturer)
{
make = makeVal;
yearOfManufature = yearOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
infantPassengers = infantPassengerVal;
}
void displayData()
{
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufacture is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
cout << "The number of infants allowed: " << infantPassengers << endl;
cout << "Infotainment Manufacturer is: " << infotainmentManufacturer
<< endl;
}
};

int main()
{
Vehicle v1("TVS", 1999, "KA02C1234", 70.0);
LightMotorVehicle lmv("TataIndigo", 2013, "KA04C3922", 13.5, 2);
SuperUtilityVehicle suv("HondaCRV", 2021, "KA049742", 8.5, 2, "Harman");
v1.displayData();
lmv.displayData();
suv.displayData();
v1.setData("Honda", 2003, "KA04C1564", 90);
lmv.setData("HondaCivic", 2009, "KA05C5678", 23, 2);
suv.setData("Mercedez", 2021, "KA041678", 8, 2, "Harman");
v1.displayData();
lmv.displayData();
suv.displayData();
}

Output:
6 WAP to demonstrate Multiple Inheritance. In the program Super Utility Vehicle
should inherit from Class Vehicle and Infotainment Manufacturer. Vehicle should have
attributes {Make, Year of manufacture, Registration number, Mileage}. Infotainment
Class should have additional attribute {Infotainment Manufacturer}. Super Utility
Vehicle should give the option to set the values either through constructor or through
setData(). Super Utility Vehicle Class should have displayData() to display the
attributes inherited using multiple inheritance.
#include <iostream>
using namespace std;
class Vehicle
{
protected:
std::string make;
int yearOfManufature;
std::string registrationNum;
float mileage;

public:
Vehicle(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal, float mileageVal)
: make{makeVal}, yearOfManufature{yearOfManufatureVal},
registrationNum{registrationNumVal},
mileage{mileageVal}
{
}
void setData(std::string makeVal, int yesrOfManufatureVal, std::string
registrationNumVal, float mileageVal)
{
make = makeVal;
yearOfManufature = yesrOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
}
void displayData()
{
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufature is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
}
};
class InfotainmentManufacturer
{
protected:
std::string infotainmentManufacturer;

public:
InfotainmentManufacturer(std::string infotainmentManufacturerVal)
: infotainmentManufacturer{infotainmentManufacturerVal}
{
}
void setData(std::string infotainmentManufacturerVal)
{
infotainmentManufacturer = infotainmentManufacturerVal;
}
void displayData()
{
cout << "Infotainment Manufacturer Is: " << infotainmentManufacturer
<< endl;
}
};

class SuperUtilityVehicle : public Vehicle, InfotainmentManufacturer


{
public:
SuperUtilityVehicle(std::string makeVal, int yesrOfManufatureVal,
std::string registrationNumVal, float mileageVal,
std::string infotainmentManufacturer)
: Vehicle{makeVal, yesrOfManufatureVal, registrationNumVal,
mileageVal},
InfotainmentManufacturer{infotainmentManufacturer}

{
}
void setData(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal,
float mileageVal, std::string infotainmentManufacturerVal)
{
make = makeVal;
yearOfManufature = yearOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
infotainmentManufacturer = infotainmentManufacturerVal;
}
void displayData()
{
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufacture is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
cout << "Infotainment Manufacturer is: " << infotainmentManufacturer
<< endl;
}
};
int main()
{
SuperUtilityVehicle suv("HondaCRV", 2021, "KA049742", 8.5, "Harman");
suv.displayData();
suv.setData("Mercedez", 2021, "KA041678", 8, "Harman");
suv.displayData();
}

Output:
7 WAP to demonstrate Pointers to objects and Derived Classes. In the program Light
Motor Vehicle should be inherited from Class Vehicle. Super Utility Vehicle should
inherit from Light Motor Vehicle. Vehicle should have attributes { Make, Year of
manufacture, Registration number, Mileage}. The inherited class Light Motor Vehicle
should have additional attribute {Infant Passengers Count}. The Super Utility Vehicle
derived from Light Motor Vehicle should have {Infotainment-Manufacturer}. All three
classes should have constructors to set the values and also they should have setData()
functions. There should be displayData() function in all three classes to display the
attribute values. Create pointers to level 0 class, level 1 and level 2 class. Using
Pointers alter the contents and then print the altered values of objects.
#include <iostream>
using namespace std;
class Vehicle
{
protected:
std::string make;
int yearOfManufature;
std::string registrationNum;
float mileage;

public:
Vehicle(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal, float mileageVal)
: make{makeVal}, yearOfManufature{yearOfManufatureVal},
registrationNum{registrationNumVal},
mileage{mileageVal}
{
}
void setData(std::string makeVal, int yesrOfManufatureVal, std::string
registrationNumVal, float mileageVal)
{
make = makeVal;
yearOfManufature = yesrOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
}
void displayData()
{
cout<<"Inside Vehicle Display Data"<<endl;
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufature is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
cout<<endl;
}
};
class LightMotorVehicle : public Vehicle
{
protected:
int infantPassengers;

public:
LightMotorVehicle(std::string makeVal, int yesrOfManufatureVal,
std::string registrationNumVal, float mileageVal, int
infantPassengerVal)
: Vehicle{makeVal, yesrOfManufatureVal, registrationNumVal,
mileageVal},
infantPassengers{infantPassengerVal}
{
}
void setData(std::string makeVal, int yearOfManufatureVal,
std::string registrationNumVal, float mileageVal, int
infantPassengerVal)
{
make = makeVal;
yearOfManufature = yearOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
infantPassengers = infantPassengerVal;
}
void displayData()
{
cout << "Inside Light Motor Vehicle Display Data" <<endl;
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufacture is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
cout << "The number of infants allowed: " << infantPassengers << endl;
cout<<endl;
}
};

class SuperUtilityVehicle : public LightMotorVehicle


{
std::string infotainmentManufacturer;

public:
SuperUtilityVehicle(std::string makeVal, int yesrOfManufatureVal,
std::string registrationNumVal, float mileageVal, int
infantPassengerVal,
std::string infotainmentManufacturer)
: LightMotorVehicle{makeVal, yesrOfManufatureVal, registrationNumVal,
mileageVal, infantPassengerVal},
infotainmentManufacturer{infotainmentManufacturer}
{
}
void setData(std::string makeVal, int yearOfManufatureVal, std::string
registrationNumVal,
float mileageVal, int infantPassengerVal, std::string
infotainmentManufacturer)
{
make = makeVal;
yearOfManufature = yearOfManufatureVal;
registrationNum = registrationNumVal;
mileage = mileageVal;
infantPassengers = infantPassengerVal;
}
void displayData()
{
cout << "Inside Super Utility Vehicle Display Data" <<endl;
cout << "Make of the vehicle is: " << make << endl;
cout << "Year of manufacture is: " << yearOfManufature << endl;
cout << "Registration number is: " << registrationNum << endl;
cout << "Mileage is: " << mileage << endl;
cout << "The number of infants allowed: " << infantPassengers << endl;
cout << "Infotainment Manufacturer is: " << infotainmentManufacturer
<< endl;
cout<<endl;
}
};

int main()
{
Vehicle *ptrToVehicle;
LightMotorVehicle *ptrToLightMotorVehicle;
SuperUtilityVehicle *ptrToSuperUtilityVehicle;

Vehicle v1("TVS", 1999, "KA02C1234", 70.0);


LightMotorVehicle lmv("TataIndigo", 2013, "KA04C3922", 13.5, 2);
SuperUtilityVehicle suv("HondaCRV", 2021, "KA049742", 8.5, 2, "Harman");
v1.displayData();
lmv.displayData();
suv.displayData();

ptrToVehicle = &v1;
ptrToLightMotorVehicle = &lmv;
ptrToSuperUtilityVehicle =&suv;

ptrToVehicle->setData("Honda", 2003, "KA04C1564", 90);


ptrToLightMotorVehicle->setData("HondaCivic", 2009, "KA05C5678", 23, 2);
ptrToSuperUtilityVehicle-> setData("Mercedez", 2021, "KA041678", 8, 2,
"Harman");

v1.displayData();
lmv.displayData();
suv.displayData();
}

Output:

Inside Vehicle Display Data


Make of the vehicle is: TVS
Year of manufature is: 1999
Registration number is: KA02C1234
Mileage is: 70

Inside Light Motor Vehicle Display Data


Make of the vehicle is: TataIndigo
Year of manufacture is: 2013
Registration number is: KA04C3922
Mileage is: 13.5
The number of infants allowed: 2

Inside Super Utility Vehicle Display Data


Make of the vehicle is: HondaCRV
Year of manufacture is: 2021
Registration number is: KA049742
Mileage is: 8.5
The number of infants allowed: 2
Infotainment Manufacturer is: Harman

Inside Vehicle Display Data


Make of the vehicle is: Honda
Year of manufature is: 2003
Registration number is: KA04C1564
Mileage is: 90

Inside Light Motor Vehicle Display Data


Make of the vehicle is: HondaCivic
Year of manufacture is: 2009
Registration number is: KA05C5678
Mileage is: 23
The number of infants allowed: 2

Inside Super Utility Vehicle Display Data


Make of the vehicle is: Mercedez
Year of manufacture is: 2021
Registration number is: KA041678
Mileage is: 8
The number of infants allowed: 2
Infotainment Manufacturer is: Harman
8 Create Vehicle Class with attributes { Manufacturer, YearOfManufature} then inherit
the Vehicle class to create Light Motor Vehicle and Heavy Motor Vehicle Classes
respectively. Light Motor Vehicle should have additional attribute {InfantsAllowed}
and Heavy Motor Vehicle should have attribute {CargoCapacity}. WAP using this
pointer in this case.

#include <iostream>
using namespace std;
class vehicle
{
protected:
std::string manufacturer;
int yearOfManufacture;

public:
virtual void setData(std::string manfacturerVal, int yearOfManuctureVal)
{
this->manufacturer = manfacturerVal;
this->yearOfManufacture = yearOfManuctureVal;
}
virtual void displayData()
{
cout << "Year of Manufacture is: " << this->yearOfManufacture << endl;
}
};
class lightMotorVehicle : public vehicle
{
int infantsAllowed;

public:
void setData(std::string manufacturerVal, int yearOfManufactureVal, int
infantsAllowedVal)
{
this->manufacturer = manufacturerVal;
this->yearOfManufacture = yearOfManufactureVal;
this->infantsAllowed = infantsAllowedVal;
}
void displayData()
{
cout << "Manufacturer is: " << this->manufacturer << endl;
cout << "Year of Manufacture is: " << this->yearOfManufacture << endl;
cout << "Infants Allowed is: " << this->infantsAllowed << endl;
}
};
class heavyMotorVehicle : public vehicle
{
int cargoCapacity;
public:
void setData(std::string manfaturerVal, int yearOfManufactureVal, int
cargoCapacityAllowed)
{
this->manufacturer = manfaturerVal;
this->yearOfManufacture = yearOfManufactureVal;
this->cargoCapacity = cargoCapacityAllowed;
}
void displayData()
{
cout << "Manufacturer is: " << this->manufacturer << endl;
cout << "Year of Manufacture is: " << this->yearOfManufacture << endl;
cout << "Cargo Capacity Allowed" << this->cargoCapacity << endl;
}
};
int main()
{
lightMotorVehicle lmv;
lmv.setData((std::string) "Honda", 1999, 2);
heavyMotorVehicle hmv;
hmv.setData((std::string) "TATA", 2023, 10000);

lmv.displayData();
hmv.displayData();
}

Output:
9 Create Vehicle Class with attributes { Manufacturer, YearOfManufature} then inherit
the Vehicle class to create Light Motor Vehicle and Heavy Motor Vehicle Classes
respectively. Light Motor Vehicle should have additional attribute {InfantsAllowed}
and Heavy Motor Vehicle should have attribute {CargoCapacity}. WAP to demonstrate
Run Time Polymorphism using virtual function concepts.

#include <iostream>
using namespace std;
class vehicle
{
protected:
std::string manufacturer;
int yearOfManufacture;

public:
virtual void setData(std::string manfacturerVal, int yearOfManuctureVal)
{
manufacturer = manfacturerVal;
yearOfManufacture = yearOfManuctureVal;
}
virtual void displayData()
{
cout << "Year of Manufacture is: " << yearOfManufacture << endl;
}
};
class lightMotorVehicle : public vehicle
{
int infantsAllowed;

public:
void setData(std::string manufacturerVal, int yearOfManufactureVal, int
infantsAllowedVal)
{
manufacturer = manufacturerVal;
yearOfManufacture = yearOfManufactureVal;
infantsAllowed = infantsAllowedVal;
}
void displayData()
{
cout << "Manufacturer is: " << manufacturer << endl;
cout << "Year of Manufacture is: " << yearOfManufacture << endl;
cout << "Infants Allowed is: " << infantsAllowed << endl;
}
};
class heavyMotorVehicle : public vehicle
{
int cargoCapacity;
public:
void setData(std::string manfaturerVal, int yearOfManufactureVal, int
cargoCapacityAllowed)
{
manufacturer = manfaturerVal;
yearOfManufacture = yearOfManufactureVal;
cargoCapacity = cargoCapacityAllowed;
}
void displayData()
{
cout << "Manufacturer is: " << manufacturer << endl;
cout << "Year of Manufacture is: " << yearOfManufacture << endl;
cout << "Cargo Capacity Allowed" << cargoCapacity << endl;
}
};
int main()
{
vehicle *ptrlmv;
vehicle *ptrhmv;

lightMotorVehicle lmv;
lmv.setData((std::string) "Honda", 1999, 2);
heavyMotorVehicle hmv;
hmv.setData((std::string) "TATA", 2023, 10000);

ptrlmv = &lmv;
ptrhmv = &hmv;

ptrlmv->displayData();
ptrhmv->displayData();
}

Output:
10. Create Vehicle Class with attributes { Manufacturer, YearOfManufature} then
inherit the Vehicle class to create Light Motor Vehicle and Heavy Motor Vehicle
Classes respectively. Light Motor Vehicle should have additional attribute
{InfantsAllowed} and Heavy Motor Vehicle should have attribute {CargoCapacity}.
WAP to create a pure virtual function maxSpeed() in base class. Also demonstrate that
C++ compiler does not allow you to instantiate abstract class containing pure virtual
function.

#include <iostream>
using namespace std;
class vehicle
{
protected:
std::string manufacturer;
int yearOfManufacture;

public:
virtual void maxSpeed() = 0;
virtual void setData(std::string manfacturerVal, int yearOfManuctureVal)
{
manufacturer = manfacturerVal;
yearOfManufacture = yearOfManuctureVal;
}
virtual void displayData()
{
cout << "Year of Manufacture is: " << yearOfManufacture << endl;
}
};
class lightMotorVehicle : public vehicle
{
int infantsAllowed;

public:
void maxSpeed()
{
cout << "Max speed is 140 km/hr" << endl;
}
void setData(std::string manufacturerVal, int yearOfManufactureVal, int
infantsAllowedVal)
{
manufacturer = manufacturerVal;
yearOfManufacture = yearOfManufactureVal;
infantsAllowed = infantsAllowedVal;
}
void displayData()
{
cout << "Manufacturer is: " << manufacturer << endl;
cout << "Year of Manufacture is: " << yearOfManufacture << endl;
cout << "Infants Allowed is: " << infantsAllowed << endl;
}
};
class heavyMotorVehicle : public vehicle
{
int cargoCapacity;

public:
void maxSpeed()
{
cout << "Max speed is 70 km/hr" << endl;
}
void setData(std::string manfaturerVal, int yearOfManufactureVal, int
cargoCapacityAllowed)
{
manufacturer = manfaturerVal;
yearOfManufacture = yearOfManufactureVal;
cargoCapacity = cargoCapacityAllowed;
}
void displayData()
{
cout << "Manufacturer is: " << manufacturer << endl;
cout << "Year of Manufacture is: " << yearOfManufacture << endl;
cout << "Cargo Capacity Allowed" << cargoCapacity << endl;
}
};
int main()
{
vehicle *ptrlmv;
vehicle *ptrhmv;

// vehicle vh1; If you uncomment this you should get compilation error as
abstract classes
// cannot be instantiated.

lightMotorVehicle lmv;
lmv.setData((std::string) "Honda", 1999, 2);
heavyMotorVehicle hmv;
hmv.setData((std::string) "TATA", 2023, 10000);

ptrlmv = &lmv;
ptrhmv = &hmv;

ptrlmv->displayData();
ptrlmv->maxSpeed();
ptrhmv->displayData();
ptrhmv->maxSpeed();
}

Output:

Assignment:
The assignment for this LAB sheet 04 is, solve similar problems as outlined in this pdf from preferred
sources such as Books, Public Blogs, Public Videos or any other reliable source and understand and
implement the concepts.

You might also like