Question

write in C++ program and write an accessor that returns the value of the Car class’s...

write in C++ program and write an accessor that returns the value of the Car class’s make private member variable.

Given the following class definition:

class Car

{

public:

Car() : make {}, model {}, year {} {}

Car(std::string make, std::string model, int year) :

make {make},

model {model}

year {year}

{

}

private:

std::string make;

std::string model;

int year;

};

Homework Answers

Answer #1

string getMake() {
    return make;
}

#include <iostream>
#include <string>

using namespace std;

class Car {
public:
    Car() : make{}, model{}, year{} {}

    Car(std::string make, std::string model, int year) :
            make{make},
            model{model},
            year{year} {
    }

    string getMake() {
        return make;
    }
private:
    std::string make;
    std::string model;
    int year;
};

int main() {
    Car car("Ferrari", "F1", 2020);
    cout << car.getMake() << endl;
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class Auto { private String make; private String model; private int year; } a) write...
public class Auto { private String make; private String model; private int year; } a) write a default constructor for the Auto Class n) Write a constructor to initialize all instance variables c) write accessor and mutator for make variable d) create an onject name it myAuto with values of Toyota, Camry, and 2016
c++ Complete the constructor Complete the constructor of Car, child class of Vehicle, to correctly assign...
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;...
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
C++ program that Create a struct called car that has the following data members (variables): -...
C++ program that Create a struct called car that has the following data members (variables): - Color //color of the car - Model //model name of the car - Year //year the car was made - isElectric //whether the car is electric (true) or not (false) - topSpeed //top speed of the car, can be a decimal. code i have done struct not working properly. #include <iostream> using namespace std; struct Car { string color; string model_number; int year_model; bool...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...
Write a program in Java to have a Car class which is comparable. Make the Car...
Write a program in Java to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling...
Identify the errors in the following C# program. Write the correct program. Explain the errors. class...
Identify the errors in the following C# program. Write the correct program. Explain the errors. class Program { static Main(string[] args) { int x = 10 Console.WriteLine("{0}" + x); Console.ReadLine(); }
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function...
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function overloading. PSEUDOCODE: Declare necessary variables. Create a class with add(int,int) ,add(float,float) as member functions and necessary variable. add(int,int) is used to add two integer values. add(float,float) is used to add two float values. Using object call the required function with corresponding input. Display the output.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT