如何使用用户定义的值初始化对象数组并从用户那里获取输入?
How to initialize array of objects with user defined values and take input from user?
#include <iostream>
using namespace std;
class car{
string owner;
string car_num;
string issue_date;
car(string o, string cn, string id)
{
owner = o;
car_num = cn;
issue_date = id;
}
void getInfo()
{
cout << "Car's Owner's Name : " << owner << endl;
cout << "Cars' Number : " << car_num << endl;
cout << "Car's Issue Date : " << issue_date << endl;
}
};
int main()
{
int n;
cout << "Enter total number of cars stored in your garage : \n";
cin >> n;
car c1[n]; //incomplete code due to the issue
return 0;
}
这里我想从用户那里获取总车数。并且还想通过循环从用户那里获取汽车属性。但是我怎样才能在使用构造函数时做到这一点呢?
使用指针数组instead.e.g.
car* c1[n];
//incomplete part
for(int i = 0; i < n; i++){
//take input and process
c1[i] = new car(//put processed inputs here);
}
PS: 感觉哪里出错了,现在测试不了。如果不起作用,请在此处发表评论。
我的建议是不要过度使用构造函数。它应该构建,实际上应该只构建。在您的情况下,您甚至不需要构造函数。
而是添加一个新函数来进行初始化。
传统的是使用 operator >> 这通常是一个外部函数。
至于循环...
car c1[n]; //incomplete code due to the issue
不是合法的 C++(尽管它在 C 中是允许的,而且许多编译器也是 C 编译器)
最好使用矢量。所以...
vector<car> c1(n);
for (auto& c : c1)
cin >> c;
一种高级技术是使用 istream iterator,这将允许您使用像 std::copy 这样的算法,为向量的每个成员调用输入运算符。然而,这真的不是必需的,只是一个“nicety”
#include <iostream>
using namespace std;
class car{
public:
string owner;
string car_num;
string issue_date;
void cars(string o, string cn, string id)
{
owner = o;
car_num = cn;
issue_date = id;
getInfo();
}
void getInfo()
{
cout << "Car's Owner's Name : " << owner << endl;
cout << "Cars' Number : " << car_num << endl;
cout << "Car's Issue Date : " << issue_date << endl;
}
};
int main()
{
int n;
string a,b,c;
cout << "Enter total number of cars stored in your garage : \n";
cin >> n;
car cas[n]; //incomplete code due to the issue
for(int i=0;i<n;++i)
{
cout<<"value1:";
cin>>a;
cout<<"value2:";
cin>>b;
cout<<"value3:";
cin>>c;
cas[i].cars(a,b,c);
}
return 0;
}
您可以将循环与 std::cin 一起使用,例如 for(int i=0;i<n;++i){std::cin<<num;}
。我在代码中提到的'n'也可以通过std::cin
赋值
int n;
std::cin>>n;
car* cars=new car[n];
for(int i=0;i<n;++i)
{
std::getline(cars[i].owner,std::cin);
// and something other you'd like to do, like to test its validity
}
car c1[n]; //incomplete code due to the issue
事实上,你有两个问题:
- 标准 C++ 中不允许可变长度数组 (VLA)。他们
在标准 C 中可选地允许,并且被一些支持
C++ 编译器作为扩展。
- 你不能有一个对象数组w/o默认构造函数(除非你完全初始化它)。
假设您不想更改 class(除了在数据成员之后插入 public:
),现代解决方案应该使用 std::vector
:
std::vector<car> c;
//incomplete part
for(int i = 0; i < n; i++){
std::string owner, car_num, issue_date;
//TODO: get the strings from the user here ...
c.emplace_back(owner, car_num, issue_date);
}
#include <iostream>
using namespace std;
class car{
string owner;
string car_num;
string issue_date;
car(string o, string cn, string id)
{
owner = o;
car_num = cn;
issue_date = id;
}
void getInfo()
{
cout << "Car's Owner's Name : " << owner << endl;
cout << "Cars' Number : " << car_num << endl;
cout << "Car's Issue Date : " << issue_date << endl;
}
};
int main()
{
int n;
cout << "Enter total number of cars stored in your garage : \n";
cin >> n;
car c1[n]; //incomplete code due to the issue
return 0;
}
这里我想从用户那里获取总车数。并且还想通过循环从用户那里获取汽车属性。但是我怎样才能在使用构造函数时做到这一点呢?
使用指针数组instead.e.g.
car* c1[n];
//incomplete part
for(int i = 0; i < n; i++){
//take input and process
c1[i] = new car(//put processed inputs here);
}
PS: 感觉哪里出错了,现在测试不了。如果不起作用,请在此处发表评论。
我的建议是不要过度使用构造函数。它应该构建,实际上应该只构建。在您的情况下,您甚至不需要构造函数。
而是添加一个新函数来进行初始化。
传统的是使用 operator >> 这通常是一个外部函数。
至于循环...
car c1[n]; //incomplete code due to the issue
不是合法的 C++(尽管它在 C 中是允许的,而且许多编译器也是 C 编译器)
最好使用矢量。所以...
vector<car> c1(n);
for (auto& c : c1)
cin >> c;
一种高级技术是使用 istream iterator,这将允许您使用像 std::copy 这样的算法,为向量的每个成员调用输入运算符。然而,这真的不是必需的,只是一个“nicety”
#include <iostream>
using namespace std;
class car{
public:
string owner;
string car_num;
string issue_date;
void cars(string o, string cn, string id)
{
owner = o;
car_num = cn;
issue_date = id;
getInfo();
}
void getInfo()
{
cout << "Car's Owner's Name : " << owner << endl;
cout << "Cars' Number : " << car_num << endl;
cout << "Car's Issue Date : " << issue_date << endl;
}
};
int main()
{
int n;
string a,b,c;
cout << "Enter total number of cars stored in your garage : \n";
cin >> n;
car cas[n]; //incomplete code due to the issue
for(int i=0;i<n;++i)
{
cout<<"value1:";
cin>>a;
cout<<"value2:";
cin>>b;
cout<<"value3:";
cin>>c;
cas[i].cars(a,b,c);
}
return 0;
}
您可以将循环与 std::cin 一起使用,例如 for(int i=0;i<n;++i){std::cin<<num;}
。我在代码中提到的'n'也可以通过std::cin
int n;
std::cin>>n;
car* cars=new car[n];
for(int i=0;i<n;++i)
{
std::getline(cars[i].owner,std::cin);
// and something other you'd like to do, like to test its validity
}
car c1[n]; //incomplete code due to the issue
事实上,你有两个问题:
- 标准 C++ 中不允许可变长度数组 (VLA)。他们 在标准 C 中可选地允许,并且被一些支持 C++ 编译器作为扩展。
- 你不能有一个对象数组w/o默认构造函数(除非你完全初始化它)。
假设您不想更改 class(除了在数据成员之后插入 public:
),现代解决方案应该使用 std::vector
:
std::vector<car> c;
//incomplete part
for(int i = 0; i < n; i++){
std::string owner, car_num, issue_date;
//TODO: get the strings from the user here ...
c.emplace_back(owner, car_num, issue_date);
}