C++ 接口封装 调用
输入Keypad
Keypad头文件
#pragma once
class Keypad
{
public:
Keypad();
int getInput()const;
~Keypad();
};
Keypad源文件
#include "Keypad.h"
#include"Screen.h"
#include<iostream>
using namespace std;
Keypad::Keypad()
{
}
int Keypad::getInput() const
{
int input;
Screen screen;
screen.displayMessageLine("pls input amount:\t\n");
cin >> input;
return input;
}
Keypad::~Keypad()
{
}
输出 Screen
Screen头文件
#pragma once
using namespace std;
#include<string>
class Screen
{
public:
Screen();
void displayMessage(string)const;//输出消息
void displayMessageLine(string)const;//换行输出消息
void displayDollarAmount(double)const; // 输出金额
~Screen();
};
screen源文件
#include "Screen.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
Screen::Screen()
{
}
void Screen::displayMessage(string message) const
{
cout << message;
}
void Screen::displayMessageLine(string message) const
{
cout << message << endl;
}
void Screen::displayDollarAmount(double amount) const
{
cout << fixed << setprecision(2) << "$" << amount;
}
Screen::~Screen()
{
}
驱动测试或者客户端测试
#include<iostream>
#include"Screen.h"
#include"Keypad.h"
#include<stdlib.h>
using namespace std;
int main() {
Screen screen;
Keypad keypad;
screen.displayDollarAmount(keypad.getInput());
putchar(10);
screen.displayMessageLine("there is no choice but to grow!!");
screen.displayMessage("hello world");
system("pause");
}
posted on 2018-09-18 15:50 Indian_Mysore 阅读(999) 评论(0) 编辑 收藏 举报