c ++编程问题2

时间:2011-05-18 04:52:32

标签: c++ compiler-construction

您好 我对c ++很新。我只是写一个c ++代码,其中包括:

#include<iostream>
using namespace std;
class complex
{
 float x,y;
};
int main ()
{
  complex a; 
  a.x=500;
   a.y=600;
cout<<" the value of a.x = "<<a.x<<"\n" ;
cout<<" the value of a.y = "<<a.y<<"\n" ;

return 0;
}

当我编译程序时,它会给我以下错误:

try.cpp: In function ‘int main()’:
try.cpp:5: error: ‘float complex::x’ is private
try.cpp:10: error: within this context
try.cpp:5: error: ‘float complex::y’ is private
try.cpp:11: error: within this context
try.cpp:5: error: ‘float complex::x’ is private
try.cpp:12: error: within this context
try.cpp:5: error: ‘float complex::y’ is private
try.cpp:13: error: within this context 

我通过声明数据成员公开来解决错误;

现在该怎么办才能使这个东西与私人成员合作? 为什么我不能用类的对象访问私有成员? 我如何直接访问私有数据成员或为什么我不能直接使用类对象的数据成员?它背后的原因是什么?

如何在内存中实现类?班级如何阻止我们或阻止我们使用其私人数据或实施其安全机制?什么是编译器什么时候看到一个类? 编译器如何实现类及其安全机制?

请向我解释

6 个答案:

答案 0 :(得分:3)

  

现在该怎么办才能使这个东西与私人成员合作?

如果你创建它们private,那么你可以编写构造函数并为你的类添加其他有用的函数,以便对复杂对象进行操作:

class complex
{
   float _x;
   float _y;
   public:
      complex(float x=0.0, float y=0.0) : _x(x), _y(y) {}
                                       //^^^^^^^^^^^^^its initialization list!

      complex operator + (const complex & c)
      {
          return complex(_x + c._x, _y + c._y);
      }
      ostream & print(ostream & out) const
      {
          return out << "(" << _x << ", "<< _y << ")";
      }
      void print() const
      {
          print(cout);
          cout << endl;
      }

      //and so on
};

ostream & operator << (ostream & out, const complex &c)
{
    return c.print(out);
}

测试它:

int main ()
{
  complex a(500,600);
  complex b(80,60);
  complex c = a + b; //operator+() gets invoked!

  a.print();
  b.print();
  c.print();

  cout << "\nprint using cout!" << endl;
  cout << a << endl;
  cout << b << endl;
  cout << c << endl;
  return 0;
}

输出:

(500,600)
(80,60)
(580,660)

print using cout!
(500,600)
(80,60)
(580,660)

参见在线演示:http://www.ideone.com/TiGat

答案 1 :(得分:0)

您的a.xa.y正在尝试访问课堂外的xy成员。如果他们不公开,那些尝试将会失败,如你所见。

如果您希望默认访问权限是公开的而非私密的,请使用struct代替class

答案 2 :(得分:0)

默认情况下,您在类中定义的字段是“私有”,并且只能由该类的方法使用。

您需要将x和y定义为该类的“公共”成员。

class complex
{
 public:
 float x,y;
};

答案 3 :(得分:0)

publicprivateprotected是编译时的概念。在运行时,它们甚至都不存在。清理完毕后,让我们继续讨论无法访问private数据的原因。

private就是它所说的 - 私人。这是完全的,除了班级本身之外,任何地方都无法访问。默认情况下,class是私有的,除非另有说明 如果您希望默认为public,请选择struct。除了默认的“隐私”,它们完全相同。

struct complex{
  float x;
  float y;
};

现在您可以从任何地方访问成员。


对于更多内容,以及我对您的问题的印象,似乎是really really need a good C++ book

答案 4 :(得分:0)

数据保护和抽象是面向对象编程的基本概念。除了Mark的答案之外,我建议你阅读任何你最喜欢的C ++编程书籍,如Eric Nagler的C ++,A Hands On Approach或C ++ Primer等。

答案 5 :(得分:0)

要访问班级的私人会员,您可以:

  1. 提供公共方法来读取和写入私有成员的价值。通过这种方式,该类可以完全控制访问操作。
  2. 提供公共方法以返回私有成员的指针或引用。这不是一个好方法,因为对象的状态可能会被破坏。
  3. 将类或函数清除为包含私有成员的类的朋友,以便朋友可以访问所有成员。确保朋友在班级的数据成员上正确运作。
  4. 私有,受保护和公共只在编译时生效,防止错误代码破坏对象的状态。他们没有运营商安全控制的业务。

    我们举个例子,看下面的简单堆栈类:

    class statck
    {
    private:
        static const int buffersize = 100;
        int buffer[buffersize];
        int count;
    public:
        stack();
        bool Push(int value);
        bool Pop(int value);
        int GetCount();
    }
    

    count memeber在逻辑上表示已将多少值推入堆栈,它的值由Push,Pop和构造函数管理,不应使用堆栈类对象通过代码更改,如:

    statck s;
    s.count = 10;
    

    代码上行在逻辑上毫无意义。如果计数是公开的,则可以编译代码,并且更难以揭示错误,并且通过将计数视为私有,错误是显而易见的并且导致编译错误。

    完成所有访问修饰符(public,protected和private)是告诉编译器,类的whitch成员可能出现在类的成员方法以外的代码中的某些表达式中。我没有使用“访问”这个短语,我认为这让你很困惑。

    访问修饰符不会影响编译器生成的用于表示对象的内存布局。严格来说,布局取决于编译器和平台的字大小,但通常在32位系统上,statck对象将是一个404字节长的内存块,400字节用于缓冲区,4字节用于计数,无论缓冲区和计数是公共的或私人的。当程序运行时,每个获取404字节内存块地址的代码都可以读写它们,私有代码与此无关。