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;
}
相关推荐
码客-ygluu7 小时前
X3 LUA 异步分布式微服务集群框架
c++·架构·lua
starzy19908 小时前
深入理解Scala隐式类:扩展方法机制
开发语言·人工智能·scala
M--Y9 小时前
LangGraph 工作流模式
开发语言·javascript·python·langchain·大模型·llm·langgraph
forest_0910 小时前
多 Agent 协作第二课:交叉验证≠独立验证
人工智能·python·算法·机器学习·prompt·agi
九英里路10 小时前
protobuf快速上手
网络·c++·中间件·json·谷歌·序列化·protobuf
吴可可12310 小时前
WScript Shell操作注册表详解
开发语言
纪念 22911 小时前
C++ 算法基础 (差分)
开发语言·c++·算法
jyOverQ14 小时前
从零学 JVM(六):垃圾回收算法详解——标记清除、标记复制与标记整理
jvm·算法
码农小河6615 小时前
基于PHP的电商后台蜜罐系统
开发语言·php
starzy199015 小时前
深入理解Scala柯里化函数:函数链式变换
开发语言·scala