标题:谁可以帮我解释一下 这是什么意思啊 我是初学者 不是很懂。
只看楼主
szyzj
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-4-1
结帖率:0
已结贴  问题点数:20 回复次数:1 
谁可以帮我解释一下 这是什么意思啊 我是初学者 不是很懂。
#include "stdafx.h"
#include<iostream>
using namespace std;

class complex {
public:
    complex() { real = 0, image = 0; }
    complex(double r, double i) { real = r; image = i; }
    friend complex add(complex &c1, complex  &c2);
    void display();
private:
    double real;
    double image;
};
complex add(complex &c1, complex  &c2) {
    complex c3;
    c3.real = c1.real + c2.real;
    c3.image = c1.image + c2.image;
    return c3;
}
void complex::display() {
    cout << real << "+" << image << "i" << endl;
}
int main() {
    complex c1(3, 4), c2(6, 8), c3;
    c3 = add(c1, c2);
    cout << "c1=", c1.display();
    cout << "c2=", c2.display();
    cout << "c3=", c3.display();
    return 0;
}
搜索更多相关主题的帖子: complex real image display cout 
2018-04-01 20:27
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
看到抹黑C++的代码,就想将之纠正过来

程序代码:
#include <iostream>

class complex
{
public:
    complex( double real=0, double imag=0 ) : real_(real), imag_(imag)
    {
    }
private:
    double real_, imag_;

    friend complex operator+( const complex& a, const complex& b );
    friend std::ostream& operator<<( std::ostream& os, const complex& cpl );
};

complex operator+( const complex& a, const complex& b )
{
    return complex( a.real_+b.real_, a.imag_+b.imag_ );
}

std::ostream& operator<<( std::ostream& os, const complex& cpl )
{
    return os << cpl.real_ << '+' << cpl.imag_ << 'i';
}

using namespace std;

int main( void )
{
    complex c1(3, 4), c2(6, 8);
    complex c3 = c1 + c2;
    cout << "c1 = " << c1 << '\n'
         << "c2 = " << c2 << '\n'
         << "c3 = " << c3 << endl;
}

2018-04-02 09:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-485717-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.101808 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved