設計桌子與木材或鋼材料。我有以下設計,這更好,爲什麼?或者有更好的建議設計?OO設計,桌子設計
設計1:
class Meterial{
public:
void virtual info()=0;
};
class Wood:public Meterial{
public:
void info();
};
void Wood::info(){
cout<<"wood"<< " ";
}
class Steel:public Meterial{
public:
void info();
};
void Steel::info(){
cout<<"steel"<< " ";
}
class Furniture{
void virtual info()=0;
};
class Table:public Furniture{
private:
Meterial * _meterial;
public:
Table(Meterial * m);
void info();
};
Table::Table(Meterial * m){
_meterial= m;
}
void Table::info(){
_meterial->info();
cout<< " table " << endl;
}
int main(){
Table * wood_table=new Table(new Wood());
Table * steel_table=new Table(new Steel());
wood_table->info();
steel_table->info();
}
設計2:
class Meterial{
public:
virtual void info()=0;
};
class Wood:public Meterial{
public:
void info();
};
void Wood::info(){
cout<<" wood " << " ";
}
class Steel:public Meterial{
public:
void info();
};
void Steel::info(){
cout<<" Steel " << " ";
}
class Furniture{
public:
virtual void info()=0;
};
class Table:public Furniture{
public:
void info();
};
void Table::info(){
cout<<" table "<< endl;
}
class WoodTable:public Wood,public Table{
public:
void info();
};
void WoodTable::info(){
Wood::info();
Table::info();
}
class SteelTable:public Steel,public Table{
public:
void info();
};
void SteelTable::info(){
Steel::info();
Table::info();
}
int main(){
WoodTable *woodTable = new WoodTable();
SteelTable *steelTable = new SteelTable();
woodTable->info();
steelTable->info();
}
的感謝!
表格的形式根據所做的不同而不同?否則,你的工作太多了。 'enum Material {Wood,Steel};' – 2011-02-23 23:02:48