系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:修改baseDMA-lacksDMA-hasDMA类的层次,让三个类都从一个ABC派生而来,然后使用于程序清单13.10相类似的程序对结果进行测试,也就是说,它应使用ABC指针素组,并让用户决定要创建的对象类型,在类定义中添加virtual View()方法以处理数据显示
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
修改baseDMA-lacksDMA-hasDMA类的层次,让三个类都从一个ABC派生而来,然后使用于程序清单13.10相类似的程序对结果进行测试,也就是说,它应使用ABC指针素组,并让用户决定要创建的对象类型,在类定义中添加virtual View()方法以处理数据显示
提示:以下是本篇文章正文内容,下面案例可供参考
一、继承
修改baseDMA-lacksDMA-hasDMA类的层次,让三个类都从一个ABC派生而来,然后使用于程序清单13.10相类似的程序对结果进行测试,也就是说,它应使用ABC指针素组,并让用户决定要创建的对象类型,在类定义中添加virtual View()方法以处理数据显示
二、使用步骤
1.头文件
直接用dma.h的类做修改,增加一个虚函数virtual void View() const;
代码如下(示例):
// dma.h -- inheritance and dynamic memory allocation
#ifndef DMA_H_
#define DMA_H_
#include <iostream>
// Base Class Using DMA
class baseDMA
{
private:
char* label;
int rating;
public:
baseDMA(const char* l = "null", int r = 0);
baseDMA(const baseDMA& rs);
virtual ~baseDMA();
baseDMA& operator=(const baseDMA& rs);
friend std::ostream& operator<<(std::ostream& os,
const baseDMA& rs);
virtual void View() const;
};
// derived class without DMA
// no destructor needed
// uses implicit copy constructor
// uses implicit assignment operator
class lacksDMA :public baseDMA
{
private:
enum { COL_LEN = 40 };
char color[COL_LEN];
public:
lacksDMA(const char* c = "blank", const char* l = "null",
int r = 0);
lacksDMA(const char* c, const baseDMA& rs);
friend std::ostream& operator<<(std::ostream& os,
const lacksDMA& rs);
void View() const;
};
// derived class with DMA
class hasDMA :public baseDMA
{
private:
char* style;
public:
hasDMA(const char* s = "none", const char* l = "null",
int r = 0);
hasDMA(const char* s, const baseDMA& rs);
hasDMA(const hasDMA& hs);
~hasDMA();
hasDMA& operator=(const hasDMA& rs);
friend std::ostream& operator<<(std::ostream& os,
const hasDMA& rs);
void View() const;
};
#endif
2 方法
对增加的虚函数进行编写
// dma.cpp --dma class methods
#define _CRT_SECURE_NO_WARNINGS
#include "pe13-3.h"
#include <cstring>
// baseDMA methods
baseDMA::baseDMA(const char* l, int r)
{
label = new char[std::strlen(l) + 1];
std::strcpy(label, l);
rating = r;
}
baseDMA::baseDMA(const baseDMA& rs)
{
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
}
baseDMA::~baseDMA()
{
delete[] label;
}
baseDMA& baseDMA::operator=(const baseDMA& rs)
{
if (this == &rs)
return *this;
delete[] label;
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
return *this;
}
void baseDMA::View() const
{
std::cout << "Label: " << label << std::endl;
std::cout << "Rating: " << rating << std::endl;
}
std::ostream& operator<<(std::ostream& os, const baseDMA& rs)
{
os << "Label: " << rs.label << std::endl;
os << "Rating: " << rs.rating << std::endl;
return os;
}
// lacksDMA methods
lacksDMA::lacksDMA(const char* c, const char* l, int r)
: baseDMA(l, r)
{
std::strncpy(color, c, 39);
color[39] = '\0';
}
lacksDMA::lacksDMA(const char* c, const baseDMA& rs)
: baseDMA(rs)
{
std::strncpy(color, c, COL_LEN - 1);
color[COL_LEN - 1] = '\0';
}
void lacksDMA::View() const
{
baseDMA::View();
std::cout << "Color: " << color << std::endl;
}
std::ostream& operator<<(std::ostream& os, const lacksDMA& ls)
{
os << (const baseDMA&)ls;
os << "Color: " << ls.color << std::endl;
return os;
}
// hasDMA methods
hasDMA::hasDMA(const char* s, const char* l, int r)
: baseDMA(l, r)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const char* s, const baseDMA& rs)
: baseDMA(rs)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA& hs)
: baseDMA(hs) // invoke base class copy constructor
{
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
}
hasDMA::~hasDMA()
{
delete[] style;
}
void hasDMA::View() const
{
baseDMA::View();
std::cout << "Style: " << style << std::endl;
}
hasDMA& hasDMA::operator=(const hasDMA& hs)
{
if (this == &hs)
return *this;
baseDMA::operator=(hs); // copy base portion
delete[] style; // prepare for new style
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
return *this;
}
std::ostream& operator<<(std::ostream& os, const hasDMA& hs)
{
os << (const baseDMA&)hs;
os << "Style: " << hs.style << std::endl;
return os;
}
3.测试函数
代码如下(示例):
测试函数这里暂用usebrass2.cpp的基础上进行修改,这里作业留给大家
// usebrass2.cpp -- polymorphic example
// compile with brass.cpp
#include <iostream>
#include <string>
#include "brass.h"
const int CLIENTS = 4;
int main()
{
using std::cin;
using std::cout;
using std::endl;
Brass * p_clients[CLIENTS];
std::string temp;
long tempnum;
double tempbal;
char kind;
for (int i = 0; i < CLIENTS; i++)
{
cout << "Enter client's name: ";
getline(cin,temp);
cout << "Enter client's account number: ";
cin >> tempnum;
cout << "Enter opening balance: $";
cin >> tempbal;
cout << "Enter 1 for Brass Account or "
<< "2 for BrassPlus Account: ";
while (cin >> kind && (kind != '1' && kind != '2'))
cout <<"Enter either 1 or 2: ";
if (kind == '1')
p_clients[i] = new Brass(temp, tempnum, tempbal);
else
{
double tmax, trate;
cout << "Enter the overdraft limit: $";
cin >> tmax;
cout << "Enter the interest rate "
<< "as a decimal fraction: ";
cin >> trate;
p_clients[i] = new BrassPlus(temp, tempnum, tempbal,
tmax, trate);
}
while (cin.get() != '\n')
continue;
}
cout << endl;
for (int i = 0; i < CLIENTS; i++)
{
p_clients[i]->ViewAcct();
cout << endl;
}
for (int i = 0; i < CLIENTS; i++)
{
delete p_clients[i]; // free memory
}
cout << "Done.\n";
/* code to keep window open
if (!cin)
cin.clear();
while (cin.get() != '\n')
continue;
*/
return 0;
}
总结
提示:这里对文章进行总结:
例如:
1,主要考察虚函数,
2,深度拷贝
3,继承