为什么我们需要在类下初始化int变量而不是其他变量。 谁能用普通的简单形式解释?
#include <iostream>
using namespace std;
class Car{
public:
string brand;
string name;
int year{};
};
int main() {
Car c1;
c1.brand = "Pagani";
c1.name = "Honda Zuyara R";
c1.year = 2015;
Car c2;
c2.brand = "Porche";
c2.name = "Carrera";
c2.year = 2003;
cout << "Chaman's Car " << c1.brand << ends <<c1.name << ends << c1.year << endl;
cout << "Mihir's Car " << c2.brand << ends <<c2.name << ends << c2.year << endl;
}