而是不言自明。我只是想知道在面向对象的 C++ 中更传统的做法是什么?
示例 A:
class CarObject{
private: //Local Variables
string name;
int cost;
public:
CarObject(string pName, int pCost){ //Constructor with parameters of car name and car cost
name = pName; //Sets the temporary variables to the private variables
cost = pCost;
}
void getDetails(){
cout << name;
cout << cost;
}//Public method that returns details of Car Object
};
示例 B:
class CarObject{
private: //Local Variables
string name;
int cost;
public:
CarObject(string pName, int pCost){ //Constructor with parameters of car name and car cost
name = pName; //Sets the temporary variables to the private variables
cost = pCost;
}
void getDetails(); //Public method that returns details of Car Object
};
void CarObject :: getDetails(){
cout << name;
cout << cost;
}