简体   繁体   中英

C++ automobile class how to implement arrays with setter and getter

I am practicing my c++ by making an automobile program where it asks the user to input the year make and model of a car and it will display what they inputed. Right now it works fine but it is not saving the data. I dont care if it erases once the program closes im just looking for a way to save the data in an array but i dont know how to implement that using getter and setters.

here is my code

//  automobile.cpp
//  cplusplusproject
//
//  Created by Alexander  on 2/2/16.
//  Copyright © 2016 Alexander. All rights reserved.
//

#include <iostream>
#include <string>

using namespace std;

class automobile
{
public:
    // Constructor
    automobile(int year, string make, string model)
    {
        setcarYear(year);
        setcarMake(make);
        setcarModel(model);
    } //  end of constructor

    void setcarYear(int year)
    {
        carYear = year;
    }

    int getcarYear()
    {
        return carYear;
    }

    void setcarMake(string make)
    {
        carMake = make;
    }

    string getcarMake()
    {
        return carMake;
    }

    void setcarModel(string model)
    {
        carModel = model;
    }

    string getcarModel() { return carModel; }

    int carYear;
    string carMake;
    string carModel;
};

int main()
{
    int year;
    string make, model;
    cout << "Please enter the year of the car" << endl;
    cin >> year;

    cout << "Please enter the make of the car" << endl;
    cin >> make;

    cout << "Please enter the model of the car" << endl;
    cin >> model;

    automobile automobile(year, make, model);

    // display user info
    cout << automobile.getcarYear() << automobile.getcarMake()
         << automobile.getcarModel() << endl;
}

any help would be appriciated

std::vector<automobile> autos;
autos.push_pack(automobile)

this creates a vector and adds the autombile to it

To save your data into the array. You need a setter function for an array. Like:

void setArray(int index, dataType value){
      arrayName[index]= value;
}

or you need to take input in an array and then pass the complete array to the setter function as:

void setArray(dataType arr[]){
     for(int i=0;i<sizeOfArray;i++){
         arrayName[i] = arr[i];
     }
}

You can read more about getter and setters for arrays .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM