C++中的代码重用(三)MI(多重继承)
/*虚基类的声明使得多重继承成为可能*/
/*主要需要解决两件问题*/
/*一、构造函数的建立
二、利用哪个基类的方法*/
//statement.h
#ifndef STATEMENT_H_
#define STATEMENT_H_
/*虚基类的声明使得多重继承成为可能*/
/*主要需要解决两件问题*/
/*一、构造函数的建立
二、利用哪个基类的方法*/
#include<iostream>
#include<string>
using namespace std;
class animal
{
private:
string sort;
int age;
protected://利用保护数据,包含关系无法访问,但是在派生类中相当于public
void get();//输入数据成员协助派生类
void display() const;//输出数据成员协助派生类
public:
animal() :sort("null"), age(0){}
animal(const string &s, int a) :sort(s), age(a){}
virtual ~animal(){}
virtual void set() = 0;
virtual void show() const = 0;
};
class dog :virtual public animal//虚基类
{
private:
string mood;
protected:
void get();
void display() const;
public:
dog() :animal(), mood("null"){}
dog(const string &s, int a, const string &str) :animal(s, a), mood(str){}
dog(const animal &an, const string &str) :animal(an), mood(str){}
~dog(){}
void set();
void show() const;
};
class cat :virtual public animal//虚基类
{
protected:
enum{ other,red, orange, yellow, green, blue, purple };
enum{num=7};
void get();
void display() const;
private:
static char* color[num];
int choice;
public:
cat() :animal(), choice(other){}
cat(const string &s, int a, int c=other) :animal(s, a), choice(c){}
cat(const animal &an, int c = other) :animal(an), choice(c){}
~cat(){}
void set();
void show() const;
};
class dogcat :public dog, public cat//默认是private
{
private:
bool exist_or_not;
protected:
void get();
void display() const;
public:
dogcat(){}//多重继承的默认构造函数的创建
dogcat(const string &s, int a, const string &str, int c, bool e)
:animal(s, a), dog(s, a, str), cat(s, a, c), exist_or_not(e){}//多重继承的构造函数构建
dogcat(const animal &an, const string &str, int c, bool e)
:animal(an), dog(an, str), cat(an, c), exist_or_not(e){}
dogcat(const dog &d, int c, bool e) :animal(d), dog(d), cat(d, c), exist_or_not(e){}
/*这里对于cat(d, c)就用到了dog的隐式复制构造函数和cat构造函数cat(const animal &an, int c = other)
实现了基类指针指向派生类对象,同样对于下面的构造函数*/
dogcat(const cat &c, const string &str, bool e) :animal(c), dog(c,str), cat(c), exist_or_not(e){}
void set();
void show() const;
};
#endif //STATEMENT_H_
//define.cpp
/**
*Copyright U+00A9 2018 XXXXXX. All Right Reserved.
*Filename:
*Author:XXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/
#include<iostream>
//#include<cstdlib>
#include"statement.h"
using namespace std;
void animal::get()//输入数据成员协助派生类
{
cout << "please input the sort of animal: ";
cin >> sort;
cout << "The following input the age of " << sort<<" : ";
cin >> age;
}
void animal::display() const//输出数据成员协助派生类
{
cout << "Sort: " << sort << ", Age: " << age << endl;
}
void dog::get()
{
cout << "input the mood: ";
cin >> mood;
}
void dog::display() const
{
cout << "Mood: " << mood << endl;
}
void dog::set()
{
animal::get();
get();
}
void dog::show() const
{
animal::display();
display();
}
char* cat::color[cat::num] = { "white", "red", "orange", "yellow", "green", "blue", "purple" };
void cat::get()
{
cout << "input the choice for cat color:\n";
int i;
for (i = 0; i < num; i++)
{
cout << i << ": " << color[i] << " ";
if (i % 4 == 3)
cout << endl;
}
if (i % 4 != 0)
cout << endl;
cin >> choice;
while (cin.get() != '\n')
continue;
}
void cat::display() const
{
cout << "The color is " << color[choice] << endl;
}
void cat::set()
{
animal::get();
get();
}
void cat::show() const
{
animal::display();
display();
}
void dogcat::get()//避免重复调用虚基类animal的get()方法
{
cout << "input the exist is or not(0 or !0):";
cin >> exist_or_not;
if (exist_or_not == true)
{
dog::get();
cat::get();
}
else cout << "Sorry, the animal is not exist!\n";
}
void dogcat::display() const
{
dog::display();
cat::display();
}
void dogcat::set()
{
animal::get();
get();
}
void dogcat::show() const
{
animal::display();
display();
}
//main.cpp
/**
*Copyright U+00A9 2018 XXXXXX. All Right Reserved.
*Filename:
*Author:XXXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/
#include<iostream>
//#include<cstdlib>
#include"windows.h"
#include<cstring>/*调用strchr(string,ch)函数返回字符ch在字符串
string中第一次出现的地址,如果没有返回NULL*/
#include"statement.h"
using namespace std;
const int size = 5;
int main()
{
animal *an[size];
int i;
for (i = 0; i < size; i++)
{
char ch;
cout << "Enter the animal category:\n"
<< "d: dog c: cat\n"
<< "e: dogcat q:quit\n";
cin >> ch;
while (strchr("dceq", ch) == NULL)
{
cout << "please input d,c,e or q:";
cin >> ch;
}
if (ch == 'q')
break;
switch (ch)
{
case 'D':
case 'd':an[i] = new dog;
break;
case 'C':
case 'c':an[i] = new cat;
break;
case 'E':
case 'e':an[i] = new dogcat;
break;
}
cin.get();
an[i]->set();
}
cout << "\nHere is your choice:\n";
for (int j = 0; j < i; j++)
{
cout << endl;
an[j]->show();
}
for (int j = 0; j < i; j++)
delete an[j];
cout << "Done!\n";
system("pause");
return 0;
}
程序运行结果如下图