close

1. 宣告函數標頭檔 drink.h

#include <string>

/* 咖啡店點兩杯飲料 */
 
using std::string;
 
class Cafeshop
{
    public:
    Cafeshop(string);
    void menu(string);
    string tellservice();
    void printreceipt();
private:
    string makedrink;
};
2.   class物件內容
#include <iostream>
#include "drink.h"
using namespace::std;
 
Cafeshop::Cafeshop(string drink)
{
menu(drink);
}
 
void Cafeshop::menu(string drink)
{
makedrink = drink;
}
 
string Cafeshop::tellservice()
{
return makedrink;
}
void Cafeshop::printreceipt()
{
cout << "Welcome to the Niki cafe shop!You drinks:" << tellservice() << endl;
}


3. 主程式

#include <iostream>
#include "drink.h"
 
using namespace::std;
 
int main()
{
Cafeshop Nikilike("Niki want drinks cafe!");
 
Cafeshop Misalike("Misa want drinks Mocha!");
 
cout << "Which kind drink you want?" << endl
<< Nikilike.tellservice() << endl
<< Misalike.tellservice() << endl;
Nikilike.printreceipt();
Misalike.printreceipt();
 
system("PAUSE");
return 0;
}

 

顯示結果:

Which kind drink you want

Niki want drinks cafe!

Misa want drinks Mocha!

Welcome to the Niki cafe shop!You drinks:Niki want drinks cafe!

Welcome to the Niki cafe shop!Misa want drinks Mocha!

 

 

arrow
arrow
    文章標籤
    c++
    全站熱搜

    布拉怡 發表在 痞客邦 留言(0) 人氣()