给类私有变量赋值

[复制链接]
查看11 | 回复2 | 2009-4-10 13:35:09 | 显示全部楼层 |阅读模式
// Note:Your choice is C++ IDE
#include
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(float x=0,float y=0):real(x),imag(y){}

Complex Add(Complex x)
{
Complex z;
z.real=real+x.real;
z.imag=imag+x.imag;
return z;

void show()
{
cout<<"real is"<<real;
cout<<"imag is"<using namespace std;class Complex{private:float m_real;float m_imag;public:Complex(float x=0,float y=0):m_real(x),m_imag(y){}Complex Add(Complex x){Complex z;z.m_real=m_real+x.m_real;z.m_imag=m_imag+x.m_imag;return z;}void show(){cout<<"real is"<<m_real;...
回复

使用道具 举报

千问 | 2009-4-10 13:35:09 | 显示全部楼层
我晕,你这个类方法不是为私有变量real赋值吗? void Complex::set(float x) { real=x; }给类的私有变量赋值,要么定义类方法, 要么使用友元...
回复

使用道具 举报

千问 | 2009-4-10 13:35:09 | 显示全部楼层
使用友元函数可以给私有成员和保护成员赋值。...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行