嗨 我對C++很陌生。我只寫一個C++代碼這是繼:C++編程問題2
#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
我剛好聲明數據成員公開糾正錯誤;
現在我該怎麼做才能使這件事情與私人成員一起工作? 爲什麼我無法訪問帶有課堂對象的私人成員? 如何直接訪問私有數據成員或爲什麼我不能直接使用類對象的數據成員?它背後的原因是什麼?
class是如何在內存中實現的?班級如何阻止我們或阻止我們使用其私人數據或實現其安全機制?什麼是編譯器在看到一個類時做什麼? 編譯器如何實現該類及其安全機制?
如果你讓他們private
請給我講解一下
你使用什麼教程來學習C++? – 2011-05-18 05:01:43