想要实现2个结果互换,都是结果没有变。

想要实现2个结果互换,都是结果没有变。

#pragma once
class people
{
public:
    virtual float getCK();//存款
    virtual void setput();//输入    
    virtual void display();//输出
    people();
    ~people();
};

class QFR :public people
{
private:
    float j;
    float h;
public:
    QFR();
    float  getCK();//存款
    void setput();//输入    
    void display();//输出
    ~QFR();
};
class QDD :public people
{
private:
    float j;
    float h;
public:
    QDD();
    float  getCK();//存款
    void setput();//输入    
    void display();//输出
    ~QDD();
};
#include<iostream>
using namespace std;
#include"b.h"
template<typename T>
void Change(T a,T b)
{    
    T c;
    c = a;
    a = b;
    b = c;
}

float people::getCK()
{
    return 0.0;
}
void people::setput()
{
    cout << "NULL" << endl;
}
void people::display()
{
    cout << "NULL" << endl;
}

people::people()
{
    cout << "people构造函数" << endl;
}
people::~people()
{
    cout << "people析构函数" << endl;
}

QFR::QFR()
{
    j = 0.0;
    h = 0.0;
}
void QFR::setput()
{
    cout << "钱夫人基本工资" << endl;
    cin >> j;
    cout << "钱夫人工作时间" << endl;
    cin >> h;
}
float QFR::getCK()
{
    return  j + h * 60;
}

void QFR::display()
{

    cout << "钱夫人基本工资:" << j << endl;
    cout << "钱夫人工作时间:" << h << endl;
    cout << "钱夫人存款:" << getCK() << endl;

}
QFR::~QFR()
{
    cout << "钱夫人析构函数" << endl;
}
        QDD::QDD()
        {
            j = 0.0;
            h = 0.0;
        }
        void QDD::setput()
        {
            cout << "钱多多基本工资" << endl;
            cin >> j;
            cout << "钱多多工作时间" << endl;
            cin >> h;
        }
        float QDD::getCK()
        {
            return  j + h * 30;
        }

        void QDD::display()
        {

            cout << "钱多多基本工资:" << j << endl;
            cout << "钱多多工作时间:" << h << endl;
            cout << "钱多多存款:" << getCK() << endl;

        }

        QDD::~QDD()
        {
            cout << "钱多多析构函数" << endl;
        }
int main()
{
    QFR d;
    d.setput();
    d.getCK();
    d.display();
    cout << "------------------" << endl;
    QDD f;
    f.setput();
    f.getCK();
    f.display();
    cout << "------------------" << endl;
    Change(f.getCK(), d.getCK());
    cout << "钱多多存款:" << f.getCK() << endl;
    cout << "钱夫人存款:" << d.getCK() << endl;
}


你这是值传递,而且要交换两个对象的工资,你计算工资用的成员函数,成员变量没变,计算结果当然也不会改变

仅供参考:

#include <stdio.h>
#define SWAP(a,b) do ((&(a))!=(&(b)))?((a)^=(b)^=(a)^=(b)):((a)=(a)); while (0)
char   *p1="1" ,*p2="2" ;
char    c1=1   , c2=2   ;
short   s1=1   , s2=2   ;
int     i1=1   , i2=2   ;
__int64 I1=1i64, I2=2i64;
float   f1=1.0f, f2=2.0f;
double  d1=1.0 , d2=2.0 ;
void main() {
    SWAP((int)p1,(int)p2);                printf("char *     %5s,   %5s\n",p1,p2);
    SWAP(c1,c2);                          printf("char       %5d,   %5d\n",c1,c2);
    SWAP(s1,s2);                          printf("short      %5d,   %5d\n",s1,s2);
    SWAP(i1,i2);                          printf("int        %5d,   %5d\n",i1,i2);
    SWAP(I1,I2);                          printf("__int64 %5I64d,%5I64d\n",I1,I2);
    SWAP(*(int     *)&f1,*(int     *)&f2);printf("float      %5g,   %5g\n",f1,f2);
    SWAP(*(__int64 *)&d1,*(__int64 *)&d2);printf("double    %5lg,  %5lg\n",d1,d2);

    SWAP(c1,c1);
    printf("%d\n",c1);
}
//char *         2,       1
//char           2,       1
//short          2,       1
//int            2,       1
//__int64     2,    1
//float          2,       1
//double        2,      1
//2