@一些C++class类的代码
//main8_class.cpp
#include // input/output standard library
#include // standard string library
#include
#include
#include "Plant.h"
using namespace std;
class CuttingFlower : public Plant
{
public:
//Default Constructor
CuttingFlower(){
cerr << "\nCuttingFlower::Constructor()";
name_ = "CuttingFlower " + randomName(10);
}
CuttingFlower(std::string my_name){
cerr << "\nCuttingFlower::Constructor() with Name"<<endl;
name_ = my_name;
}
//Destructor
~CuttingFlower()
{
cerr << "\nCuttingFlower::Destructor()";
}//this is a declaration and a definition
string TypeOfVaseToUse(){
//if
return "LargeVase";
}
int MonthToCut(){
//if
return 7;
}
virtual std::string GetName() const{
cerr << "\nCuttingFlower::GetName()"<<endl;
return "CuttingFlower" + Plant::name_;
}
virtual int GetID() const{
return Object::id_ + 90000;
}
};
class Vegetable : public Plant
{
public:
//Constructor
Vegetable(){
cerr << "\nVegetable::Constructor()";}
~Vegetable()
{
cerr << "\nVegetable::Destructor()";
}//this is a declaration and a definition
virtual std::string GetName() const
{
cerr << "\nVegetable::GetName()"<<endl;
return "Vegetable" + Plant::name_;
}
int MonthToPick()
{
//if
return 7;
}
virtual int GetID() const{
return Object::id_ + 70000;
}
};
class Fruit : public Plant
{
public:
//Constructor
Fruit()
{
cerr << "\nFruit::Constructor()";
}
~Fruit()
{
cerr << "\nFruit::Destructor()";
}//this is a declaration and a definition
};
int main()
{
cerr << "\nMy garden"<<endl;
Seed mini_seed;
mini_seed.size_ = 3.e-3;
//Vegetable carrot_;
//carrot_.SetSeed(mini_seed);
//Fruit melon_;
//melon_.SetSeed(mini_seed);
cout << "----------------------CuttingFlower dahlia_;-----------------------" << endl;
CuttingFlower dahlia_;
dahlia_.SetSeed(mini_seed);
cerr << "\nName (FIRST-GetName-BASE): " << dahlia_.GetName()<<endl;
//cerr << "\nName (FIRST-FlowerName-DERIVED): " << dahlia_.FlowerName();
cout << "-----------------------CuttingFlower dahlia2_(Dahlia)-------------------------"<< endl;
CuttingFlower dahlia2_("Dahlia");
dahlia2_.SetSeed(mini_seed);
cerr << "\nName (SECOND-GetName-BASE): " << dahlia2_.GetName()<<endl;
//cerr << "\nName (SECOND-FlowerName-DERIVED): " << dahlia2_.FlowerName();
{
cout << "-----------------------CuttingFlower* dahlia_ = new CuttingFlower(Cyclamen)--------------------" << endl;
CuttingFlower* dahlia_ = new CuttingFlower("Cyclamen");
dahlia_->SetSeed(mini_seed);
cerr << "\n\nName: " << dahlia_->GetName()<<endl;
delete dahlia_;
}
//cerr << "------------------------\n\n";
{
Plant* plant = NULL;
//asking the user what plant to create
cout << "-----------------------plant = new CuttingFlower(Cyclamen)---------------------------------" << endl;
plant = new CuttingFlower("Cyclamen");
plant->SetSeed(mini_seed);
cerr << "\n\nName: " << plant->GetName()<<endl;// << " = " << (*plant).GetName();
delete plant;
///
cout << "--------------------------------plant = new vegetable-----------------------" << endl;
plant = new Vegetable();
plant->SetSeed(mini_seed);
cerr << "\n\nName: " << plant->GetName()<<endl;// << " = " << (*plant).GetName();
delete plant;
cout << "----------------------Object* obj = new CuttingFlower(Cyclamen)------------------------------" << endl;
Object* obj = new CuttingFlower("Cyclamen"); //父类创建的步骤
}
std::vector<Object*> vItems_in_the_garden;
std::map<std::string, Object*> mItems_in_the_garden_by_name;
//define plant
cout << "--------------------plant1 = new CuttingFlower(Cyclamen)-----------------------------" << endl;
Plant* plant1 = NULL;
plant1 = new CuttingFlower("Cyclamen");
plant1->SetSeed(mini_seed);
vItems_in_the_garden.push_back(plant1);
mItems_in_the_garden_by_name.insert(make_pair(plant1->GetName(), plant1));
cout << "----------------------plant2 = new Vegetable()------------------------------" << endl;
Vegetable* plant2 = NULL;
plant2 = new Vegetable();
plant2->SetSeed(mini_seed);
vItems_in_the_garden.push_back(plant2);
mItems_in_the_garden_by_name.insert(make_pair(plant2->GetName(), plant2));
cout << "--------------------plant3 = new CuttingFlower()------------------------------" << endl;
CuttingFlower* plant3 = NULL;
plant3 = new CuttingFlower();
plant3->SetSeed(mini_seed);
vItems_in_the_garden.push_back(plant3);
mItems_in_the_garden_by_name.insert(make_pair(plant3->GetName(), plant3));
//iterate over items in my garden and print out the name
cout << endl;
cout << "------------------ID----------------------" << endl;
cerr << "\n\n\nIDs from vector:";
for (std::vector<Object*>::const_iterator iVect = vItems_in_the_garden.begin(); iVect != vItems_in_the_garden.end(); iVect++)
cerr << "\n" << (*iVect)->GetID();
//cerr << "\nNames from Map:";
//for (std::map::const_iterator iVect = mItems_in_the_garden_by_name.begin(); iVect != mItems_in_the_garden_by_name.end(); iVect++)
// cerr << "\n" << (*iVect).first;
//cerr << "\nNames from vector:";
//for (std::vector
// cerr << "\n" << dynamic_cast((*iVect))->GetName();//dynamically re-assigned the type of this object
//for (std::map::const_iterator iVect = mItems_in_the_garden_by_name.begin(); iVect != mItems_in_the_garden_by_name.end(); iVect++)
// cerr << "\n" << (*iVect).first; -> SAME AS...
cout << endl;
cout << "---------mItems_in_the_garden_by_name-----------" << endl;
for (auto object : mItems_in_the_garden_by_name)
cerr << "\n" << object.first;
cout << endl;
cout << "------vItems_in_the_garden------------" << endl;
//for (std::vector
// cerr << "\n" << dynamic_cast((*iVect))->GetName();//dynamically re-assigned the type of this object -> SAME AS...
for (auto object : vItems_in_the_garden)
cerr << "\n" << dynamic_cast<Plant*>(object)->GetName()<<endl;
cout << endl;
cout << "--------------Delete------------" << endl;
delete plant1;
delete plant2;
delete plant3;
cin.get();
return 0;
}
下面是Plant.h
//Plant.h
#ifndef PLANT_H
#define PLANT_H
#include // input/output standard library
#include // standard string library
#include
//dont include using namespace commands in header files because it leads to inconsistent interpretation of the naming of the classes
//simple class
enum Colour {
Yellow, Red, Blue, White, Pink, Green };//Instances
std::string randomName(int len);//declaration
//Essentially the same as a class; but by default PUBLIC
struct Seed
{
double size_;
};
class Object //Abstract Class
{
public:
Object(int random_id) {
id_ = random_id; }
virtual int GetID() const = 0; //@pure@ virtual function (=0)
protected:
int id_;
};
//Declaration of the class "Plant", everything in a class is by default PRIVATE
//Base Class
class Plant : public Object
{
public:
Plant();
~Plant();
//Get Name
virtual std::string GetName() const;
//Member functions
//Accessor for the seed
//Seed GetSeed() const { return seed_; }//making a copy
const Seed& GetSeed() const;
//Mutator for the seed
void SetSeed(const Seed& s);
//Accessor/Mutator
Seed& MySeed();
virtual int GetID() const {
return -1; }
private:
//Members
Seed seed_;
protected:
std::string name_;
};
#endif
下面是Plant.cpp
//Plant.cpp
#include "Plant.h"
using namespace std;
//This file contains the definitions of our functions
std::string randomName(int len)
{
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string r;
for (int i = 0; i < len; i++) r.push_back(a.at(size_t(rand() % 62)));
return r;
}
Plant::Plant() : Object(rand() % 1000)
{
name_ = randomName(10);
cerr << "\nPlant::Constructor() with auto name --> " << name_;
}//this is a declaration and a definition
Plant::~Plant()
{
cerr << "\nPlant::Destructor()"<<endl;
}//this is a declaration and a definition
std::string Plant::GetName() const
{
cerr << "\nPlant::GetName()";
return "Plant" + name_;
}
//Member functions
//Accessor for the seed
//Seed GetSeed() const { return seed_; }//making a copy
const Seed& Plant::GetSeed() const
{
return seed_;
}//NOT making a copy
//Mutator for the seed
void Plant::SetSeed(const Seed& s)
{
seed_ = s;
}
//Accessor/Mutator
Seed& Plant::MySeed()
{
return seed_;
}