代码如下,定义了一个抽象基类ABCDMA,派生出BaseDMA,LacksDMA,HasDMA三个类,其中BaseDMA的私有成员和ABCDMA类完全一样。每个类都有一个重新定义的View()函数用于显示类成员。
测试程序时出现问题,定义了一个ABCDMA类指针数组,用户自己决定数组元素指向哪个类型的对象。在初始化后调用View()函数时,BaseDMA类没有问题,但是剩下两个类的私有成员显示不出来。求解这是为什么呢?
主程序
//dmanew.cpp
#include <iostream>
#include <cctype>
#include "abcdma.h"
int main()
{
using std::cin;
using std::cout;
using std::endl;
char trigger;
char templab[30];
int rat;
ABCDMA *item[10];
char kind;
for(int i=0;i<10;i++)
{
cout<<"Enter the label of item #"<<i+1<<": "<<endl;
cin.getline(templab,30);
cout<<"Enter the rating of item #"<<i+1<<": "<<endl;
cin>>rat;
cout<<"Enter 1 for BaseDMA, 2 for LacksDMA or 3 for HasDMA: ";
while(cin>>kind && (kind!='1' && kind!='2' && kind!='3'))
cout<<"Enter 1,2 or 3: ";
if(kind=='1')
item[i]=new BaseDMA(templab,rat);
else if(kind=='2')
{
char tempcolor[40];
cout<<"Enter the color of item #"<<i+1<<": "<<endl;
cin.getline(tempcolor,40);
item[i]=new LacksDMA(tempcolor,templab,rat);
}
else
{
char tempstyle[30];
cout<<"Enter the style of item #"<<i+1<<": "<<endl;
cin.getline(tempstyle,30);
item[i]=new HasDMA(tempstyle,templab,rat);
}
while(cin.get()!='\n')
continue;
}
cout<<endl;
for(int i=0;i<10;i++)
{
item[i]->View();
}
for(int i=0;i<10;i++)
{
delete item[i];
}
cout<<"Done.Bye!"<<endl;
return 0;
}
头文件
//abcdma.h
#ifndef ABCDMA_H_
#define ABCDMA_H_
//abstract base class
class ABCDMA
{
private:
char *label;
int rating;
public:
ABCDMA(const char *l="null",int r=0);
ABCDMA(const ABCDMA &ad);
virtual void View() const=0;
virtual ~ABCDMA() {delete [] label;}
ABCDMA &operator=(const ABCDMA &ad);
};
//BaseDMA class
class BaseDMA:public ABCDMA
{
public:
BaseDMA(const char *l="null",int r=0);
BaseDMA(const ABCDMA &ad):ABCDMA(ad) {}
BaseDMA(const BaseDMA &rs);
~BaseDMA() {}
virtual void View() const;
BaseDMA &operator=(const BaseDMA &rs);
};
//LacksDMA class
class LacksDMA:public ABCDMA
{
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 ABCDMA &ad);
virtual void View() const;
};
//HasDMA class
class HasDMA:public ABCDMA
{
private:
char *style;
public:
HasDMA(const char *s="none",const char *l="null",int r=0);
HasDMA(const char *s,const ABCDMA &ad);
HasDMA(const HasDMA &hs);
~HasDMA() {delete [] style;}
virtual void View() const;
HasDMA &operator=(const HasDMA &hs);
};
#endif
类方法实现
//abcdma.cpp
#include <iostream>
#include <cstring>
#include "abcdma.h"
//ABCDMA methods
ABCDMA::ABCDMA(const char *l,int r)
{
label=new char[std::strlen(l)+1];
std::strcpy(label,l);
rating=r;
}
ABCDMA::ABCDMA(const ABCDMA &ad)
{
label=new char[std::strlen(ad.label)+1];
std::strcpy(label,ad.label);
rating=ad.rating;
}
void ABCDMA::View() const
{
std::cout<<"Label: "<<label<<std::endl;
std::cout<<"Rating: "<<rating<<std::endl;
}
ABCDMA &ABCDMA::operator=(const ABCDMA &ad)
{
if(this==&ad)
return *this;
delete [] label;
label=new char[std::strlen(ad.label)+1];
std::strcpy(label,ad.label);
rating=ad.rating;
return *this;
}
//BaseDMA methods
BaseDMA::BaseDMA(const char *l,int r):ABCDMA(l,r) {}
BaseDMA::BaseDMA(const BaseDMA &rs):ABCDMA(rs) {}
void BaseDMA::View() const
{
ABCDMA::View();
}
BaseDMA &BaseDMA::operator=(const BaseDMA &rs)
{
if(this==&rs)
return *this;
ABCDMA::operator=(rs);
return *this;
}
//LacksDMA methods
LacksDMA::LacksDMA(const char *c,const char *l,int r):ABCDMA(l,r)
{
std::strncpy(color,c,39);
color[39]='\0';
}
LacksDMA::LacksDMA(const char *c,const ABCDMA &ad):ABCDMA(ad)
{
std::strncpy(color,c,COL_LEN-1);
color[COL_LEN-1]='\0';
}
void LacksDMA::View() const
{
ABCDMA::View();
std::cout<<"Color: "<<color<<std::endl;
}
//HasDMA methods
HasDMA::HasDMA(const char *s,const char *l,int r):ABCDMA(l,r)
{
style=new char[std::strlen(s)+1];
std::strcpy(style,s);
}
HasDMA::HasDMA(const char *s,const ABCDMA &ad):ABCDMA(ad)
{
style=new char[std::strlen(s)+1];
std::strcpy(style,s);
}
HasDMA::HasDMA(const HasDMA &hs):ABCDMA(hs)
{
style=new char[std::strlen(hs.style)+1];
std::strcpy(style,hs.style);
}
void HasDMA::View() const
{
ABCDMA::View();
std::cout<<"Style: "<<style<<std::endl;
}
HasDMA &HasDMA::operator=(const HasDMA &hs)
{
if(this==&hs)
return *this;
ABCDMA::operator=(hs);
delete [] style;
style=new char[std::strlen(hs.style)+1];
std::strcpy(style,hs.style);
return *this;
}