c++ Complete the constructor
Complete the constructor of Car, child class of Vehicle, to correctly assign the parameters (m and y) to the attributes maker and year.
#include <iostream>
using namespace std;
class Vehicle {
private:
string maker;
int year;
public:
Vehicle(string m, int y):maker(m),year(y){};
string getMaker() { return maker; };
int getYear() { return year; };
};
class Car : public Vehicle {
private:
string model;
public:
Car(string m, int y, string md) //COMPLETE CONSTRUCTOR
{ model = md; };
string getModel() { return model; };
};
int main() {
return 0;
}
Ans
#include <iostream>
#include<string>
using namespace std;
class Vehicle {
private:
string maker;
int year;
public:
Vehicle(string m, int y):maker(m),year(y){};
string getMaker() { return maker; };
int getYear() { return year; };
};
class Car : public Vehicle {
private:
string model;
public:
Car(string m, int y, string md):Vehicle(m,y) //COMPLETE CONSTRUCTOR
{
model = md; };
string getModel() { return model; };
};
int main() {
return 0;
}
.
.
.The child class should call the parameterised constructor of base class by passing parameters before its constructor begins.
.
.
.If any doubt ask in the comments.
Please appreciate the work by giving a thumbs up.
Get Answers For Free
Most questions answered within 1 hours.