fshb_ 1370
پنج شنبه 08 دی 1390, 15:34 عصر
سلام دوستان
این برنامه اجرا میشه ولی وقتی pointer b به lacksDMA و hasDMA اشاره میکنه نمیتونه فیلدهای color و style رو نمایش بده. اون قسمت از کد که فک کنم ایراد داره با //ERROR مشخص کردم
دلیلش چیه؟ مشکل کار کجاست؟
پیشاپیش ممنون از راهنمایی همه
DMA.h
#ifndef DMA_H_
#define DMA_H_
//Abstract base class
class base
{
private:
char *label;
int rating;
public:
base(const char *l="NULL",int r=0);
base(const base & b);
virtual ~base();
base & operator=(const base & b);
virtual void show();
};
class DMA:public base
{
public:
DMA(const char *l="NULL",int r=0):base(l,r){}
DMA(const DMA &d):base(d){}
//virtual ~DMA();
//DMA& operator=(const DMA &b);
//friend ostream & operator<<(ostream &os,const DMA &d);
};
class lacksDMA:public base
{
private:
char color[40];
public:
lacksDMA(const char *c="NO",const char *l="NULL",int r=0);
lacksDMA(const char *c,const base & b);
lacksDMA(const lacksDMA &l);
virtual void show();
//friend ostream & operator<<(ostream &os,const lacksDMA &l);
};
class hasDMA:public base
{
private:
char *style;
public:
hasDMA(const char *s="NO",const char *l="NULL",int r=0);
hasDMA(const char *s,const base &b);
hasDMA(const hasDMA &h);
~hasDMA();
hasDMA & operator=(const hasDMA &h);
virtual void show();
//friend ostream & operator<<(ostream &os,const hasDMA &h);
};
#endif
DMA.cpp
#include<iostream>
#include<cstring>
using namespace std;
#include "DMA.h"
base::base(const char *l,int r)
{
label=new char[strlen(l)+1];
strcpy(label,l);
rating=r;
}
base::base(const base &b)
{
label=new char[strlen(b.label)+1];
strcpy(label,b.label);
rating=b.rating;
}
base::~base()
{
delete [] label;
}
base & base::operator =(const base &b)
{
if(this==&b)
return *this;
delete [] label;
label=new char[strlen(b.label)+1];
strcpy(label,b.label);
rating=b.rating;
return *this;
}
/*ostream & operator<<(ostream &os,const base &b)
{
os<<"label: "<<b.label<<endl;
os<<"rating: "<<b.rating<<endl;
int x;
cin>>x;
return os;
}
ostream & operator<<(ostream &os,const DMA &d)
{
os<<(const base &)d;
return os;
}*/
void base::show()
{
cout<<"label: "<<label<<endl;
cout<<"rating: "<<rating<<endl;
}
lacksDMA::lacksDMA(const char *c, const char *l, int r):base(l,r)
{
strncpy(color,c,39);
color[30]='\0';
}
lacksDMA::lacksDMA(const char *c, const base &b):base(b)
{
strncpy(color,c,39);
color[30]='\0';
}
lacksDMA::lacksDMA(const lacksDMA &l):base(l)
{
strncpy(color,l.color,39);
color[39]='\0';
}
void lacksDMA::show()
{
base::show();
cout<<"color: "<<color<<endl;
}
hasDMA::hasDMA(const char *s, const char *l, int r):base(l,r)
{
style=new char[strlen(s)+1];
strcpy(style,s);
}
hasDMA::hasDMA(const char *s, const base &b):base(b)
{
style=new char[strlen(s)+1];
strcpy(style,s);
}
hasDMA::hasDMA(const hasDMA &h):base(h)
{
style=new char[strlen(h.style)+1];
strcpy(style,h.style);
}
hasDMA::~hasDMA()
{
delete [] style;
}
hasDMA & hasDMA::operator =(const hasDMA &h)
{
if(this==&h)
return *this;
base::operator =(h);
delete [] style;
style=new char[strlen(h.style)+1];
strcpy(style,h.style);
return *this;
}
/*ostream & operator<<(ostream &os,const hasDMA &h)
{
os<<(const base &)h;
os<<"style: "<<h.style<<endl;
return os;
}*/
void hasDMA::show()
{
base::show();
cout<<"style: "<<style<<endl;
}
MAIN.cpp
#include<iostream>
using namespace std;
#include"DMA.h"
const int LEN=40;
const int SIZE=1;
int main()
{
char temp[LEN];
int tempR=0;
char kind;
base *b[SIZE];
int i=0;
for (i=0;i<SIZE;i++)
{
cout<<"Enter Label: ";
cin.getline(temp,LEN);
cout<<"Enter Rating: ";
cin>>tempR;
cout<<"Enter 1 for DMA and 2 for lacksDMA and 3 for hasDMA: ";
while(cin>>kind&&(kind!='1'&&kind!='2'&&kind!='3'))
cout<<"Eneter either 1 or 2 or 3";
if(kind=='1')
b[i]=new DMA(temp,tempR);
else if(kind=='2')
{
//ERROR
char tempC[LEN];
cout<<"Enter color: ";
cin.getline(tempC,LEN);
b[i]=new lacksDMA(tempC,temp,tempR);
}
else
{
//ERROR
char tempS[LEN];
cout<<"Enter Style: ";
cin.getline(tempS,LEN);
b[i]=new hasDMA(tempS,temp,tempR);
}
while(cin.get()!='\n')
continue;
}
cout<<"--------\n";
for(i=0;i<SIZE;i++)
b[i]->show();
for(i=0;i<SIZE;i++)
delete b[i];
cout<<"\nDONE.\n";
return 0;
}
این برنامه اجرا میشه ولی وقتی pointer b به lacksDMA و hasDMA اشاره میکنه نمیتونه فیلدهای color و style رو نمایش بده. اون قسمت از کد که فک کنم ایراد داره با //ERROR مشخص کردم
دلیلش چیه؟ مشکل کار کجاست؟
پیشاپیش ممنون از راهنمایی همه
DMA.h
#ifndef DMA_H_
#define DMA_H_
//Abstract base class
class base
{
private:
char *label;
int rating;
public:
base(const char *l="NULL",int r=0);
base(const base & b);
virtual ~base();
base & operator=(const base & b);
virtual void show();
};
class DMA:public base
{
public:
DMA(const char *l="NULL",int r=0):base(l,r){}
DMA(const DMA &d):base(d){}
//virtual ~DMA();
//DMA& operator=(const DMA &b);
//friend ostream & operator<<(ostream &os,const DMA &d);
};
class lacksDMA:public base
{
private:
char color[40];
public:
lacksDMA(const char *c="NO",const char *l="NULL",int r=0);
lacksDMA(const char *c,const base & b);
lacksDMA(const lacksDMA &l);
virtual void show();
//friend ostream & operator<<(ostream &os,const lacksDMA &l);
};
class hasDMA:public base
{
private:
char *style;
public:
hasDMA(const char *s="NO",const char *l="NULL",int r=0);
hasDMA(const char *s,const base &b);
hasDMA(const hasDMA &h);
~hasDMA();
hasDMA & operator=(const hasDMA &h);
virtual void show();
//friend ostream & operator<<(ostream &os,const hasDMA &h);
};
#endif
DMA.cpp
#include<iostream>
#include<cstring>
using namespace std;
#include "DMA.h"
base::base(const char *l,int r)
{
label=new char[strlen(l)+1];
strcpy(label,l);
rating=r;
}
base::base(const base &b)
{
label=new char[strlen(b.label)+1];
strcpy(label,b.label);
rating=b.rating;
}
base::~base()
{
delete [] label;
}
base & base::operator =(const base &b)
{
if(this==&b)
return *this;
delete [] label;
label=new char[strlen(b.label)+1];
strcpy(label,b.label);
rating=b.rating;
return *this;
}
/*ostream & operator<<(ostream &os,const base &b)
{
os<<"label: "<<b.label<<endl;
os<<"rating: "<<b.rating<<endl;
int x;
cin>>x;
return os;
}
ostream & operator<<(ostream &os,const DMA &d)
{
os<<(const base &)d;
return os;
}*/
void base::show()
{
cout<<"label: "<<label<<endl;
cout<<"rating: "<<rating<<endl;
}
lacksDMA::lacksDMA(const char *c, const char *l, int r):base(l,r)
{
strncpy(color,c,39);
color[30]='\0';
}
lacksDMA::lacksDMA(const char *c, const base &b):base(b)
{
strncpy(color,c,39);
color[30]='\0';
}
lacksDMA::lacksDMA(const lacksDMA &l):base(l)
{
strncpy(color,l.color,39);
color[39]='\0';
}
void lacksDMA::show()
{
base::show();
cout<<"color: "<<color<<endl;
}
hasDMA::hasDMA(const char *s, const char *l, int r):base(l,r)
{
style=new char[strlen(s)+1];
strcpy(style,s);
}
hasDMA::hasDMA(const char *s, const base &b):base(b)
{
style=new char[strlen(s)+1];
strcpy(style,s);
}
hasDMA::hasDMA(const hasDMA &h):base(h)
{
style=new char[strlen(h.style)+1];
strcpy(style,h.style);
}
hasDMA::~hasDMA()
{
delete [] style;
}
hasDMA & hasDMA::operator =(const hasDMA &h)
{
if(this==&h)
return *this;
base::operator =(h);
delete [] style;
style=new char[strlen(h.style)+1];
strcpy(style,h.style);
return *this;
}
/*ostream & operator<<(ostream &os,const hasDMA &h)
{
os<<(const base &)h;
os<<"style: "<<h.style<<endl;
return os;
}*/
void hasDMA::show()
{
base::show();
cout<<"style: "<<style<<endl;
}
MAIN.cpp
#include<iostream>
using namespace std;
#include"DMA.h"
const int LEN=40;
const int SIZE=1;
int main()
{
char temp[LEN];
int tempR=0;
char kind;
base *b[SIZE];
int i=0;
for (i=0;i<SIZE;i++)
{
cout<<"Enter Label: ";
cin.getline(temp,LEN);
cout<<"Enter Rating: ";
cin>>tempR;
cout<<"Enter 1 for DMA and 2 for lacksDMA and 3 for hasDMA: ";
while(cin>>kind&&(kind!='1'&&kind!='2'&&kind!='3'))
cout<<"Eneter either 1 or 2 or 3";
if(kind=='1')
b[i]=new DMA(temp,tempR);
else if(kind=='2')
{
//ERROR
char tempC[LEN];
cout<<"Enter color: ";
cin.getline(tempC,LEN);
b[i]=new lacksDMA(tempC,temp,tempR);
}
else
{
//ERROR
char tempS[LEN];
cout<<"Enter Style: ";
cin.getline(tempS,LEN);
b[i]=new hasDMA(tempS,temp,tempR);
}
while(cin.get()!='\n')
continue;
}
cout<<"--------\n";
for(i=0;i<SIZE;i++)
b[i]->show();
for(i=0;i<SIZE;i++)
delete b[i];
cout<<"\nDONE.\n";
return 0;
}