第十三章编程练习(3)
2016-02-26 15:32
375 查看
brass.h
brass.cpp
main.cpp
#include "brass.h"
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
Brass *pd[3];//虚基类不能创建对象,但可以创建指向其的指针
for (int i = 0; i<3; i++)
{
cout << "Enter the label:";
string label;
getline(cin, label);
cout << "Enter the rating:";
int rat;
cin >> rat;
cout << "Enter the 1 for baseDMA" << endl
<< "2 for lacksDMA" << endl
<< "3 for hasDMA" << endl;
int temp;
(cin >> temp).get();
if (temp == 1)
pd[i] = new baseDMA(label,rat);
else if (temp == 2)
{
cout << "Enter the color:";
string color;
getline(cin, color,'\n');
pd[i]=new lacksDMA (color,label,rat);
}
else if (temp == 3)
{
cout << "Enter the style:";
string style;
getline(cin, style,'\n');
pd[i] = new hasDMA(style,label ,rat);
}
else
{
cout << "invalid input! try again!" << endl;
i--;
}
while (cin.get() != '\n')
continue;
}
cout << endl;
for (int i = 0; i < 3; i++)
pd[i]->show();
for (int i = 0; i < 3; i++)
delete pd[i];
system("pause");
return 0;
}
#pragma once #ifndef BRASS_H_ #define BRASS_H_ #include <string> class Brass{ private: std::string label; int rating; public: Brass(const std::string l = "null", int r = 0); Brass(const Brass & r); virtual void test() = 0 {};//纯虚方法,是定义ABC模型的特征标 virtual ~Brass(); //friend std::ostream & operator<<(std::ostream & os, const Brass & r); std::string lab() { return label; }; int ra() { return rating; }; virtual void show(); }; class baseDMA :public Brass { public: baseDMA(const std::string = "null", int r = 0); baseDMA(baseDMA & rs); ~baseDMA(); virtual void test() {}; //friend std::ostream &operator<<(std::ostream & os, const baseDMA &rs); virtual void show(); }; class lacksDMA :public Brass { private: std::string color; public: lacksDMA(const std::string = "blank", const std::string = "null", int r = 0); virtual void test() {}; //friend std::ostream & operator<< (std::ostream & os, const lacksDMA & rs); virtual void show(); }; class hasDMA :public Brass { private: std::string style; public: hasDMA(const std::string = "none", const std::string = "null", int r = 0); virtual void test() {}; ~hasDMA(); //friend std::ostream & operator<<(std::ostream & os, const hasDMA & hs); virtual void show(); }; #endif
brass.cpp
#include "brass.h" #include <iostream> Brass::Brass(const std::string l, int r) { label = l; rating = r; } Brass::Brass(const Brass & r) { label = r.label; rating = r.rating; } Brass::~Brass() { } void Brass::show() { std::cout<<"label: " << label << "\nrating: "<< rating <<std::endl; } void baseDMA::show() { std::cout << "baseDMA:\n"; Brass::show(); } void lacksDMA::show() { std::cout << "lacksDMA:\n" <<"color: " << color << std::endl; Brass::show(); } void hasDMA::show() { std::cout << "hasDMA:\n" << "style: "<< style << std::endl; Brass::show(); } baseDMA::baseDMA(const std::string l, int r):Brass(l,r) { } baseDMA::baseDMA(baseDMA & rs):Brass(rs.lab(),rs.ra()) { } baseDMA::~baseDMA() { } lacksDMA::lacksDMA(const std::string c, const std::string l, int r):Brass(l,r) { color = c; } hasDMA::hasDMA(const std::string s, const std::string l, int r):Brass(l,r) { style = s; } hasDMA::~hasDMA() { }
main.cpp
#include "brass.h"
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
Brass *pd[3];//虚基类不能创建对象,但可以创建指向其的指针
for (int i = 0; i<3; i++)
{
cout << "Enter the label:";
string label;
getline(cin, label);
cout << "Enter the rating:";
int rat;
cin >> rat;
cout << "Enter the 1 for baseDMA" << endl
<< "2 for lacksDMA" << endl
<< "3 for hasDMA" << endl;
int temp;
(cin >> temp).get();
if (temp == 1)
pd[i] = new baseDMA(label,rat);
else if (temp == 2)
{
cout << "Enter the color:";
string color;
getline(cin, color,'\n');
pd[i]=new lacksDMA (color,label,rat);
}
else if (temp == 3)
{
cout << "Enter the style:";
string style;
getline(cin, style,'\n');
pd[i] = new hasDMA(style,label ,rat);
}
else
{
cout << "invalid input! try again!" << endl;
i--;
}
while (cin.get() != '\n')
continue;
}
cout << endl;
for (int i = 0; i < 3; i++)
pd[i]->show();
for (int i = 0; i < 3; i++)
delete pd[i];
system("pause");
return 0;
}
相关文章推荐
- java web笔记
- 如何在Windows平台使用VS搭建C++/Lua的开发环境
- 深入分析Java的序列化与反序列化
- C#简单的windows文件操作方法
- java程序的加载过程
- Python 3 之 装饰器详解
- c++ 内存泄漏检查
- JAVA邮件发送的简单实现,使用javamail通过smtp协议发信
- [memory]C语言关于字节对齐的问题
- Java 上传文件到ftp服务器
- java回调机制
- C#-dynamic及动态绑定
- gomobile 真机 log 打出的日志跟踪
- jvm虚拟机学习笔记(一)---总体认识
- 原创翻译:James Whittaker系列——Google是如何测试的(3)
- 用goto语句 百钱百鸡。公鸡5元一只,母鸡3元一只,小鸡1元3只。求100元分别能买多少公鸡、母鸡、小鸡。
- 机试练习 北理14年01、02题 打开文件顺序
- Java基础(函数、数组)
- Java自动化工具Ant的基础使用教程
- C#关于FTP上的文件操作