c++第六天

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;
}
相关推荐
Bio Coder1 分钟前
R语言安装生物信息数据库包
开发语言·数据库·r语言
Tiger Z2 分钟前
R 语言科研绘图第 27 期 --- 密度图-分组
开发语言·程序人生·r语言·贴图
fai厅的秃头姐!2 小时前
C语言03
c语言·数据结构·算法
life_time_2 小时前
C语言(22)
c语言·开发语言
lisanndesu2 小时前
动态规划
算法·动态规划
Minner-Scrapy2 小时前
DApp 开发入门指南
开发语言·python·web app
myprogramc3 小时前
十大排序算法
数据结构·算法·排序算法
记得早睡~3 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
修己xj3 小时前
算法系列之贪心算法
算法