请设计4个类,“上帝”,“动物”,“狗”,“牛”,

vectorsoftwareli 2014-04-18 10:31:30
请设计4个类,“上帝”,“动物”,“狗”,“牛”,请简要写出类定义和实现代码,如有必要,可以引入辅助类。
1),全世界只有一个“上帝”。
2),只有“上帝”才能创造“动物”,“上帝”根据“动物”的名字创造“动物”。
3),所有的“动物”都会跑。
4),“狗”会吠。
5),“牛”会吃。
...全文
662 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿_Sir 2014-04-30
  • 打赏
  • 举报
回复
引用 7 楼 xiemanR 的回复:
非科班的渣程序员献丑了,感觉实现的不是很完美,不过还是把代码发出来吧。只有一个上帝所以上帝类要用到单件模式,如何实现单件模式请自行百度。重构代码之后,下面再把代码发一次:


#include <iostream>
#include <string>

using namespace std;

class Animal
{
public:
	 void run()
	 {
		 cout << "runing." << endl;
	 }

	 virtual void DoSomething()
	 {
		 cout << "do something." << endl;
	 }

	 virtual ~Animal()
	 {

	 }

};

class Dog:public Animal
{
public:
	void DoSomething()
	{
		cout << "dog bark." << endl;
	}
};

class Cow:public Animal
{
public:
	void DoSomething()
	{
		cout << "cow eat." << endl;
	}
};

class God
{
public:
	
	static God* CreatInstance();

	Animal* CreateAnimal(string animalName)
	{
		if (animalName == "Dog")
		{
			pAnimal = new Dog();
		}
		else if (animalName == "Cow")
		{
			pAnimal = new Cow();
		}
		else
		{
			pAnimal = new Animal();
		}

		return pAnimal;
	}

	~God()
	{
		delete instance;
		delete pAnimal;

	}

private:

	static God* instance;

	Animal* pAnimal;

	God()
		: pAnimal(NULL)
	{

	}

};

God* God::instance = NULL;

God* God::CreatInstance()
{
	if (instance == NULL)
	{
		instance = new God();
	}

	return instance;

}


int main()
{

	God * pGod = God::CreatInstance(); // There is only one god

	Animal* pAnimal = NULL;

	pAnimal = pGod->CreateAnimal("Dog");  // God Creat Dog.

	pAnimal->run();
	pAnimal->DoSomething(); 


	pAnimal = pGod->CreateAnimal("Cow");  // God Creat Cow.
	pAnimal->run();
	pAnimal->DoSomething();


	int pause = 0;
	cin >> pause;

	return 0;
}

单例模式可以这样吧 class n { public: ~n(){} n* Get(){ static n nn ; return &nn ;} private: n(){} };
netcore 2014-04-30
  • 打赏
  • 举报
回复
这个实现有点长。 // --- main.cpp --- #include "God.h" #include "Animal.h" #include <iostream> using namespace std; God *God::instance = NULL; void myGod() { God *god; Animal *dog; Animal *cow; Animal *unknown; god = God::creatInstance(); dog = god->createAnimal("Dog"); cow = god->createAnimal("Cow"); unknown = god->createAnimal("???"); dog->run(); dog->doSomething(); cow->run(); cow->doSomething(); unknown->run(); unknown->doSomething(); god->deleteInstance(); } void wait() { int i; cin >> i; } int main(int argc, char* argv[]) { myGod(); wait(); return 0; } // --- output --- Dog running... Dog bark... Cow running... Cow eat... unknown animal running... unknown animal do something... // --- Animal.h --- #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <iostream> #include <string> using namespace std; class Animal { public: Animal(string name) { this->animalName = name; } virtual ~Animal() { } void run() { cout << animalName << " running..." << endl; } virtual void doSomething() { cout << animalName << " do something..." << endl; } virtual Animal *clone() { return new Animal("unknown animal"); } string &name() { return animalName; } private: string animalName; }; #endif // --- AnimalFactory.h --- #ifndef ANIMAL_FACTORY_H_ #define ANIMAL_FACTORY_H_ #include "Factory.h" #include "Animal.h" #include "Cow.h" #include "Dog.h" class AnimalFactory : private Factory { public: AnimalFactory() { initAnimalFactory(); } virtual ~AnimalFactory() { // free animal ... } Animal *createAnimal(string &name) { Animal *animalType = (Animal *)createObject(name); if (animalType == NULL) { animalType = unknownAnimal; } Animal *animal = animalType->clone(); // save animal ... return animal; } private: void initAnimalFactory() { unknownAnimal = new Animal(""); insertAnimalToFactory(new Cow()); insertAnimalToFactory(new Dog()); } void insertAnimalToFactory(Animal *animal) { insertObjectToFactory(animal->name(), animal); } Animal *unknownAnimal; }; #endif // --- Cow.h --- #ifndef COW_H_ #define COW_H_ #include "Animal.h" #include <iostream> using namespace std; class Cow : public Animal { public: void doSomething() { cout << name() << " eat..." << endl; } Animal *clone() { return new Cow(); } Cow() : Animal("Cow") { } virtual ~Cow() {} }; #endif // --- Dog.h --- #ifndef DOG_H_ #define DOG_H_ #include "Animal.h" #include <iostream> using namespace std; class Dog : public Animal { public: void doSomething() { cout << name() << " bark..." << endl; } Animal *clone() { return new Dog(); } Dog() : Animal("Dog") { } virtual ~Dog() {} }; #endif // --- Factory.h --- #ifndef FACTORY_H_ #define FACTORY_H_ #include <map> #include <string> using namespace std; typedef map<string, void *> ObjectMap; typedef pair<string, void *> ObjectPair; class Factory { public: Factory() { } virtual ~Factory() { // free objectFactory ... } protected: void *createObject(string &name) { if (objFactory.end() != objFactory.find(name)) { return objFactory.find(name)->second; } return NULL; } void insertObjectToFactory(string &name, void *obj) { objFactory.insert(ObjectPair(name, obj)); } private: ObjectMap objFactory; }; #endif // --- God.h --- #ifndef GOD_H_ #define GOD_H_ #include "Animal.h" #include "AnimalFactory.h" #include <string> using namespace std; class Animal; class AnimalFactory; class God { public: static God* creatInstance() { if (instance == NULL) { instance = new God(); } return instance; } static void deleteInstance() { delete instance; instance = NULL; } Animal *createAnimal(string name) { return animalFactory->createAnimal(name); } private: God() { animalFactory = new AnimalFactory(); } virtual ~God() { delete animalFactory; } static God* instance; AnimalFactory *animalFactory; }; #endif
乐百川 2014-04-25
  • 打赏
  • 举报
回复
引用 32 楼 Study_Bird 的回复:
上帝创造的动物应该放到容器里面,上帝毁灭后,杀死所有的动物。
这个略狠哇……我只能想到牛和狗是由动物类派生出来的,他们应该是上帝类的成员对象,别的就想不到了……
Study_Bird 2014-04-25
  • 打赏
  • 举报
回复
class CGod { public: CGod(){} ~CGod() { for (long i= 0; i< m_arrAnimal.size(); i++) { delete m_arrAnimal[i]; m_arrAnimal[i]= NULL; } m_arrAnimal.clear(); } static CGod* CreatInstance(); CAnimal* CreateAnimal(string animalName) { CAnimal* pAnimal= NULL; if (animalName == "Dog") { pAnimal = new CDog(); } else if (animalName == "Cow") { pAnimal = new CCow(); } else { pAnimal = new CAnimal(); } if (pAnimal) { m_arrAnimal.push_back(pAnimal); } return pAnimal; } private: static CGod* m_staticGod; vector<CAnimal*> m_arrAnimal; }; CGod* CGod::m_staticGod = NULL; CGod* CGod::CreatInstance() { if (m_staticGod == NULL) { m_staticGod = new CGod(); } return m_staticGod; }
Study_Bird 2014-04-25
  • 打赏
  • 举报
回复
上帝创造的动物应该放到容器里面,上帝毁灭后,杀死所有的动物。
Hardworking2009 2014-04-21
  • 打赏
  • 举报
回复
把Animal *作为God的属性合适吗? 只有上帝可以创造动物,“上帝”根据“动物”的名字创造“动物”,这里是不是要把Animal及子类的构造函数全部private,在每个类里加 friend Animal *God::CreateAnimal(string AnimalName);
盘子饿了 2014-04-21
  • 打赏
  • 举报
回复
严格的单件模式要把析构函数、copy构造函数和operator=()也写出来并且写成私有,并且提供静态cleanup()方法。
希望在哪 2014-04-21
  • 打赏
  • 举报
回复
学习了。
神-气 2014-04-21
  • 打赏
  • 举报
回复
static void KillGod()
神-气 2014-04-21
  • 打赏
  • 举报
回复


#include <iostream>
#include <string>

using namespace std;

class Animal
{
public:
	 void run()
	 {
		 cout << "runing." << endl;
	 }
	 virtual ~Animal()
	 {

	 }

};

class Dog:public Animal
{
public:
	void Bark()
	{
		cout << "bark." << endl;
	}
};

class Cow:public Animal
{
public:
	void Eat()
	{
		cout << "eat." << endl;
	}
};

class God
{
public:
	static God* CreatGod();

	Animal* CreateAnimal(string animalName)
	{
		if (animalName == "Dog")
		{
			pAnimal = new Dog();
		}
		else if (animalName == "Cow")
		{
			pAnimal = new Cow();
		}
		else
		{
			pAnimal = new Animal();
		}

		return pAnimal;
	}
        void KillAnimal(Animal* someone)
       {
              delete someone;
       }

	void KillGod()
	{
                if(instance)
		{
                       delete instance;
                       instance = NULL;
                }
	}

private:
       God();
        ~God();
	static God* instance;

	Animal* pAnimal;

	God()
		: pAnimal(NULL)
	{

	}

};

God* God::instance = NULL;

God* God::CreatInstance()
{
	if (instance == NULL)
	{
		instance = new God();
	}

	return instance;

}


int main()
{

	God * pGod = God::CreatGod(); // There is only one god

	Animal* pAnimal = NULL;

	pAnimal = pGod->CreateAnimal("Dog");  // God Creat Dog.
	pAnimal->run();
	pAnimal->Bake(); 
        pGod->KillAnimal(pAnimal);

	pAnimal = pGod->CreateAnimal("Cow");  // God Creat Cow.
	pAnimal->run();
	pAnimal->Eat();
        pGod->KillAnimal(pAnimal);

        God::KillGod()

	int pause = 0;
	cin >> pause;

	return 0;
}

神-气 2014-04-21
  • 打赏
  • 举报
回复
引用 7 楼 xiemanR 的回复:
非科班的渣程序员献丑了,感觉实现的不是很完美,不过还是把代码发出来吧。只有一个上帝所以上帝类要用到单件模式,如何实现单件模式请自行百度。重构代码之后,下面再把代码发一次:


#include <iostream>
#include <string>

using namespace std;

class Animal
{
public:
	 void run()
	 {
		 cout << "runing." << endl;
	 }

	 virtual void DoSomething()
	 {
		 cout << "do something." << endl;
	 }

	 virtual ~Animal()
	 {

	 }

};

class Dog:public Animal
{
public:
	void DoSomething()
	{
		cout << "dog bark." << endl;
	}
};

class Cow:public Animal
{
public:
	void DoSomething()
	{
		cout << "cow eat." << endl;
	}
};

class God
{
public:
	
	static God* CreatInstance();

	Animal* CreateAnimal(string animalName)
	{
		if (animalName == "Dog")
		{
			pAnimal = new Dog();
		}
		else if (animalName == "Cow")
		{
			pAnimal = new Cow();
		}
		else
		{
			pAnimal = new Animal();
		}

		return pAnimal;
	}

	~God()
	{
		delete instance;
		delete pAnimal;

	}

private:

	static God* instance;

	Animal* pAnimal;

	God()
		: pAnimal(NULL)
	{

	}

};

God* God::instance = NULL;

God* God::CreatInstance()
{
	if (instance == NULL)
	{
		instance = new God();
	}

	return instance;

}


int main()
{

	God * pGod = God::CreatInstance(); // There is only one god

	Animal* pAnimal = NULL;

	pAnimal = pGod->CreateAnimal("Dog");  // God Creat Dog.

	pAnimal->run();
	pAnimal->DoSomething(); 


	pAnimal = pGod->CreateAnimal("Cow");  // God Creat Cow.
	pAnimal->run();
	pAnimal->DoSomething();


	int pause = 0;
	cin >> pause;

	return 0;
}

改一下你的。
wanren13 2014-04-20
  • 打赏
  • 举报
回复
引用 7 楼 xiemanR 的回复:
非科班的渣程序员献丑了,感觉实现的不是很完美,不过还是把代码发出来吧。只有一个上帝所以上帝类要用到单件模式,如何实现单件模式请自行百度。重构代码之后,下面再把代码发一次:


#include <iostream>
#include <string>

using namespace std;

class Animal
{
public:
	 void run()
	 {
		 cout << "runing." << endl;
	 }

	 virtual void DoSomething()
	 {
		 cout << "do something." << endl;
	 }

	 virtual ~Animal()
	 {

	 }

};

class Dog:public Animal
{
public:
	void DoSomething()
	{
		cout << "dog bark." << endl;
	}
};

class Cow:public Animal
{
public:
	void DoSomething()
	{
		cout << "cow eat." << endl;
	}
};

class God
{
public:
	
	static God* CreatInstance();

	Animal* CreateAnimal(string animalName)
	{
		if (animalName == "Dog")
		{
			pAnimal = new Dog();
		}
		else if (animalName == "Cow")
		{
			pAnimal = new Cow();
		}
		else
		{
			pAnimal = new Animal();
		}

		return pAnimal;
	}

	~God()
	{
		delete instance;
		delete pAnimal;

	}

private:

	static God* instance;

	Animal* pAnimal;

	God()
		: pAnimal(NULL)
	{

	}

};

God* God::instance = NULL;

God* God::CreatInstance()
{
	if (instance == NULL)
	{
		instance = new God();
	}

	return instance;

}


int main()
{

	God * pGod = God::CreatInstance(); // There is only one god

	Animal* pAnimal = NULL;

	pAnimal = pGod->CreateAnimal("Dog");  // God Creat Dog.

	pAnimal->run();
	pAnimal->DoSomething(); 


	pAnimal = pGod->CreateAnimal("Cow");  // God Creat Cow.
	pAnimal->run();
	pAnimal->DoSomething();


	int pause = 0;
	cin >> pause;

	return 0;
}

微wx笑 2014-04-20
  • 打赏
  • 举报
回复
引用 7 楼 xiemanR 的回复:
非科班的渣程序员献丑了,感觉实现的不是很完美,不过还是把代码发出来吧。只有一个上帝所以上帝类要用到单件模式,如何实现单件模式请自行百度。重构代码之后,下面再把代码发一次:


#include <iostream>
#include <string>

using namespace std;

class Animal
{
public:
	 void run()
	 {
		 cout << "runing." << endl;
	 }

	 virtual void DoSomething()
	 {
		 cout << "do something." << endl;
	 }

	 virtual ~Animal()
	 {

	 }

};

class Dog:public Animal
{
public:
	void DoSomething()
	{
		cout << "dog bark." << endl;
	}
};

class Cow:public Animal
{
public:
	void DoSomething()
	{
		cout << "cow eat." << endl;
	}
};

class God
{
public:
	
	static God* CreatInstance();

	Animal* CreateAnimal(string animalName)
	{
		if (animalName == "Dog")
		{
			pAnimal = new Dog();
		}
		else if (animalName == "Cow")
		{
			pAnimal = new Cow();
		}
		else
		{
			pAnimal = new Animal();
		}

		return pAnimal;
	}

	~God()
	{
		delete instance;
		delete pAnimal;

	}

private:

	static God* instance;

	Animal* pAnimal;

	God()
		: pAnimal(NULL)
	{

	}

};

God* God::instance = NULL;

God* God::CreatInstance()
{
	if (instance == NULL)
	{
		instance = new God();
	}

	return instance;

}


int main()
{

	God * pGod = God::CreatInstance(); // There is only one god

	Animal* pAnimal = NULL;

	pAnimal = pGod->CreateAnimal("Dog");  // God Creat Dog.

	pAnimal->run();
	pAnimal->DoSomething(); 


	pAnimal = pGod->CreateAnimal("Cow");  // God Creat Cow.
	pAnimal->run();
	pAnimal->DoSomething();


	int pause = 0;
	cin >> pause;

	return 0;
}

调用了两次pGod->CreateAnimal,而只在God中释放了一次,内存会泄漏吧?
微wx笑 2014-04-20
  • 打赏
  • 举报
回复
不用单例模式吧,上帝设计成静态类就可以了吧
derekrose 2014-04-20
  • 打赏
  • 举报
回复
上帝是单例没问题 但是上帝应该是拥有一个creator 本身不应该是creator 但是看成工厂也可以,那么扩展可能就有问题了 比如我说让上帝有说话的功能 “要有光!” creator可不能会说话吧
匚匚 2014-04-20
  • 打赏
  • 举报
回复
引用 14 楼 testcs_dn 的回复:
[quote=引用 7 楼 xiemanR 的回复:] 非科班的渣程序员献丑了,感觉实现的不是很完美,不过还是把代码发出来吧。只有一个上帝所以上帝类要用到单件模式,如何实现单件模式请自行百度。重构代码之后,下面再把代码发一次:


#include <iostream>
#include <string>

using namespace std;

class Animal
{
public:
	 void run()
	 {
		 cout << "runing." << endl;
	 }

	 virtual void DoSomething()
	 {
		 cout << "do something." << endl;
	 }

	 virtual ~Animal()
	 {

	 }

};

class Dog:public Animal
{
public:
	void DoSomething()
	{
		cout << "dog bark." << endl;
	}
};

class Cow:public Animal
{
public:
	void DoSomething()
	{
		cout << "cow eat." << endl;
	}
};

class God
{
public:
	
	static God* CreatInstance();

	Animal* CreateAnimal(string animalName)
	{
		if (animalName == "Dog")
		{
			pAnimal = new Dog();
		}
		else if (animalName == "Cow")
		{
			pAnimal = new Cow();
		}
		else
		{
			pAnimal = new Animal();
		}

		return pAnimal;
	}

	~God()
	{
		delete instance;
		delete pAnimal;

	}

private:

	static God* instance;

	Animal* pAnimal;

	God()
		: pAnimal(NULL)
	{

	}

};

God* God::instance = NULL;

God* God::CreatInstance()
{
	if (instance == NULL)
	{
		instance = new God();
	}

	return instance;

}


int main()
{

	God * pGod = God::CreatInstance(); // There is only one god

	Animal* pAnimal = NULL;

	pAnimal = pGod->CreateAnimal("Dog");  // God Creat Dog.

	pAnimal->run();
	pAnimal->DoSomething(); 


	pAnimal = pGod->CreateAnimal("Cow");  // God Creat Cow.
	pAnimal->run();
	pAnimal->DoSomething();


	int pause = 0;
	cin >> pause;

	return 0;
}

调用了两次pGod->CreateAnimal,而只在God中释放了一次,内存会泄漏吧?[/quote] 肯定泄漏了
~God()
    {
        delete instance;
        delete pAnimal; 
    }
God类是单例模式,而设计时是通过new创建的对象,释放内存时须显式调用 delete God::CreatInstance();
f_liu 2014-04-20
  • 打赏
  • 举报
回复
围观大神操作
ooolinux 2014-04-20
  • 打赏
  • 举报
回复
学习了。。。
Inhibitory 2014-04-20
  • 打赏
  • 举报
回复
上帝是工厂类。 动物是狗和牛的父类。
kuankuan_qiao 2014-04-20
  • 打赏
  • 举报
回复
引用 21 楼 z84616995z 的回复:
这题有意思。学习了。
哈哈
加载更多回复(16)

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧