一开始玩C++总问自己一个问题,一个cpp文件中只能有一个main函数,那如果有多个类、函数的定义该怎么办?冥思苦想,才反应过来,用头文件啊!.h文件中,做相应的定义、描述;.cpp文件中,做相应的连接、实现!
用一个例子来展示如何使用.h和.cpp完成一个小应用!
题目如下:
Pat.h文件里定义了一些类的函数定义及其实现,如下:
Pat.cpp文件将其串联起来,充当了GUI入口,如下:
看看控制台里输出了什么?
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
1
输入编号:0
输入宠物类型:
1-猫
2-狗
3-蛇
2
类型:狗
名字:小白
颜色:黑
食物:骨头
体重:30
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
3
输入编号:0
类型:狗
名字:小白
颜色:黑
食物:骨头
体重:30
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
4
宠物有:
猫:0只
狗:1只
蛇:0只
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
2
输入编号:0
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
5
用一个例子来展示如何使用.h和.cpp完成一个小应用!
题目如下:
/** * Annie的宠物小屋里有12个笼子,每个笼子可以放不同的动物, * 包括猫,狗和蛇,但同一时刻一个笼子只能放0只或1只宠物 * 本题要求完成: * (1)实现一个简单的管理系统, * 可以增加、删除指定笼子中的宠物, * 查询每个笼子中存放宠物的情况, * 统计宠物的种类和数量. * (2)定义描述宠物小屋的类Shelves, * 其中包括12个笼子用于存放各种宠物; * (3)定义虚拟基类Animal, * 至少包含纯虚函数showMe; * (4)定义派生类Cat,Dog,Snake, * 具体实现上述纯虚函数showMe, * 显示该宠物的种类,名称,着色,体重和喜欢的食物. * (5)重载输入>>操作符, * 使得可以通过cin直接读入宠物的着色,体重,和喜欢的食物; * (6)编写main函数, * 测上述所要求的各种功能; */
Pat.h文件里定义了一些类的函数定义及其实现,如下:
/* * Pat.h * * Created on: 2009-9-14 * Author: 梁栋 */ #ifndef PAT_H_ #define PAT_H_ #endif /* PAT_H_ */ #include <iostream> using namespace std; /** * 动物 */ class Animal { protected: // 类型 int type; // 名字 char * name; // 食物 char * food; // 颜色 char * color; // 体重 int weight; public: /** * 自我展示 */ virtual void showMe() = 0; /** * 自我清理 */ virtual void removeMe() = 0; }; /** * 猫 */ class Cat: public Animal { public: // 数量 static int num; /** * 初始化 */ Cat() { type = 1; name = new char[20]; food = new char[20]; color = new char[20]; weight = 0; num++; } /** * 操作符重载 */ friend istream & operator >>(istream& is, Cat& cat) { cout << "类型:猫" << endl; cout << "名字:"; cin >> cat.name; cout << "颜色:"; cin >> cat.color; cout << "食物:"; cin >> cat.food; cout << "体重:"; cin >> cat.weight; return is; } /** * 自我展示 */ void showMe() { cout << endl; cout << "类型:猫"; cout << endl; cout << "名字:" << name; cout << endl; cout << "颜色:" << color; cout << endl; cout << "食物:" << food; cout << endl; cout << "体重:" << weight; cout << endl; } /** * 自我清理 */ void removeMe() { num--; } /** * 获得数量 */ static int getNum() { return num; } }; int Cat::num = 0; /** * 狗 */ class Dog: public Animal { public: // 数量 static int num; /** * 初始化 */ Dog() { type = 2; name = new char[20]; food = new char[20]; color = new char[20]; weight = 0; num++; } /** * 操作符重载 */ friend istream & operator >>(istream& is, Dog& dog) { cout << "类型:狗" << endl; cout << "名字:"; cin >> dog.name; cout << "颜色:"; cin >> dog.color; cout << "食物:"; cin >> dog.food; cout << "体重:"; cin >> dog.weight; return is; } /** * 自我展示 */ void showMe() { cout << endl; cout << "类型:狗"; cout << endl; cout << "名字:" << name; cout << endl; cout << "颜色:" << color; cout << endl; cout << "食物:" << food; cout << endl; cout << "体重:" << weight; cout << endl; } /** * 自我清理 */ void removeMe() { num--; } /** * 获得数量 */ static int getNum() { return num; } }; int Dog::num = 0; /** * 蛇 */ class Snake: public Animal { public: // 数量 static int num; /** * 初始化 */ Snake() { type = 2; name = new char[20]; food = new char[20]; color = new char[20]; weight = 0; num++; } /** * 操作符重载 */ friend istream & operator >>(istream& is, Snake& snake) { cout << "类型:蛇" << endl; cout << "名字:"; cin >> snake.name; cout << "颜色:"; cin >> snake.color; cout << "食物:"; cin >> snake.food; cout << "体重:"; cin >> snake.weight; return is; } /** * 自我展示 */ void showMe() { cout << endl; cout << "类型:蛇"; cout << endl; cout << "名字:" << name; cout << endl; cout << "颜色:" << color; cout << endl; cout << "食物:" << food; cout << endl; cout << "体重:" << weight; cout << endl; } /** * 自我清理 */ void removeMe() { num--; } /** * 获得数量 */ static int getNum() { return num; } }; int Snake::num = 0; class Shelves { private: // 笼子 Animal * shelves[12]; public: /** * 初始化 */ Shelves() { for (int i = 0; i < 12; i++) { shelves[i] = NULL; } } /** * 追加 */ void add(int index, Animal * animal) { shelves[index] = animal; } /** * 清理 */ void remove(int index) { if (isExist(index)) { shelves[index]->removeMe(); delete shelves[index]; shelves[index] = NULL; } else { cout << "宠物No." << index << "不存在!" << endl << endl; } } /** * 查询 */ void query(int index) { if (isExist(index)) { shelves[index]->showMe(); } else { cout << "宠物No." << index << "不存在!" << endl << endl; } } /** * 统计 */ void stat() { cout << endl << "宠物有:" << endl; cout << "猫:" << Cat::getNum() << "只" << endl; cout << "狗:" << Dog::getNum() << "只" << endl; cout << "蛇:" << Snake::getNum() << "只" << endl << endl; } /** * 判断是否存在 */ int isExist(int index) { return (shelves[index] != NULL) ? 1 : 0; } };
Pat.cpp文件将其串联起来,充当了GUI入口,如下:
/* * Pat.cpp * * Created on: 2009-9-14 * Author: 梁栋 */ #include <iostream> #include "Pat.h" using namespace std; int main() { Shelves * shelves = new Shelves; Cat * cat; Dog * dog; Snake * snake; int choice = 0; // 操作选项 int index = 0; // 编号 int type = 0; // 类型 while (choice != 5) { cout << "--宠物管理系统--" << endl; cout << "===============" << endl; cout << "请选择你的操作:" << endl; cout << " 1.添加宠物" << endl; cout << " 2.删除宠物" << endl; cout << " 3.查询宠物" << endl; cout << " 4.统计宠物" << endl; cout << " 5.退出系统" << endl; cout << "===============" << endl; cin >> choice; switch (choice) { case 1: cout << "输入编号:"; cin >> index; if (shelves->isExist(index)) { cout << "宠物No." << index << "已存在!" << endl; break; } cout << "输入宠物类型:" << endl; cout << "1-猫" << endl; cout << "2-狗" << endl; cout << "3-蛇" << endl; cin >> type; switch (type) { case 1: cat = new Cat; cin >> *cat; shelves->add(index, cat); break; case 2: dog = new Dog; cin >> *dog; shelves->add(index, dog); break; case 3: snake = new Snake; cin >> *snake; shelves->add(index, snake); break; default: cout << "输入错误!" << endl; break; } break; case 2: cout << "输入编号:"; cin >> index; shelves->remove(index); break; case 3: cout << "输入编号:"; cin >> index; shelves->query(index); break; case 4: shelves->stat(); break; case 5: return 0; break; default: break; } } return 0; }
看看控制台里输出了什么?
引用
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
1
输入编号:0
输入宠物类型:
1-猫
2-狗
3-蛇
2
类型:狗
名字:小白
颜色:黑
食物:骨头
体重:30
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
3
输入编号:0
类型:狗
名字:小白
颜色:黑
食物:骨头
体重:30
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
4
宠物有:
猫:0只
狗:1只
蛇:0只
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
2
输入编号:0
--宠物管理系统--
===============
请选择你的操作:
1.添加宠物
2.删除宠物
3.查询宠物
4.统计宠物
5.退出系统
===============
5