zoukankan      html  css  js  c++  java
  • 类的构造函数(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;
    }

  • 相关阅读:
    洛谷【P1177】【模板】归并排序
    洛谷【P1177】【模板】快速排序
    洛谷【P1104】生日(冒泡排序版)
    洛谷【P1104】生日(插入排序版)
    洛谷【P1104】生日(选择排序版)
    BZOJ5443:[CEOI2018]Lottery
    ReactNative---ref的用法和技巧
    ios---运用MJRefresh组件设置下拉刷新
    ReactNative---setState与性能的平衡
    ios---设置UITabBarController的字体颜色和大小
  • 原文地址:https://www.cnblogs.com/w413133157/p/1653589.html
Copyright © 2011-2022 走看看