ticket machine
- ticket machines prints a ticket when a customer inserts the correct money for their fare
- our ticket machines work by customers inserting money into them ,and then requesting a ticket to be printed.a machine keeps a running total of the amount of money it has collected throughout its opration
规范:项目名全小写; 类名每个单词首字母大写;
每个类都应该有一个 .h 和一个 .cpp 。
在 .h 文件中声明类,在 .cpp 里面定义 .h 里面声明的实体。
TicketMachine.h
#pragma once
class TicketMachine
{
public:
void showPrompt(); // 函数的原型
void insertMoney(int money);
void getMoney();
void printTicket();
void showBalance();
void printError();
void showTotal();
private:
int price; // 数据
int balance;
int total;
};
TicketMachine.cpp
#include "TicketMachine.h"
#include <iostream>
using namespace std;
void TicketMachine::showPrompt() {
cout << "some thing!";
}
void TicketMachine::insertMoney(int money) {
balance += money;
}
void TicketMachine::showBalance() {
cout << balance;
}
main.cpp
#include "TicketMachine.h";
int main() {
TicketMachine tm;
tm.insertMoney(100);
tm.showBalance();
return 0;
}
输出: -858993360
:: 域的解析符
- 类名::函数名 类中的一个函数
- ::函数名
void S::f(){
::f();//woulde be recursive otherwise! 全局的函数
::a++;//select the aglobal a 全局的a
a--;//the a at class scope 类内的a
}