1.
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void perform(){}
};
class Lion : public Animal
{
string feature = "凶猛";
string performance = "lll";
public:
void perform()
{
cout << feature << " ";
cout << performance << endl;
}
};
class Elephant : public Animal
{
string feature = "高大";
string performance = "eee";
public:
void perform()
{
cout << feature << " ";
cout << performance << endl;
}
};
class Monkey : public Animal
{
string feature = "灵活";
string performance = "mmm";
public:
void perform()
{
cout << feature << " ";
cout << performance << endl;
}
};
int main()
{
Animal *a;
Lion l;
a = &l;
a->perform();
Elephant e;
a = &e;
a->perform();
Monkey m;
a = &m;
a->perform();
return 0;
}
2.
#include <iostream>
using namespace std;
template <typename T>
void fun(T &a,T &b)
{
T temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10,b = 20;
fun(a,b);
cout << a << " " << b << endl;
float c = 1.34,d = 3.14;
fun(c,d);
cout << c << " " << d << endl;
return 0;
}
3.