码迷,mamicode.com
首页 > 其他好文 > 详细

类对象做函数参数(传值和传引用)

时间:2015-04-27 18:13:14      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

一、传引用

#include "stdafx.h"
#include <iostream>
using namespace std;

class A
{
public:
    virtual void show(){cout<<"A"<<endl;}
};
class B:public A
{
public:
    virtual void show(){cout<<"B"<<endl;}
};
class C:public A
{
public:
    virtual void show(){cout<<"C"<<endl;}
};

void show(A &a)   //void show (A a)
{
    a.show();
}

int main()
{
    A a;
    B b;
    C c;
    show(a);
    show(b);
    show(c);
    return 0;
}

执行结果:

技术分享

二、传值

#include "stdafx.h"
#include <iostream>
using namespace std;

class A
{
public:
    virtual void show(){cout<<"A"<<endl;}
};
class B:public A
{
public:
    virtual void show(){cout<<"B"<<endl;}
};
class C:public A
{
public:
    virtual void show(){cout<<"C"<<endl;}
};

void show (A a)//void show(A &a)   //void show (A a)
{
    a.show();
}

int main()
{
    A a;
    B b;
    C c;
    show(a);
    show(b);
    show(c);
    return 0;
}

技术分享

类对象做函数参数(传值和传引用)

标签:

原文地址:http://www.cnblogs.com/audi-car/p/4460789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!