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;
}
相关推荐
yb0os14 分钟前
RPC实战和核心原理学习(一)----基础
java·开发语言·网络·数据结构·学习·计算机·rpc
liuyao_xianhui12 分钟前
内存管理(C/C++)
java·开发语言·c++
rit843249913 分钟前
基于灰狼算法(GWO)优化支持向量回归机(SVR)参数C和γ的实现
c语言·算法·回归
蒋士峰DBA修行之路15 分钟前
实验五 静态剪枝
数据库·算法·剪枝
蒋士峰DBA修行之路18 分钟前
实验六 动态剪枝
数据库·算法·剪枝
饭碗的彼岸one29 分钟前
C++设计模式之单例模式
c语言·开发语言·c++·单例模式·设计模式·饿汉模式·懒汉模式
Tim_101 小时前
【算法专题训练】20、LRU 缓存
c++·算法·缓存
Vect__1 小时前
从零实现一个简化版string 类 —— 深入理解std::string的底层设计
c++
hope_wisdom1 小时前
C/C++数据结构之栈基础
c语言·数据结构·c++··stack
青铜发条1 小时前
【Qt】PyQt、原生QT、PySide6三者的多方面比较
开发语言·qt·pyqt