Cplusplus Lab Sheet 04
Cplusplus Lab Sheet 04
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]
#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;
// Declare Pointers
char *pointerToChar;
int *pointerToInt;
float *pointerToFloat;
double *pointerToDouble;
long *pointerToLong;
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:
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;
}
};
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;
}
};
{
}
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;
}
};
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;
ptrToVehicle = &v1;
ptrToLightMotorVehicle = &lmv;
ptrToSuperUtilityVehicle =&suv;
v1.displayData();
lmv.displayData();
suv.displayData();
}
Output:
#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.