标题:访问权限问题。。。
取消只看楼主
wdalin
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2006-7-12
 问题点数:0 回复次数:1 
访问权限问题。。。

#include <cstdlib>
#include <iostream>

using namespace std;

class Complex{
public:
Complex(double,double);
Complex(const Complex &c);
void print(int i);
private:
double real,imag;
};

Complex::Complex(double r,double i)
{
real=r;
imag=i;
cout<<"constructor(1) called:real="<<real<<",imag="<<imag<<endl;
}

Complex::Complex(const Complex &c)
{
real=c.real; //这里real 和 imag 是私有数据成员 对象怎么可以直接访问呢?
imag=c.imag;
cout<<"constructor(2) called:real="<<real<<",imag="<<imag<<endl;
}

void Complex::print(int i)
{
if( imag<0 )
cout<<"c"<<i<<"="<<real<<imag<<"i"<<endl;
else
cout<<"c"<<i<<"="<<real<<"+"<<imag<<"i"<<endl;
}

int main(int argc, char *argv[])
{
Complex c1(6,-8),c2=c1;

c1.print(1);
c2.print(2);

system("PAUSE");
return EXIT_SUCCESS;
}


程序中注释的问题到底是怎么回事呢?

搜索更多相关主题的帖子: 访问 权限 
2006-08-14 23:18
wdalin
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2006-7-12
得分:0 

就是说在public:域下的函数体内可以通过类对象来访问私有数据成员吗?

2006-08-14 23:47



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




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

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