Respuesta :
Answer:
Here.
Explanation:
#include <iostream>
#include <string>
using namespace std;
//Create a class Car, which contains • Three data members i.e. carName (of string type), ignition (of bool type), and //currentSpeed (of integer type)
class Car
{
public:
string carName;
bool ignition;
int currentSpeed;
//A no-argument constructor to initialize all data members with default values
//default value of string is "",bool is false,int is 0
Car()
{
carName="";
ignition=false;
currentSpeed=0;
}
//A parameterized constructor to initialize all data members with user-defined values
Car(string name,bool i,int speed)
{
carName=name;
ignition=i;
currentSpeed=speed;
}
//Three setter functions to set values for all data members individually
// Three getter function to get value of all data members individually
void setCarName(string s)
{
carName=s;
}
void setIgnition(bool ig)
{
ignition=ig;
}
void setCurrentSpeed(int speed)
{
currentSpeed=speed;
}
string getCarName()
{
return carName;
}
bool getIgnition()
{
return ignition;
}
int getCurrentSpeed()
{
return currentSpeed;
}
//A member function setSpeed( ) // takes integer argument for setting speed
void setSpeed(int sp1)
{
currentSpeed=sp1;
}
};
//Derive a class named Convertible
class Convertible:public Car
{
//A data member top (of Boolean type)
public:
bool top;
public:
//A no-argument constructor to assign default value as “false” to top
Convertible()
{
top=false;
}
//A four argument constructor to assign values to all data-members i.e. carName, ignition,
//currentSpeed and top.
Convertible(string n,bool i,int s,bool t):Car(n,i,s)
{
carName=n;
ignition=i;
currentSpeed=s;
top=t;
}
// A setter to set the top data member up
void setTop(bool t)
{
top=t;
}
//A function named show() that displays all data member values of invoking object
void show()
{
cout<<"Car name is:"<<carName<<endl;
cout<<"Ignition is: "<<ignition<<endl;
cout<<"Current Speed is :"<<currentSpeed<<endl;
cout<<"Top is:"<<top<<endl;
}
};
//main function
int main()
{
//creating object for Convertible class
Convertible c1("Audi",true,100,true);
c1.show();
c1.setCarName("Benz");
c1.setIgnition(true);
c1.setCurrentSpeed(80);
c1.setTop(true);
c1.show();
cout<<"Car Name is: "<<c1.getCarName()<<endl;
cout<<"Ignition is:"<<c1.getIgnition()<<endl;
cout<<"Current Speed is:"<<c1.getCurrentSpeed()<<endl;
return 0;
}
The following are the program to the given question:
Program Explanation:
- Defining the header file.
- Defining a class "Car", inside the three variables "carName, ignition, and currentSpeed" is declared that are "string, bool, and integer" types.
- Inside the class, "default and parameterized constructor" and get and set method has defined that set and returns the parameter values.
- Outside the class, another class "Convertible" (child) is declared that inherit the base class "Car".
- Inside the child class, "default and parameterized constructor" is defined, which inherits the base class constructor.
- In the child class parameterized constructor it takes four parameters, in which 3 are inherited from the base class and one is bool type that is "t".
- In this class, a method "setTop" is defined that sets the "t" variable value and defines a show method that prints the variable values.
- Outside the class, the Main method is defined that creating the child class object and calling the parameterized constructor and other methods to print its values.
Program:
#include <iostream>//header file
#include <string>
using namespace std;
class Car//defining a class Car
{
//defining a data members
public:
string carName;//defining string variable
bool ignition;//defining bool variable
int currentSpeed;//defining integer variable
Car()//defining default constructor
{
carName="";//initiliaze a space value in string variable
ignition=false;//initiliaze a bool value in bool variable
currentSpeed=0;//initiliaze an integer value into int variable
}
Car(string name,bool i,int speed)//defining parameterized constructor that holds value into the variable
{
carName=name;//holding value in carName variable
ignition=i;//holding value in ignition variable
currentSpeed=speed;//holding value in currentSpeed variable
}
void setCarName(string s)//defining a set method that takes parameter to set value into the variable
{
carName=s;//set value
}
void setIgnition(bool ig)//defining a set method that takes a parameter to set value into the variable
{
ignition=ig;//set value
}
void setCurrentSpeed(int speed)//defining a set method that takes a parameter to set value into the variable
{
currentSpeed=speed;//set value
}
string getCarName()//defining a get method that return input value
{
return carName;//return value
}
bool getIgnition()//defining a get method that return input value
{
return ignition;//return value
}
int getCurrentSpeed()//defining a get method that return input value
{
return currentSpeed;//return value
}
void setSpeed(int sp1)//defining a method setSpeed that holds parameter value
{
currentSpeed=sp1;//hold value in currentSpeed
}
};
class Convertible:public Car//defining a class currentSpeed that inherits Car class
{
public:
bool top;//defining bool variable
public:
Convertible()//defining default constructor
{
top=false;//holing bool value
}
Convertible(string n,bool i,int s,bool t):Car(n,i,s)//defining parameterized constructor Convertible that inherits base class parameterized constructor
{
carName=n;//holding value in string variable
ignition=i;//holding value in bool variable
currentSpeed=s;//holding value in integer variable
top=t;//holding value in bool variable
}
void setTop(bool t)//defining a method setTop that takes parameter to set bool value
{
top=t;//holding bool value
}
void show()//defining show method that prints variable value
{
cout<<"Car name is:"<<carName<<"\n";//prints value
cout<<"Ignition is: "<<ignition<<"\n";//prints value
cout<<"Current Speed is :"<<currentSpeed<<"\n";//prints value
cout<<"Top is:"<<top<<"\n";//prints value
}
};
int main()//defining a main method
{
Convertible cs("BMW",true,180,true);//creating the Convertible calss object that calls the parameterized constructor by accepting value into the parameter
cs.show();//calling method show
cs.setCarName("Benz");//calling the setCarName method
cs.setIgnition(true);//calling the setIgnition method
cs.setCurrentSpeed(160);//calling setCurrentSpeed method
cs.setTop(true);//calling setTop method
cs.show();//calling show method
cout<<"Car Name is: "<<cs.getCarName()<<"\n";//calling the getCarName method and print its value
cout<<"Ignition is:"<<cs.getIgnition()<<"\n";//calling the getIgnition method and print its value
cout<<"Current Speed is:"<<cs.getCurrentSpeed();//calling the getCurrentSpeed method and print its value
return 0;
}
Output:
Please find the attached file.
Learn more:
brainly.com/question/23313563