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;
}
相关推荐
@大迁世界3 分钟前
TypeScript 的本质并非类型,而是信任
开发语言·前端·javascript·typescript·ecmascript
栗子叶8 分钟前
Java对象创建的过程
java·开发语言·jvm
零小陈上(shouhou6668889)12 分钟前
K-近邻算法 - lazy learning的代表
算法·近邻算法
Amumu1213817 分钟前
React面向组件编程
开发语言·前端·javascript
有一个好名字18 分钟前
力扣-从字符串中移除星号
java·算法·leetcode
IT=>小脑虎18 分钟前
Python零基础衔接进阶知识点【详解版】
开发语言·人工智能·python
wjs202420 分钟前
C 标准库 - `<float.h>》详解
开发语言
萧瑟其中~22 分钟前
二分算法模版——基础二分查找,左边界查找与右边界查找(Leetcode的二分查找、在排序数组中查找元素的第一个位置和最后一个位置)
数据结构·算法·leetcode
码上就好ovo24 分钟前
Atcoder Beginnner Contest 440
算法