• C++ 模块累积的理解


    通俗的解释就是元素构成小模块,小模块构成大模块。
    析构的过程正好相反,大模块析构后小模块被暴露,小模块再被析构
    要注意:先构造的后析构

    #include <bits/stdc++.h>
    using namespace std;
    class Point{///定义一个类对象相当于一个模块
    	private:
    		int x,y;///类对象的数据成员,对外不可见
    	public:///类对象的对外接口
        ///构造函数:与类同名,初始化对象的数据成员,使得小模块形成完整模块
    		Point(int px,int py):x(px),y(py){
    		  cout<<"普通构造函数生成"<<this<<endl;;
    		  ShowPoint();
    		}
        ///拷贝构造函数:完成完整模块之间的复制
    		Point(Point& p):x(p.x),y(p.y){
    		  cout<<"拷贝构造函数生成"<<this<<endl;
    		  ShowPoint();
    		}
    		void ShowPoint(){ cout<<"("<<x<<","<<y<<")"<<this<<endl;}
            int getx(){return x;}
            int gety(){return y;}
    		~Point(){cout<<"析构函数调用"<<this<<endl;}
    };
    class Line{
        private:
            Point p1;
            Point p2;
            double len;
        public:
        ///pa->xp1 pb->xp2 拷贝构造函数
        ///xp1->p1 xp2->p2 拷贝构造函数
            /*Line(Point xp1,Point xp2):p1(xp1),p2(xp2){///构造函数
                cout<<"Line的构造函数"<<this<<endl;
            };
            Line(Line &L):p1(L.p1),p2(L.p2){
                cout<<"Line的拷贝构造函数"<<this<<endl;
            }*/
            Line(Point xp1,Point xp2);
            Line(Line &L);
            ~Line(){cout<<"Line的析构函数"<<this<<endl;}
            double getlen() {return len;}
    
    };
    ///::预作用符 表示Line属于Line类
    ///内联函数:class内必须声明才能使用
    Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){
         cout<<"Line的构造函数"<<this<<endl;
         double x=p1.getx()-p2.getx();
         double y=p1.gety()-p2.gety();
         len=sqrt(x*x+y*y);
    }
    ///p1(L.p1)调用Point的拷贝构造函数
    Line::Line(Line &L):p1(L.p1),p2(L.p2){
        cout<<"Line的拷贝构造函数"<<this<<endl;
    }
    void ShowPointInfo(Point p){
    	cout<<"ShowPointInfo begin"<<endl;
    	p.ShowPoint() ;
        cout<<"ShowPointInfo end"<<endl;
    }
    ///加Line的拷贝构造函数L2(L1)
    int main(){
    	/*Point pa(3,4);
    	ShowPointInfo(pa);
        cout<<endl;*/
        Point pa(3,4);
        Point pb(10,9);
    
        Line L1(pa,pb);
        cout<<"L2***********"<<endl;
        Line L2(L1);
        /*cout<<"L1 start point:";
        pa.ShowPoint();
        puts("");
    
        cout<<"L1 end point:";
        pb.ShowPoint();
        puts("");
    
        cout<<"The lengh of L1 is:"<<L1.getlen()<<endl;*/
    	return 0;
    }
    
    /**以下结合this指针分析程序执行过程
    运行结果:
    普通构造函数生成0x6dfec8 ->根据参数构造pa
    (3,4)0x6dfec8
    普通构造函数生成0x6dfec0 ->根据参数构造pb
    (10,9)0x6dfec0
    拷贝构造函数生成0x6dfed0 ->根据pb生成xp2
    (10,9)0x6dfed0
    拷贝构造函数生成0x6dfed8 ->根据pa生成xp1
    (3,4)0x6dfed8 
    拷贝构造函数生成0x6dfea8 ->根据xp1生成p1
    (3,4)0x6dfea8
    拷贝构造函数生成0x6dfeb0 ->根据xp2生成p2
    (10,9)0x6dfeb0
    Line的构造函数0x6dfea8 ->构造出L1
    析构函数调用0x6dfed8 ->L1构造完成,xp1析构
    析构函数调用0x6dfed0 ->xp2析构
    L2*********** ->以下为L2的拷贝构造
    拷贝构造函数生成0x6dfe90  ->根据L1.p1构造L2的p1
    (3,4)0x6dfe90
    拷贝构造函数生成0x6dfe98 ->根据L1.p2构造L2的p2
    (10,9)0x6dfe98
    Line的拷贝构造函数0x6dfe90 ->L2拷贝构造成功
    Line的析构函数0x6dfe90 ->L2析构
    析构函数调用0x6dfe98 ->L2.p2析构
    析构函数调用0x6dfe90 ->L2.p1析构
    Line的析构函数0x6dfea8 ->L1析构
    析构函数调用0x6dfeb0 ->L1.p2析构
    析构函数调用0x6dfea8 ->L1.p1析构
    析构函数调用0x6dfec0 ->pa析构
    析构函数调用0x6dfec8 ->pb析构
    **/
    
    
    
  • 相关阅读:
    二叉平衡树
    红黑树
    [leetcode] LCP 比赛
    二叉搜索树
    面向对象的二叉树的实现
    二叉树的序列化与反序列化
    [leetcode] 基本计算器
    【pandas】玩转一行拆多行,多行并一行(分分合合你说了算)
    【VBA】数据溢出与解决
    【VBA】criterial 未找到命名参数
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853169.html
Copyright © 2020-2023  润新知