C++ラーニング(1)--ベースクラス、派生クラスのオブジェクト空間

2046 ワード

#include<iostream>
#include<stdio.h>
using namespace std;
//  
class CMyBase
{
    int x;
    int y;
public:

    int SetX(int nValue){return x=nValue;}
    int GetX(){return x;}
    int SetY(int nValue){return y=nValue;}
    int GetY(){return y;}
    void print(){cout<<"in the base class : x = "<<x<<endl;}
    void printY(){cout<<"in the base class : y = "<<y<<endl;}
};

//   
/**     */
//                 
//                  
//                 
/**    */
//                 
//                  
//                 
class CMyDerive:public CMyBase
{
    int x;    //                  
public:
    int SetX(int nValue){return x=nValue;}
    int GetX(){return x;}
    //             
    void print(){cout<<"in the derive class : x = "<<x<<endl;}
    void printY(){cout<<"in the derive class : y = "<<GetY()<<endl;}
};

int main()
{
    CMyBase obj1;
    obj1.SetX(1000);
    obj1.print();//1000
    cout<<"in main function, in the base    class : x = "<<obj1.GetX()<<endl;//1000
    cout<<endl;

    CMyDerive obj2;
    obj2.SetX(300);//          x
    obj2.print();//300

    obj2.SetY(123456);
    obj2.printY();//         
    obj2.CMyBase::printY();//        
    /*
    **       ,                           
    **
    */
    obj2.CMyBase::print();//4273296        x     ,            
     cout<<"in main function, in the derived class : x = "<<obj2.GetX()<<endl;//300
    cout<<"in main function, in the base    class : x = "<<obj2.CMyBase::GetX()<<endl;//4273296

    obj2.CMyBase::SetX(200);//   
    obj2.print();//300
    obj2.CMyBase::print();//200
    cout<<"in main function, in the derived class : x = "<<obj2.GetX()<<endl;//300
    cout<<"in main function, in the base    class : x = "<<obj2.CMyBase::GetX()<<endl;//200

    return 0;
}