65,210
社区成员
发帖
与我相关
我的任务
分享
//main.cpp
/*
Label: Portabelly
Rating: 8
Label: Blimpo
Rating: 4
Color: red
Label: Buffalo Keys
Rating: 5
Style: Mercator
Label: Blimpo
Rating: 4
Color: red
Label: null
Rating: 0
Style: 请按任意键继续. . .
*/
#include <iostream>
#include <stdlib.h>
#include "acctdma.h"
using namespace std;
int main(int argc, char *argv[])
{
Basedma shirt("Portabelly", 8);
Lacksdma balloon("red", "Blimpo", 4);
Hasdma map("Mercator", "Buffalo Keys", 5);
cout << shirt << endl;
cout << balloon << endl;
cout << map << endl;
Lacksdma balloon2(balloon);
Hasdma map2; //这里Hasdma缺少匹配的构造函数
map2 = map;
cout << balloon2 << endl;
cout << map2 << endl;
Acctdma * pts[3];
pts[0] = &shirt;
pts[1] = &balloon;
pts[2] = ↦
for (int i = 0; i < 3; i++)
cout << *pts[i] << endl;
for (int i = 0; i < 3; i++)
pts[i]->View();
system("PAUSE");
return 0;
}
//acctdma.h
#ifndef ACCTDMA_H_
#define ACCTDMA_H_
#include <iostream>
// Abstract Base Class
class Acctdma
{
private:
char * label;
int rating;
public:
Acctdma(const char * l = "null",int r = 0);
Acctdma(const Acctdma & rs);
virtual ~Acctdma()=0;
virtual Acctdma & operator*() { return *this; }
Acctdma & operator = (const Acctdma & rs);
virtual void View() const;
friend std::ostream & operator << (std::ostream & os,const Acctdma & rs);
};
// Class Basedma
class Basedma:public Acctdma
{
private:
public:
Basedma(const char * l = "null",int r = 0);
};
// Class Lacksdma
class Lacksdma:public Acctdma
{
private:
char color[40];
public:
Lacksdma (const char * c ,const char * l,int r);
Lacksdma (const char * c,const Acctdma & rs);
void View() const;
friend std::ostream & operator <<(std::ostream & os,const Lacksdma & ls);
};
//Class Hasdma
class Hasdma:public Acctdma
{
private:
char * style;
public:
Hasdma():style(NULL){};//注意这里,你缺少了缺省构造函数,因为你已经声明了其他构造函数,那么编译器不会再给你生成缺省构造函数
Hasdma(const char * s ,const char * l,int r );
Hasdma(const char * s,const Acctdma & hs);
Hasdma(const Hasdma & hs);
void View() const;
~Hasdma();
Hasdma & operator = (const Hasdma & hs);
friend std::ostream & operator <<(std::ostream & os,const Hasdma & hs);
};
#endif
//acctdma.cpp
//类方法:
#include <iostream>
#include <cstring>
#include"acctdma.h"
using namespace std;
//acctdma methods
Acctdma::Acctdma(const char * l,int r)
{
label = new char[std::strlen(l)+1];
std::strcpy(label,l);
rating = r;
}
Acctdma::Acctdma(const Acctdma & rs)
{
label = new char[std::strlen(rs.label) +1];
std::strcpy(label,rs.label);
rating = rs.rating;
}
Acctdma::~Acctdma()
{
delete [] label;
}
Acctdma & Acctdma::operator = (const Acctdma & 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 Acctdma:: View() const
{
cout <<"Label: " <<label <<endl;
cout <<"Rating: " <<rating <<endl;
}
std::ostream & operator <<(std::ostream & os,const Acctdma & rs)
{
os <<"Label: " <<rs.label <<endl;
os <<"Rating: " <<rs.rating <<endl;
return os;
}
//Basedma methods
Basedma::Basedma(const char * l, int r) : Acctdma(l,r)
{
}
//Lacksdma methods
Lacksdma::Lacksdma(const char *c , const char *l , int r):Acctdma(l,r)
{
std::strncpy(color,c,39);
color[39]='\0';
}
Lacksdma::Lacksdma(const char *c, const Acctdma &rs):Acctdma(rs)
{
std::strncpy(color ,c,39);
color[39] = '\0';
}
void Lacksdma::View() const
{
Acctdma::View();
cout <<"Color: " <<color <<endl;
}
std::ostream & operator <<(std::ostream & os,const Lacksdma & ls)
{
os <<(const Acctdma &) ls;
os <<"Color: " <<ls.color <<endl;
return os;
}
//Hasdma methods
Hasdma::Hasdma(const char *s , const char *l , int r ):Acctdma(l,r)
{
style = new char[std::strlen(s)+1];
std::strcpy(style,s);
}
Hasdma::Hasdma(const char *s, const Acctdma &hs):Acctdma(hs)
{
style = new char[std::strlen(s)+1];
std::strcpy(style,s);
}
Hasdma::Hasdma(const Hasdma &hs):Acctdma(hs)
{
style = new char[std::strlen(hs.style)+1];
std::strcpy(style,hs.style);
}
Hasdma::~Hasdma()
{
delete [] style;
}
Hasdma & Hasdma::operator =(const Hasdma & hs)
{
if(this==&hs);
return *this;
Acctdma::operator=(hs); //copy abstract portion
style = new char[std::strlen(hs.style)+1];
std::strcpy(style,hs.style);
return *this;
}
void Hasdma::View() const
{
Acctdma::View();
cout <<"Style: " <<style <<endl;
}
std::ostream & operator <<(std::ostream & os,const Hasdma & hs)
{
os <<(const Acctdma &) hs;
os <<"Style: " <<hs.style <<endl;
return os;
}