类与继承

#ifndef ANIMAL_H
#define ANIMAL_H
#include
#include
class Animal
{
public:
	Animal();
	Animal(const std::string type_name1, int leg, std::string action);
	Animal(const Animal& animal);
	//Animal& operator=(const Animal& animal);
	virtual ~Animal();

	//virtual std::string Type_name() const;
	virtual void Type_name(std::string type_name1);
	virtual void Leg(int val);
	virtual void Behaviour(std::string action);

	virtual void animal_type(const Animal*);
	virtual void leg_number(const Animal*);
	virtual void move_type(const Animal*);
	
protected:
	std::string m_type_name;//动物类型
	int m_leg = 0;
	std::string m_behaviour;//行走方式

};

#endif
#ifndef BIRDS_H
#define BIRDS_H
#include "Animal.h"
#include
#include
class  Birds :public Animal
{
public:
	Birds();
	Birds(const std::string type_name1, int leg, std::string action, int weigh_val);
	Birds(const Birds& bird);
	Birds& operator=(const Birds& bird);
	virtual ~Birds();
	virtual void Weigh(int val);
	virtual void animal_type(const Animal*);
	virtual void leg_number(const Animal*);
	virtual void move_type(const Animal*);
	virtual void egg_weight(const Birds*);
	
protected:
	int m_weigh = 0;
	
};




#endif
#ifndef _D0G_H
#define _DOG_H
#include"Mammals.h"
#include
#include
class Dog :public Mammals
{
public:
	Dog();
	Dog(const std::string type_name1, int leg, std::string action, int pregnant_period);
	Dog(const Dog& dog);
	virtual void move_type(const Animal*);
	virtual void pregnant_month_count(const Mammals*);
};
#endif
#ifndef MAMMALS_H
#define MAMMALS_H
#include "Animal.h"
#include
#include
class  Mammals :public Animal
{
public:
	Mammals();
	Mammals(const std::string type_name1, int leg, std::string action, int pregnant_period);
	Mammals(const Mammals& mammals);
	//Mammals& operator=(const Mammals& mammals);
	virtual ~Mammals();
	virtual void animal_type(const Animal*);
	virtual void leg_number(const Animal*);
	virtual void move_type(const Animal*);
	virtual void pregnant_month_count(const Mammals*);
	
	virtual void Pregnant_period(int val);
protected:
	int  m_pregnant_period;
};

#endif
#ifndef _OSTRICH_H
#define _OSTRICH_H
#include "Birds.h"
#include
#include
class Ostrich :public Birds
{
public:
	Ostrich();
	Ostrich(const std::string type_name1, int leg, std::string action, int weigh_val);
	Ostrich(const Ostrich& ostrich);
	virtual void move_type(const Animal*);
	virtual void egg_weight(const Birds*);
};


#endif
#ifndef SPARROW_H
#define SPARROW_H
#include "Birds.h"
#include
#include
class Sparrow :public Birds
{
public:
	Sparrow();
	Sparrow(const std::string type_name1, int leg, std::string action, int weigh_val);
	Sparrow(const Sparrow& sparrow);
	virtual void move_type(const Animal*);
	virtual void egg_weight(const Birds*);
};

#endif
#ifndef _WHALE_H
#define _WHALE_H
#include"Mammals.h"
#include
#include
class Whale :public Mammals
{
public:
	Whale();
	Whale(const std::string type_name1, int leg, std::string action, int pregnant_period);
	Whale(const Whale& whale);
	virtual void move_type(const Animal*);
	virtual void pregnant_month_count(const Mammals*);
};


#endif
#include "Animal.h"
#include
#include
Animal::Animal()
{

}
Animal::Animal(const std::string type_name1, int leg, std::string action)
{
	m_type_name.assign(type_name1);//将字符串付给m_type_name
	m_leg = leg;
	m_behaviour.assign(action);
	
}
Animal::Animal(const Animal& animal)
{
	/*int len = strlen(animal.m_type_name);
	m_type_name = new std::string[len];
	memset(m_type_name, 0, len);
	strcpy(m_type_name, animal.m_type_name);*/
	m_type_name.assign(animal.m_type_name);
	m_leg = animal.m_leg;
	m_behaviour.assign(animal.m_behaviour);

	/*int action_len = strlen(animal.m_behaviour);
	m_behaviour = new std::string[action_len];
	memset(m_behaviour, 0, action_len);
	strcpy(m_behaviour, animal.m_behaviour);*/
}
Animal::~Animal()
{
	//delete m_type_name;
	//delete m_behaviour;
}


void Animal::Type_name(std::string type_name1)
{
	m_type_name = type_name1;
}
void Animal::Leg(int val)
{
	 m_leg = val; 
}
void Animal::Behaviour(std::string action)
{
	m_behaviour = action;
}

void Animal::animal_type(const Animal*)
{
	std::cout << "the type: " << m_type_name<
#include "Birds.h"
#include "Animal.h"
#include
#include
using namespace std;
Birds::Birds()
{

}
Birds::Birds(const std::string type_name1, int leg, std::string action,int weigh_val)
	:Animal(type_name1, leg, action)
{
	m_weigh = weigh_val;
}
Birds::Birds(const Birds& bird)
	: Animal(bird)
{
	m_weigh = bird.m_weigh;
}
//Birds::Birds& operator=(const Birds& bird)
Birds:: ~Birds()
{

}


void Birds::Weigh(int val)
{
	m_weigh = val;
}
void Birds:: animal_type(const Animal*)
{
	std::cout << "the type belong to " << m_type_name<< endl;
}
void Birds::leg_number(const Animal*)
{
	std::cout << "leg_number  is " << m_leg << endl;
}
void Birds::move_type(const Animal*)
{
	std::cout << "Birds move_type " << m_behaviour << endl;
}
void Birds::egg_weight(const Birds*)
{
	if (-1 == m_weigh)
	{
		std::cout << "egg_weight is NOTSURE" << endl;
	}
	else
	{
		std::cout << "Birds egg_weight: " << m_weigh << endl;
	}
	
}





#include "Mammals.h"
#include "Dog.h"
#include
#include

Dog::Dog()
{

}
Dog::Dog(const std::string type_name1, int leg, std::string action, int pregnant_period)
	:Mammals(type_name1, leg, action, pregnant_period)
{

}
Dog::Dog(const Dog& dog)
	:Mammals(dog)
{

}
void Dog::move_type(const Animal*)
{
	std::cout << "Dog move_type is " << m_behaviour<< std::endl;
}
void Dog::pregnant_month_count(const Mammals*)
{
	std::cout << "Dog pregnant_month_count is " << m_pregnant_period <<"days"<
#include"Mammals.h"
#include "Animal.h"
#include
#include

Mammals::Mammals()
{

}
Mammals::Mammals(const std::string type_name1, int leg, std::string action, int pregnant_period)
	:Animal(type_name1, leg, action)
{
	m_pregnant_period = pregnant_period;
}
Mammals::Mammals(const Mammals& mammals)
	: Animal(mammals)
{
	m_pregnant_period = mammals.m_pregnant_period;
}
//Mammals::Mammals& operator=(const Mammals& mammals);
Mammals:: ~Mammals()
{

}

void Mammals::Pregnant_period(int val)
{
	m_pregnant_period = val;
}
void Mammals::animal_type(const Animal*)
{
	std::cout << "the type belong to "<
#include "Birds.h"
#include"Ostrich.h"
#include
#include

Ostrich::Ostrich()
{

}
Ostrich::Ostrich(const std::string type_name1, int leg, std::string action, int weigh_val)
	:Birds(type_name1, leg, action, weigh_val)
{

}
Ostrich::Ostrich(const Ostrich& ostrich)
	: Birds(ostrich)
{

}
void Ostrich::move_type(const Animal*)
{
	std::cout << "Ostrich move_type " << m_behaviour << std::endl;
}
void Ostrich::egg_weight(const Birds*)
{
	std::cout << "Ostrich egg_weight: " << m_weigh <<"g"<< std::endl;
}
#include "Birds.h"
#include"Sparrow.h"
#include
#include

Sparrow::Sparrow()
{

}
Sparrow::Sparrow(const std::string type_name1, int leg, std::string action, int weigh_val)
	:Birds(type_name1, leg, action, weigh_val)
{

}
Sparrow::Sparrow(const Sparrow& sparrow)
	: Birds(sparrow)
{

}

void Sparrow::move_type(const Animal*)
{
	std::cout << "Sparrow move_type " << m_behaviour;
	std::cout<< std::endl;
}
void Sparrow::egg_weight(const Birds*)
{
	std::cout << "Sparrow egg_weight: " << m_weigh <<"g" << std::endl;
}
#include "Mammals.h"
#include"Whale.h"
#include
#include

Whale::Whale()
{

}
Whale::Whale(const std::string type_name1, int leg, std::string action, int pregnant_period)
	:Mammals(type_name1, leg, action, pregnant_period)
{

}
Whale::Whale(const Whale& whale)
	:Mammals(whale)
{

}

void Whale::move_type(const Animal*)
{
	std::cout << "Whale move_type is " << m_behaviour;
	std::cout<
//C++类方式实现动物(Animal)、飞禽(Birds)、哺乳动物(Mammals)、麻雀(Sparrow)、鸵鸟(Ostrich)、狗(Dog)、鲸鱼(Whale)
//提供接口:
//(1)animal_type(const Animal*)动物类型, 例如:接口中打印飞禽或哺乳类
//(2)leg_number(const Animal*)几只脚,打印出脚的个数
//(3)move_type(const Animal*)行走的方式,打印出飞、走
//(4)egg_weight(const Birds*)蛋的重量
//(5)pregnant_month_count(const Mammals*)
//要求:
//(1)成员变量不能为public
#include
#include
#include "Animal.h"
#include "Birds.h"
#include"Mammals.h"
#include "Dog.h"
#include"Whale.h"
#include "Ostrich.h"
#include"Sparrow.h"
using namespace std;//C++标准库中的函数或者对象都是在命名空间std中定义的,所以要调用C++标准库时,要写上std
int main()
{
	
	////animals
	//cout << "animals" << endl;
	//Animal* animal = new Animal;//构造
	//animal->animal_type(animal);
	//animal->leg_number(animal);
	//animal->move_type(animal);
	////飞禽类
	//cout << "Birds" << endl;
	//Animal* birds = new Birds;
	//birds->animal_type(birds);
	//birds->leg_number(birds);
	//birds->move_type(birds);
	//Birds* bird=new Birds;
	//bird->egg_weight(bird);
	//cout << "sparrow" << endl;
	//Birds* sparrow = new Sparrow("Birds",2,"fly", 200);
	//sparrow->animal_type(sparrow);
	//cout << "ostrich" << endl;
	//Birds* ostrich = new Ostrich("Birds",2, "walk", 500);
	//ostrich->animal_type(ostrich);
	//cout << "dog" << endl;
	//Mammals* dog = new Dog;
	//dog->animal_type(dog);//mammals;
	//dog->leg_number(dog);
	//dog->move_type(dog);//dog
	//dog->pregnant_month_count(dog);
	//cout << "whale" << endl;
	//Mammals* whale = new Whale;
	//whale->animal_type(whale);
	//whale->leg_number(whale);
	//whale->move_type(whale);
	//whale->pregnant_month_count(whale);
	//delete animal;
	//delete bird;
	//delete sparrow;
	//delete ostrich;
	//delete dog;
	//delete whale;

	//动物类
	cout << "animals" << endl;
	Animal animal;
	animal.Type_name("animal");
	animal.Leg(-1);
	animal.Behaviour("NOTSURE");
	Animal* _animal = new Animal;
	animal.animal_type(_animal);
	animal.leg_number(_animal);
	animal.move_type(_animal);

	//飞禽
	cout << "Birds" << endl;
	Birds bird;
	bird.Type_name("Birds");
	bird.Leg(2);
	bird.Behaviour("NOTSURE");
	bird.Weigh(-1);
	Animal* birds = new Birds;
	bird.animal_type(birds);
	bird.leg_number(birds);
	bird.move_type(birds);
	Birds* _bird = new Birds;
	bird.egg_weight(_bird);
	//哺乳
	cout << "mammals" << endl;
	Mammals mammal;
	mammal.Type_name("Mammals");
	mammal.Leg(4);
	mammal.Behaviour("NOTSURE");
	mammal.Pregnant_period(-1);
	Animal* mammals = new Mammals;
	mammal.animal_type(mammals);
	mammal.leg_number(mammals);
	mammal.move_type(mammals);
	Mammals* _mammal = new Mammals;
	mammal.pregnant_month_count(_mammal);

	//麻雀
	cout << "sparrow" << endl;
	Animal* sparrow=new Sparrow("Birds",2,"fly",200);
	sparrow->animal_type(sparrow);
	sparrow->leg_number(sparrow);
	sparrow->move_type(sparrow);
	Birds* _sparrow = new Sparrow("Birds", 2, "fly", 200);
	_sparrow->egg_weight(_sparrow);
	//鸵鸟
	cout << "ostrich" << endl;
	Animal* ostrich = new Ostrich("Birds", 2, "walk", 500);
	ostrich->animal_type(ostrich);
	ostrich->leg_number(ostrich);
	ostrich->move_type(ostrich);
	Birds* _ostrich = new Ostrich("Birds", 2, "walk", 500);
	_ostrich->egg_weight(_ostrich);
	//狗
	cout << "dog" << endl;
	Animal* dog = new Dog("Mammals", 4, "walk", 60);
	dog->animal_type(dog);
	dog->leg_number(dog);
	dog->move_type(dog);
	Mammals* _dog = new Dog("Mammals", 4, "walk", 60);
	_dog->pregnant_month_count(_dog);
	//鲸鱼
	cout << "whale" << endl;
	Animal* whale = new Whale("Whale", 4, "swim", 365);
	whale->animal_type(whale);
	whale->leg_number(whale);
	whale->move_type(whale);
	Mammals* _whale = new Whale("Whale", 4, "walk", 365);
	_whale->pregnant_month_count(_whale);

	
	delete _animal;
	delete birds;
	delete _bird;
	delete mammals;
	delete _mammal;
	delete sparrow;
	delete _sparrow;
	delete ostrich;
	delete _ostrich;
	delete dog;
	delete _dog;
	delete whale;
	delete _whale;

	system("pause");
	return 0;
}

 

你可能感兴趣的:(c++)