0
我正在通過製作一個汽車程序來練習我的C++,它要求用戶輸入汽車的年份和型號,並顯示它們輸入的內容。現在它工作正常,但它不保存數據。我不在乎它是否會在程序關閉時消除,只是尋找一種將數據保存在數組中的方法,但我不知道如何使用getter和setter來實現該方法。C++汽車類如何用setter和getter實現數組
這裏是我的代碼
// 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;
}
任何幫助將appriciated
看看'的std :: VECTOR' http://en.cppreference.com/w/cpp/container/vector –
你的問題與getters或setter無關。 – immibis
受版權保護的代碼。 XD – dingalapadum