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;
}
相关推荐
猫头虎8 分钟前
Rust评测案例:Rust、Java、Python、Go、C++ 实现五大排序算法的执行时间效率比较(基于 OnlineGDB 平台)
java·开发语言·c++·python·golang·rust·排序算法
milanyangbo11 分钟前
从局部性原理到一致性模型:深入剖析缓存设计的核心权衡
开发语言·后端·缓存·架构
Learn Beyond Limits15 分钟前
Clustering vs Classification|聚类vs分类
人工智能·算法·机器学习·ai·分类·数据挖掘·聚类
chao18984418 分钟前
遗传算法与粒子群算法优化BP提高分类效果
算法·分类·数据挖掘
ScilogyHunter20 分钟前
卫星姿态控制模式全解析:从基准到任务的体系化分类
算法·分类
ftpeak23 分钟前
Rust 嵌入式开发的经验之谈
开发语言·后端·rust
lly20240628 分钟前
Node.js 多进程
开发语言
曹绍华1 小时前
kotlin扩展函数是如何实现的
android·开发语言·kotlin
上去我就QWER1 小时前
Qt中的QShortcut:高效键盘快捷方式开发指南
开发语言·c++·qt
gihigo19982 小时前
MATLAB数值分析方程求解方法详解
算法·机器学习·matlab