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;
    }

  • 相关阅读:
    js规范
    JS多个函数多个参数如何动态调用,apply的灵活应用
    我是插件狂人,jDuang,jValidator,jModal,jGallery
    Button在IE6、7下的自适应宽度问题解决方法
    100个直接可以拿来用的JavaScript实用功能代码片段
    docker
    jsonp实现post跨域请求
    setInterval倒计时10s
    CI框架源码解读(1)-入口文件index.php
    为什么开始源码阅读
  • 原文地址:https://www.cnblogs.com/w413133157/p/1653589.html
Copyright © 2011-2022 走看看