注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

小可尼の博客

Linux后端的技术窝窝

 
 
 

日志

 
 

类的构造与析构的例子(一)(原创)  

2012-06-27 12:31:01|  分类: C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

题目:谭浩强C++ P396例12.1

自己的编写:

#include <iostream>
using namespace std;
class Point
{
        public:
                Point()
                {
                        x=0.0;
                        y=0.0;
                }
                ~Point()
                {
                        cout<<"call Point deconstructor fun:"<<endl;
                }
                Point(float a,float b)
                {
                        x=a;
                        y=b;
                        cout<<"call Point constructor fun:"<<endl;
                }
                float getX() const {return x;}
                float getY() const {return y;}
                friend ostream &operator<<(ostream &,const Point &);
        protected:
                float x;
                float y;
};

ostream &operator<<(ostream &output,const Point &A)
{
        output<<"The Point is :"<<"("<<A.getX()<<","<<A.getY()<<")"<<endl;
        return output;
}

class Circle:public Point
{
        public:
                Circle()
                {
                        radius=0.0;
                        Point();
                }
                ~Circle()
                {
                        cout<<"call Circle deconstructor fun:"<<endl;
                }
                Circle(float a,float b,float c):Point(a,b)
                {
                        //setPoint(a,b);
                        radius=c;
                        cout<<"call Circle constructor fun:"<<endl;
                }
                float getX() const {return x;}
                float getY() const {return y;}
                float getR() const {return radius;}
                friend ostream &operator<<(ostream &,const Circle &);

        protected:
                float radius;

};

ostream &operator<<(ostream &output,const Circle &C)
{
        output<<"The Circle is :"<<"("<<C.getX()<<","<<C.getY()<<","<<C.getR()<<")"<<endl;
        return output;
}

        public:
                Cylinder()
                {
                        Circle();
                        height=0.0;
                }
                ~Cylinder()
                {
                        cout<<"call Cylinder destructor fun:"<<endl;
                }
                Cylinder(float a,float b,float c,float d):Circle(a,b,c)
                {
                        //SetCircle(a,b,c);
                        height=d;
                        cout<<"call Cylinder constructor fun:"<<endl;
                }
                float getX(){return x;}
                float getY(){return y;}
                float getR(){return radius;}
                float getH(){return height;}
                friend ostream &operator<<(ostream &,Cylinder &);
        private:
                float height;

};

ostream &operator<<(ostream &output,Cylinder &Cy)
{
        output<<"The Cylinder is :"<<"("<<Cy.getX()<<","<<Cy.getY()<<","<<Cy.getR()<<","<<Cy.getH()<<")"<<endl;
        return output;
}


int main()
{
        Point P(10,10);  //Point(float a,float b)
        Circle C(20,20,20);//Circle(float a,float b,float c):Point(a,b)
        Cylinder Cy(30,30,30,30);//Cylinder(float a,float b,float c,float d):Circle(a,b,c)

        cout<<P<<endl;
        cout<<C<<endl;
        cout<<Cy<<endl;

        return 0;
}

 

小结:构造步骤:由里往外

                          1)先构造Point 类;

                         2)再构造Circle类:因为Point是Circle的直接子类,所以先构造Point类,在构造Circle类;

                          3)再构造Cylinder类:因为Point是Cylinder是间接子类,Circle是Cylinder的直接之类,

                               所以有最里面的基类开始构造,其顺序为:Point--------->Circle--------->Cylinder;

          析构步骤 :由外往里

                           1)先析构Cylinder类:Cylinder------------>Circel---------->Point; 

                          2)再析构Citcle类:Circle------------->Point;

                          3)再析构Point类:Point;   

 

 

程序输出:

call Point constructor fun:
call Point constructor fun:
call Circle constructor fun:
call Point constructor fun:
call Circle constructor fun:
call Cylinder constructor fun:
The Point is :(10,10)

The Circle is :(20,20,20)

The Cylinder is :(30,30,30,30)

call Cylinder destructor fun:
call Circle deconstructor fun:
call Point deconstructor fun:
call Circle deconstructor fun:
call Point deconstructor fun:
call Point deconstructor fun:

  评论这张
 
阅读(168)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018