Create a class Car. Car has its data members speed and colour under private. Void speed(int car_speed) and void colour(string car_colour) are the member functions in class Car. Create one more class Audi. Audi wants to access the private data members of Class Car. Along with the inherited method, Audi has 5 airbags and so it has additional member function void air_bag_count( ). There is one more class Audi_a6 which is the latest version and inludes all the features of audi along with additional ones. It has additional vent for AC ...and so it has additional member function Car_ac( ) .Identify the type of inheritance..Create object for each class, supply value to all classes and display speed and colour for car, speed, colour and airbag details for audi. And car speed, colour, airbag detail, ac details for Audi_A6?
Question
Create a class Car. Car has its data members speed and colour under private. Void speed(int car_speed) and void colour(string car_colour) are the member functions in class Car. Create one more class Audi. Audi wants to access the private data members of Class Car. Along with the inherited method, Audi has 5 airbags and so it has additional member function void air_bag_count( ). There is one more class Audi_a6 which is the latest version and inludes all the features of audi along with additional ones. It has additional vent for AC ...and so it has additional member function Car_ac( ) .Identify the type of inheritance..Create object for each class, supply value to all classes and display speed and colour for car, speed, colour and airbag details for audi. And car speed, colour, airbag detail, ac details for Audi_A6?
Solution
This is an example of Multilevel Inheritance in C++. Here's how you can implement it:
#include<iostream>
#include<string>
using namespace std;
class Car {
private:
int speed;
string colour;
public:
void setSpeed(int car_speed) {
speed = car_speed;
}
void setColour(string car_colour) {
colour = car_colour;
}
int getSpeed() {
return speed;
}
string getColour() {
return colour;
}
};
class Audi : public Car {
private:
int airbags;
public:
void setAirbags(int count) {
airbags = count;
}
int getAirbags() {
return airbags;
}
};
class Audi_A6 : public Audi {
private:
int ac_vents;
public:
void setAcVents(int count) {
ac_vents = count;
}
int getAcVents() {
return ac_vents;
}
};
int main() {
Car myCar;
myCar.setSpeed(120);
myCar.setColour("Red");
cout << "Car speed: " << myCar.getSpeed() << ", colour: " << myCar.getColour() << endl;
Audi myAudi;
myAudi.setSpeed(150);
myAudi.setColour("Blue");
myAudi.setAirbags(5);
cout << "Audi speed: " << myAudi.getSpeed() << ", colour: " << myAudi.getColour() << ", airbags: " << myAudi.getAirbags() << endl;
Audi_A6 myAudiA6;
myAudiA6.setSpeed(180);
myAudiA6.setColour("Black");
myAudiA6.setAirbags(6);
myAudiA6.setAcVents(2);
cout << "Audi A6 speed: " << myAudiA6.getSpeed() << ", colour: " << myAudiA6.getColour() << ", airbags: " << myAudiA6.getAirbags() << ", AC vents: " << myAudiA6.getAcVents() << endl;
return 0;
}
In this code, we first create a class Car with private members speed and colour, and public member functions to set and get these values. We then create a class Audi which inherits from Car and adds an additional private member airbags with its own set and get functions. Finally, we create a class Audi_A6 which inherits from Audi and adds an additional private member ac_vents with its own set and get functions. In the main function, we create an object of each class, set their values, and display them.
Similar Questions
Dinesh is working in a supermarket and he is developing a program to calculate the cost of different types of items. Help him write the program that does the following:a) Create a base class, ItemType, with one virtual function double calculateAmount()b) Create a class called wooden that extends ItemType class with a number of items and cost as its private attributes. Obtain the data members and override the virtual function and calculate the total amount.c) Create a class called electronics that extends ItemType class with cost as its private attribute. Obtain the data member and override the virtual function and calculate the amount with 20% discount.Note: This question helps in clearing Infosys tests.Input format :The first line consists of an integer choice (1 or 2) representing the choice of item type.If the choice is 1 (wooden items), the next line consists of two space-separated integers: noOfItems and cost, representing the number of wooden items and their individual cost, respectively.If the choice is 2 (electronics), the next line consists of a single floating-point number cost, representing the cost of the electronic item.Output format :The output prints a floating-point number representing the calculated total cost of the chosen item type rounded off to two decimal places.Code constraints :10 < cost < 1060 < noOfItems < 20Sample test cases :Input 1 :15 840.5Output 1 :4202.50Input 2 :21800.56Output 2 :1440.45Note :
Identify the issue in this C++ code snippet:class Car {public:int speed;void Car() { speed = 0; }};a.Missing data encapsulationb.Incorrect class constructor syntaxc.No errord.Inheritance misuse
class Vehicle { protected int wheelsCount; private int doorsCount; public Vehicle(int wheelsCount, int doorsCount) { this.wheelsCount = wheelsCount; this.doorsCount = doorsCount; }}public class Truck extends Vehicle { private double loadCapacity; private int weight; public Truck(int wheelsCount, int doorsCount, double loadCapacity, int weight) { /* Missing Code */ }}
Design a C++ program to manage vehicles for a transport company. The program should handle three types of vehicles: cars, trucks, and buses. Implement a class with a base class and derived classes for each vehicle type. The base class, Vehicle, should have the following attributes: colour, mileage, top speed, and the number of gears. Each derived class should inherit from the Vehicle class and include additional attributes specific to the vehicle type.Car Class (Derived from Vehicle):Additional Attributes: number of doors and fuel type.Functionality: Implement a function to display car details, including all attributes inherited from the base class and the additional attributes specific to cars.Truck Class (Derived from Vehicle):Additional Attributes: cargo capacity and max load capacity.Functionality: Implement a function to display truck details, including all attributes inherited from the base class and the additional attributes specific to trucks.Bus Class (Derived from Vehicle):Additional Attributes: seating capacity and route number.Functionality: Implement a function to display bus details, including all attributes inherited from the base class and the additional attributes specific to buses.
The type Vehicle has drive functionality. The classes Car and Bike implements the drive functionality and can be further subclassed. Fill in the given code with appropriate access specifier so that the subclasses of Car and Bike do not modify the Drive functionality.interface Vehicle{ void drive();}class Car implements Vehicle{ final void drive() { //drive } }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.