6: Initialization and Cleanup

1.constructor

In C++, the class designer can guarantee initialization of every object by providing a special function called the constructor whose name is the same as class's name.

class A
{
private:
	int a;
public:
	A();
};



int main()
{
	A ta;
}
In this simple example, storage is allocated for the object. And the compiler quietly inserts the call to A::A() for the object a at the point of definition. Like any member function, the first(secret) argument to the constructor is the this pointer - the address of the object for which it is being called.

Notice: If you have a constructor, the compiler ensures that construction always happend. If there are no constructors for a class, the compiler will automatically create one for you.

2.destructor

The destructor never has any arguments because destruction never needs any options.

3.no return value

Both the constructor and destructor are very unusual types of functions: they have no return value. This is distinctly diffenent from a void return value, in which the function returns nothing but you still have the option to make it something else.

VC mfc单文档中代码如下void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); for (int i=0;i<points.size();i++){ color=RGB(rand()%6,rand()%6,rand()%6); r=rand()F+5; br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(points[i].x-r,points[i].y-r,points[i].x+r,points[i].y+r); br.DeleteObject(); } // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /pDC/, CPrintInfo* /pInfo/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /pDC/, CPrintInfo* /pInfo/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center=point; r=rand()F+5;//r=5~50 color=RGB(rand()%6,rand()%6,rand()%6); points.push_back(center); SetTimer(1,200,NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for(int i=0;i<points.size();i++) { points[i].y-=5; if(points[i].y<-r) { points.erase(points.begin()+i); i--; } } } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1){ RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); },怎么修改此代码能实现每次单击鼠标出现的圆大小和颜色随机,但在圆的上升过程中大小和颜色都为刚开始单击鼠标时的大小和颜色,不会不停的变换。并且前面单击鼠标出现的圆的颜色和大小不会随着后面单击鼠标出现的圆的颜色和大小改变而改变。给出完整步骤及代码
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值