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...
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.
Program C++ (use visual studio) Q1. What default copy constructor does the compiler insert in the...
Program C++ (use visual studio) Q1. What default copy constructor does the compiler insert in the following class? class Student { string name; string id; double grade; }; =========================== Q2 .What is the factor transfer method used when the f() function is called from? void f(int n[]); int main() { int m[3]= {1, 2, 3}; f(m); } ================================== Q3. Write a program that produces a bigger() with a prototype as shown below and outputs a large value by inputting two...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT