• 类的构造函数(2)


    /*类的构造函数(2)*/

    #include "stdafx.h"
    #include <iostream.h>

    class Color
    {
    public:
        Color()
        {
            cout << this << " color
    无参构造" << endl;
        }
        ~Color()
        {
            cout << this << " color
    析构" << endl;
        }
        int m_nRed;
        int m_nGreen;
        int m_nBlue;
    };

    class MyPoint
    {
    private:
        int m_nx;
        int m_ny;
        Color m_color2;
        Color m_color1;
        //
    成员对象的构造在前,本对象的构造在后
        //
    析构过程相反 成员在后,本对象在前
        //
    构造  客人->自己
        //
    析构  自己->客人
        //
    根据成员对象在类中定义的顺序,先后构造
    public:
        //
    当自定义了一个有参数构造时候,
        //
    编译器不会再提供一个无参默认的构造
        MyPoint(int nx,int ny)
        {
            cout << this << " Point
    有参构造" << endl;
            SetPoint(nx,ny);
        }
        MyPoint()
        {
            cout << this << " Point
    无参构造" << endl;
            m_nx = 0;
            m_ny = 0;
        }
        ~MyPoint()
        {  
            cout << this << " Point
    析构" << endl;
        }
        void SetPoint(int nx,int ny);
        void SetColor(int nIndex,int nred, int ngreen , int nblue);
        int GetPointx();
        int GetPointy();
        Color*  GetColor(int nIndex);
    };

    void MyPoint::SetColor(int nIndex,
                           int nred, int ngreen ,int nblue)
    {
        if ( nIndex == 1 )
        {
            m_color1.m_nRed = nred;
            m_color1.m_nGreen = ngreen;
            m_color1.m_nBlue = nblue;
        }
        else if ( nIndex == 2 )
        {
            m_color2.m_nRed = nred;
            m_color2.m_nGreen = ngreen;
            m_color2.m_nBlue = nblue;
        }
    }

    void MyPoint::SetPoint(int nx,int ny)
    {
        m_nx = nx;
        m_ny = ny;
    }

    int MyPoint::GetPointx()
    {
        return m_nx;
    }

    int MyPoint::GetPointy()
    {
        return m_ny;
    }

    Color* MyPoint::GetColor( int nIndex )
    {
        if ( nIndex == 1 )
        {
            return &m_color1;
        }
        else if( nIndex == 2 )
        {
            return &m_color2;
        }
       
        return NULL;
    }

    int main()
    {
       
        MyPoint Point(10,20);
       
        Point.SetColor(1,255,255,255);
       
        Point.SetColor(2,0,0,0);
       
        cout << "x is" << Point.GetPointx()
            << "y is" << Point.GetPointy() << endl;
       
       
        for ( int nIndex = 0 ; nIndex <= 3 ; nIndex++ )
        {
            Color *pColor = Point.GetColor(nIndex);
           
            if ( pColor )
            {
                cout << "color" << nIndex << " is" << " " << pColor->m_nRed
                    << " " << pColor->m_nGreen << " " << pColor->m_nBlue << endl;
            }
        }
       
        cout << "Color1 Address " << Point.GetColor(1) << endl;
       
        cout << "Color2 Address " << Point.GetColor(2) << endl;
       
        return 0;
    }

  • 相关阅读:
    查看本机安装了哪些.Net Framework版本的方法
    tomcat启动超时, Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...
    设置PL/SQL Developer 字符集
    JDK安装与环境变量配置
    function 与 => 的区别
    如何使用tomcat,使用域名直接访问javaweb项目首页
    Spring整合Struts2
    Oracle to_char函数的使用方法
    PLSQL 误删表恢复操作
    Python可视化动态图表,基于Python环境,使用jupyter notebook编辑ipynb文件,基于pyecharts生成Sankey的桑基图HTML制作
  • 原文地址:https://www.cnblogs.com/w413133157/p/1653589.html
Copyright © 2020-2023  润新知