作者dendrobium (石斛兰)
看板C_and_CPP
标题Re: [问题]有关class的小问题(c++)
时间Sun Feb 15 22:29:03 2009
※ 引述《starmovie (盗族)》之铭言:
: 以下是原本的程式码
: #include <iostream>
: #include <cstdlib>
: using namespace std;
: class CWin
: {
: public:
private:
: char id;
: int width,height;
: int area()
: {
: return width*height;
: }
: void show_area(void)
: {
: cout << "window " << id << ", area=" << area() << endl;
: }
friend void show_area(const CWin & cw)
{
cout << "window " << cw.id << ", area=" << cw.width*cw.height << endl;
}
: void set_data(char i,int w,int h)
: {
: id=i;
: width=w;
: height=h;
: }
: void set_data(char i)
: {
: id=i;
: }
: void set_data(int w,int h)
: {
: width=w;
: height=h;
: }
: };
: int main(void)
: {
: CWin win1,win2;
: win1.set_data('A',50,40);
: win2.set_data('B');
: win2.set_data(80,120);
: win1.show_area();
show_area(win1);
: win2.show_area();
: system("pause");
: return 0;
: }
: 现在想把id,width,height的成员存取属性改成private
: 然後也把set_data()改成friend函数,可是我想很久想不出来怎麽改= =
: 麻烦各位大大指点一下= =
: 谢谢!!
试试看吧
如果可以的话 可以试着把宣告和定义分开
--
没有吧?!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.170.244.36
1F:→ starmovie:可是D大好像没改set_data()~我主要想改这个= = 02/15 22:46
2F:→ starmovie:不过还是谢谢D大的回应~ 02/15 22:46
3F:→ cfbbq:friend void set_data(char i,int w,int h,const CWin& cw) 02/15 22:54
4F:→ starmovie:请问一下是把win1当成set_data引数,然後里面内容写 02/15 23:11
5F:→ starmovie:cw.id=i 类似这样的吗? 02/15 23:11
6F:→ dendrobium:既然是set data, cw就不会是const啦 02/15 23:15
7F:→ dendrobium:你写写看就知道罗 不难滴~ 02/15 23:16
8F:→ starmovie:嗯嗯~谢谢阿 02/16 01:06
9F:→ cfbbq:sorry,没注意看,没有const。 02/16 09:21