[英]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. 我正在通过编写汽车程序来练习C ++,该程序要求用户输入汽车的年份和型号,并会显示他们输入的内容。 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. 我不在乎是否在程序关闭后立即擦除,只是在寻找一种将数据保存在数组中的方法,但我不知道如何使用getter和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 这将创建一个向量,并向其中添加autombile
To save your data into the array. 将数据保存到阵列中。 You need a setter function for an array. 您需要一个数组的setter函数。 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: 或者您需要在数组中输入,然后将完整的数组传递给setter函数,如下所示:
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 . 您可以阅读有关数组的getter和setter的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.