cpp
复制代码
#include <iostream>
using namespace std;
template<typename T>
void fun(T &a,T &b)
{
T temp;
temp=a;
a=b;
b=temp;
}
void fun(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void fun(double &a,double &b)
{
double temp;
temp=a;
a=b;
b=temp;
}
void fun(char &a,char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int a=10,b=20;
fun(a,b);
cout<<a<<" "<<b<<endl;
double c=1.3,d=1.4;
fun(c,d);
cout<<c<<" "<<d<<endl;
return 0;
}
cpp
复制代码
#include <iostream>
using namespace std;
class Animal
{
private:
string name;
public:
Animal(){}
Animal(string n):name(n){}
virtual void perform()
{
cout<<" ... "<<endl;
}
};
class Lion:public Animal
{
private:
string str;
public:
Lion(){}
Lion(string s,string a):str(s),Animal(a){}
void perform()
{
cout<<"狮子,河东狮哄 "<<endl;
}
};
class Dog:public Animal
{
private:
string str1;
public:
Dog(){}
Dog(string s1,string b):str1(s1),Animal(b){}
void perform()
{
cout <<"狗,狗叫"<<endl;
}
};
int main()
{
Lion s("Lion","狮子");
Animal *p;
p=&s;
p->perform();
Dog s1("Dog","狗");
Animal *q;
q=&s1;
q->perform();
return 0;
}