| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1098 人关注过本帖
标题:谁可以帮我解释一下 这是什么意思啊 我是初学者 不是很懂。
只看楼主 加入收藏
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
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册: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
快速回复:谁可以帮我解释一下 这是什么意思啊 我是初学者 不是很懂。
数据加载中...
 
   



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

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