嗨,下面的代碼是 C 中的一個簡單類示例
#include <iostream>
using namespace std;
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
// W3Schools
在我看來,定義物件的方法是用如下代碼撰寫它: <ClassName> <ObjectName>
現在我的問題是:有沒有辦法輸入一個物件?喜歡cin >> ObjectName;
在我們輸入物件名稱后,我們輸入引數。
有可能嗎?
uj5u.com熱心網友回復:
您不能從命令列輸入物件名稱,但是,您可以創建一個默認構造的物件,然后通過多載operator>>.
class Car {
public:
Car() = default;
// ...
};
std::istream& operator>>(std::istream& in, Car& car) {
in >> car.brand >> car.model >> car.year;
return in;
}
int main() {
Car carObj1;
cout << "Enter a car brand, model, and year: ";
cin >> carObj1;
// ...
}
uj5u.com熱心網友回復:
是的,C 中有運算子多載,您可以多載 << 和 >> 運算子。這是一個示例代碼:
#include <iostream>
using namespace std;
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
Car() = default;
void Carinput(std::istream& is)
{
is >> brand >> model >> year;
}
void carOutput(std::ostream& os) const
{
os << brand << " " << model << " " << year << std::endl;
}
};
std::istream &operator>>(std::istream &is,Car &car)
{
car.Carinput(is);
return is;
}
std::ostream &operator<<(std::ostream &os,const Car &car)
{
car.carOutput(os);
return os;
}
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
std::cout << "\n*============================================*\n"
<< std::endl;
std::cout << "Your input:" << std::endl;
Car carObj3;
std::cin >> carObj3;
std::cout << carObj3;
return 0;
}
uj5u.com熱心網友回復:
這是這樣做的方法之一:
#include<iostream>
#include<vector>
class Car
{
public:
Car( const std::string& brand, const std::string& model, int year )
: m_brand( brand ), m_model( model ), m_year( year ) // initialize members like this
{
}
std::string m_brand;
std::string m_model;
int m_year;
};
int main ()
{
std::vector< Car > cars; // use an std::vector to store the objects
std::string brand( "" );
std::string model( "" );
std::string year( "" );
std::cout << "How many cars do you want to add? ";
std::string count( "" );
std::getline( std::cin, count ); // get input as string using std::getline()
int carCount { std::stoi( count ) }; // converts it to type int before
// assigning it to carCount
cars.reserve( carCount ); // reserve some space in vector to avoid
// unnecessary allocations and slowdowns
for ( int idx = 0; idx < carCount; idx )
{
std::cout << "Enter the brand name of car number " << idx 1 << ": ";
std::getline( std::cin, brand );
std::cout << "Enter the model name of car number " << idx 1 << ": ";
std::getline( std::cin, model );
std::cout << "Enter the production year of car number " << idx 1 << ": ";
std::getline( std::cin, year );
cars.emplace_back( Car( brand, model, std::stoi( year ) ) ); // push the new Car
} // instance to the
// vector called cars
std::cout << '\n';
for ( int idx = 0; idx < carCount; idx )
{
std::cout << "Info for car number " << idx 1 << ": "
<< cars[ idx ].m_brand << " " << cars[ idx ].m_model << " " << cars[ idx ].m_year << "\n";
}
return 0;
}
這是輸出:
How many cars do you want to add? 2
Enter the brand name of car number 1: Porsche
Enter the model name of car number 1: GT3 RS
Enter the production year of car number 1: 2019
Enter the brand name of car number 2: BMW
Enter the model name of car number 2: M6
Enter the production year of car number 2: 2016
Info for car number 1: Porsche GT3 RS 2019
Info for car number 2: BMW M6 2016
但是請記住,這個類仍然需要很多作業,例如添加成員函式(如 getter 和 setter)等。所以這不是我撰寫類的方式。我只是想保持簡單,以便您可以輕松理解這些概念。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363439.html
