免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1520 | 回复: 0
打印 上一主题 下一主题

开始尝试面向对象的编程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-12 12:06 |只看该作者 |倒序浏览
学c++也很久了,不过一直都是些皮毛。现在打算深入下去,所以准备写些有意思的代码,训练自己的思维。
c++的精髓一般很难把握,而且由于它兼容C代码,搞得很多人都用c的思维方式来写c++的代码。
我决定改变这一点。当然要从自身做起。从今天起,我要陆陆续续贴些代码上来,争取用面向对象的方式来完成这个项目。
//filename: cell.h
#ifndef _MAC_CELL
#define _MAC_CELL
#include
#include
#include
namespace std {} using namespace std;
const int CELL1=0xa9b0; //┌
const int CELL2=0xa9a4; //─
const int CELL3=0xa9d0; //┬
const int CELL4=0xa9b4; //┐
const int CELL5=0xa9a6; //│
const int CELL6=0xa9c0; //├
const int CELL7=0xa9c8; //┤
const int CELL8=0xa9e0; //┼
const int CELL9=0xa9b8; //└
const int CELL10=0xa9d8;//┴
const int CELL11=0xa9bc;//┘
class pos
{
private:
  int x;
  int y;
public:
  pos();
  pos(int lx, int ly);
  pos(pos & s);
      void operator=(pos s);     
  int getX();
  int getY();
};
class cell
{
private:
  pos start;
  int width;
  int height;
  wstring content;
public:
  cell();
  cell(int x, int y,int w,int h);
  cell(pos s,int w,int h);
  ~cell();
  int getWidth();
  int getHeight();
  wstring getContent();
  void setWidth(int w);
  void setHeight(int h);
  void setContent(wstring s);
};
#endif /* _MAC_CELL */
--------------cut here -----------------------
//filename:cell.cpp
#include
pos::pos():
x(0),y(0)
{
}
pos::pos(int lx,int ly):
x(lx),y(ly)
{
}
pos::pos(pos & s)
{
x=s.getX();
y=s.getY();
}
void pos::operator=(pos s)
{
x=s.getX();
y=s.getY();
}
int pos::getX()
{
return x;
}
int pos::getY()
{
return y;
}

cell::cell():
start(0,0),width(0),height(0),content()
{
}
cell::cell(int x,int y,int w,int h):
start(x,y),width(w),height(h),content()
{
}
cell::cell(pos s,int w,int h):
start(s),width(w),height(h),content()
{

}
int cell::getWidth()
{
return width;
}
int cell::getHeight()
{
return height;
}
wstring cell::getContent()
{
return content;
}
void cell::setWidth(int w)
{
width=w;
}
void cell::setHeight(int h)
{
height=h;
}
void cell::setContent(wstring s)
{
content=s;
}
cell::~cell()
{
}
--------------------cut here--------------------------------
//filename:test.cpp
#include
int main()
{
cell a(0,0,10,10);
a.setContent(L"this is just a test");
cout

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4353/showart_20852.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP