//test.h
#ifndef TEST_H_
#define TEST_H_
#include<iostream>
class ABC
{
private:
char mode;
public:
ABC(char m);
ABC(const ABC & a);
ABC(){mode='0';};
virtual ~ABC(){};
virtual void View();
ABC & operator=(const ABC &);
friend std::ostream & operator <<(std::ostream & os,
const ABC & rs);
};
class baseDMA:public ABC
{
private:
char * label;
public:
baseDMA(const char * l ,char m);
baseDMA(const char * l ,const ABC & a);
baseDMA(const baseDMA & bs);
baseDMA();
~baseDMA(){delete [] label;};
baseDMA & operator=(const baseDMA & rs);
void View();
friend std::ostream & operator <<(std::ostream & os,
const baseDMA & rs);
};
class lacksDMA:public ABC
{
private:
enum{COL_LEN =40};
char color[COL_LEN];
public:
lacksDMA(const char*c="blank",char m='0');
lacksDMA(const char * c,const ABC & a);
lacksDMA(const lacksDMA & ls);
lacksDMA();
~lacksDMA(){};
void View();
lacksDMA & operator=(const lacksDMA & ls);
friend std::ostream & operator <<(std::ostream & os,
const lacksDMA &rs);
};
// derived class with DMA
class hasDMA:public ABC
{
private:
char * style;
int cc;
public:
hasDMA(const char * s,int cc,char m );
hasDMA(const char * s,int cc,const ABC & a);
hasDMA(const hasDMA & hs);
hasDMA();
~hasDMA(){delete [] style;};
void View();
hasDMA & operator=(const hasDMA &);
friend std::ostream & operator<<(std::ostream &,const hasDMA & rs);
};
#endif
#include"test.h"
#include<iostream>
#include<cstring>
//base
ABC::ABC(char m)
{
mode=m;
}
ABC::ABC(const ABC & a)
{
mode=a.mode;
}
ABC & ABC::operator=(const ABC & a)
{
mode=a.mode;
return *this;
}
std::ostream & operator <<(std::ostream & os,
const ABC & a)
{
os << "Mode: " << a.mode << std::endl;
return os;
}
void ABC::View()
{
std::cout << *this <<std::endl; //试试用 cout << "Mode: " << mode ;
}
// mode a
baseDMA::baseDMA(const char * l,char m):ABC(m)
{
label=new char[strlen(l)+1];
strcpy(label,l);
}
baseDMA::baseDMA(const char * l ,const ABC & a):ABC(a)
{
label=new char[strlen(l)+1];
strcpy(label,l);
}
baseDMA::baseDMA(const baseDMA & rs):ABC(rs)
{
label=new char[strlen(rs.label)+1];
strcpy(label,rs.label);
}
baseDMA::baseDMA():ABC('0')
{
label=0;
};
baseDMA & baseDMA::operator=(const baseDMA & rs)
{
if(this==&rs)
return *this;
ABC::operator=(rs);
delete [] label;
label=new char[strlen(rs.label)+1];
strcpy(label,rs.label);
return *this;
}
std::ostream & operator <<(std::ostream & os,const baseDMA & rs)
{
os<<(ABC)rs << std::endl;
os << "Label: " << rs.label << std::endl;
return os;
}
void baseDMA::View()
{
std::cout << *this << std::endl;
}
// mode b
lacksDMA::lacksDMA(const char*c,char m):ABC(m)
{
strcpy(color,c);
}
lacksDMA::lacksDMA(const char * c,const ABC & a):ABC(a)
{
strcpy(color,c);
}
lacksDMA::lacksDMA(const lacksDMA & ls):ABC(ls)
{
strcpy(color,ls.color);
}
lacksDMA::lacksDMA():ABC('0')
{
strcpy(color,"blank");
}
lacksDMA & lacksDMA::operator=(const lacksDMA & ls)
{
if(this==&ls)
return *this;
strcpy(color,ls.color);
ABC::operator=(ls);
return *this;
}
std::ostream & operator <<(std::ostream & os,
const lacksDMA &rs)
{
os << (ABC)rs << std::endl;
os << "Color: " << rs.color << std::endl;
return os;
}
void lacksDMA::View()
{
std::cout << *this << std::endl;
}
// mode c
hasDMA::hasDMA(const char * s,int c0,char m ):ABC(m)
{
style=new char[strlen(s)+1];
strcpy(style,s);
int cc=c0;
}
hasDMA::hasDMA(const char * s,int c0,const ABC & a):ABC(a)
{
style=new char[strlen(s)+1];
strcpy(style,s);
cc=c0;
}
hasDMA::hasDMA(const hasDMA & hs):ABC(hs)
{
style=new char[strlen(hs.style)+1];
strcpy(style,hs.style);
cc=hs.cc;
}
hasDMA::hasDMA():ABC('0')
{
style=0;
cc=0;
}
hasDMA & hasDMA::operator=(const hasDMA & rs)
{
if(this==&rs)
return *this;
delete [] style;
ABC::operator=(rs);
style=new char[strlen(rs.style)+1];
strcpy(style,rs.style);
cc=rs.cc;
return *this;
}
std::ostream & operator<<(std::ostream & os,const hasDMA & rs)
{
os << (ABC)rs << std::endl;
os << "Style: " << rs.style << std::endl;
os << "CC: " << rs.cc << std::endl;
return os;
}
void hasDMA::View()
{
std::cout<< *this << std::endl;
}
#include"test.h"
#include<iostream>
#include<cstring>
#include<string>
#include<stdlib.h>
const int MODE=3;
int main()
{
using std::cout,std::endl,std::cin;
ABC * Pa[MODE];
char mode;
char label1[40];
char color1[40];
char style1[40];
int cc;
for(int i=0;i<MODE;i++)
{
cout <<"Please enter mode: ";
cin >>mode;
cin.get();
if(mode=='a')
{
cout << "Please enter label: ";
cin.getline(label1,39);
baseDMA ba(label1,'a');
Pa[i]=&ba;
cout << "aaaaaaaaaaaaaaaaa\n" << endl;
}
else if(mode=='b')
{
cout << "Please enter color: ";
cin.getline(color1,39);
lacksDMA la(color1,'b');
Pa[i]=&la;
cout << "bbbbbbbbbbbbbbbbb\n" << endl;
}
else if(mode=='c')
{
cout <<"Please enter style: ";
cin.getline(style1,39);
cout << "Please enter CC: ";
cin >> cc;
hasDMA ha(style1,cc,'c');
Pa[i]=&ha;
cout << "ccccccccccccccccccccc\n" << endl;
}
else
{
cout <<"Please enter a,b or c.\n";
i--;
continue;
}
}
for(int i=0;i<MODE;i++)
{
Pa[i]->View();
}
cin.get();
cin.get();
return 0;
}
=====================================================
Why does running the program output 3 Mode: b?
Please enter mode: c Please enter style: cc Please enter CC: 1 ccccccccccccccccccccc
Please enter mode: c Please enter style: ccc Please enter CC: 2 ccccccccccccccccccccc
Please enter mode: b Please enter color: bb bbbbbbbbbbbbbbbbb
Mode: b
Mode: b
Mode: b
Don't tell me that the unpredictable program behavior is caused by the pointer pointing to the object of the released memory. Because this output is obviously regular, if you don't believe it, you can copy the code and try it. I just don't understand why the view() function can be called even though the memory has been released, and sometimes the output is repeated. enter image description hereenter image description here