題目:
定義一個類別Cafeshop,有著帶著參數的成員函數(member function)
創一個物件(object)然後呼叫成員函數
程式碼:
#include <iostream>
#include <string>
using namespace::std;
// Cafeshop class definition
class Cafeshop
{
public:
// function that displays a welcome message to the Cafeshop customer
void displayMessage( string shopName)
{
cout << "Welcome to " << shopName << "'s Cafeshop!";
} // end function displayMessage
}; // end class Cafeshop
// function main begins program execution
int main()
{
string shopName; // string of characters to store the shop name
Cafeshop myCafeshop; // create a Cafeshop object named myCafeshop
// prompt for and input course name
cout << "Please enter the shop name:";
getline( cin, shopName ); // read a shop name with blanks
cout << endl; // output a blank line
// call myCafeshop's displayMessage function
// and pass shopName as an argument
myCafeshop.displayMessage( shopName );
system("PAUSE");
return 0; // indicate successful termination
} // end main
結果:
Please enter the shop name:Niki
Welcome to Niki's Cafeshop!
