執行 ❯
建立您
自己的
網站
×
更改方向
更改主題,深色/淺色
前往 Spaces
#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 declaration }; // Constructor definition outside the class Car::Car(string x, string y, int z) { 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; }
寶馬 X5 1999
福特野馬 1969