C++学习笔记(1)——基类、派生类的对象空间
2005-01-27 10:39
603 查看
观察下列程序中产生的两个对象的地址空间及其内容
/**//************************************************************************
* 基类和派生类例子
************************************************************************/
#include <IOSTREAM.H>
#include <CONIO.H>
//基类
class CMyBase
...{
int x;
public:
int SetX(int nValue)...{return x=nValue;}
int GetX()...{return x;}
void print()...{cout<<"in the base class : x = "<<x<<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;}
};
main()
...{
CMyBase obj1;
obj1.SetX(1000);
obj1.print();
cout<<"in main function, in the base class : x = "<<obj1.GetX()<<endl;
cout<<endl;
CMyDerive obj2;
obj2.SetX(300);
obj2.print();
obj2.CMyBase::print();
cout<<"in main function, in the derived class : x = "<<obj2.GetX()<<endl;
cout<<"in main function, in the base class : x = "<<obj2.CMyBase::GetX()<<endl;
obj2.CMyBase::SetX(200);
obj2.print();
obj2.CMyBase::print();
cout<<"in main function, in the derived class : x = "<<obj2.GetX()<<endl;
cout<<"in main function, in the base class : x = "<<obj2.CMyBase::GetX()<<endl;
return 0;
}
1.基类和派生类的对象的值
2.基类和派生类的成员函数的地址
3.基类和派生类的对象的地址及其成员变量的地址
/**//************************************************************************
* 基类和派生类例子
************************************************************************/
#include <IOSTREAM.H>
#include <CONIO.H>
//基类
class CMyBase
...{
int x;
public:
int SetX(int nValue)...{return x=nValue;}
int GetX()...{return x;}
void print()...{cout<<"in the base class : x = "<<x<<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;}
};
main()
...{
CMyBase obj1;
obj1.SetX(1000);
obj1.print();
cout<<"in main function, in the base class : x = "<<obj1.GetX()<<endl;
cout<<endl;
CMyDerive obj2;
obj2.SetX(300);
obj2.print();
obj2.CMyBase::print();
cout<<"in main function, in the derived class : x = "<<obj2.GetX()<<endl;
cout<<"in main function, in the base class : x = "<<obj2.CMyBase::GetX()<<endl;
obj2.CMyBase::SetX(200);
obj2.print();
obj2.CMyBase::print();
cout<<"in main function, in the derived class : x = "<<obj2.GetX()<<endl;
cout<<"in main function, in the base class : x = "<<obj2.CMyBase::GetX()<<endl;
return 0;
}
1.基类和派生类的对象的值
2.基类和派生类的成员函数的地址
3.基类和派生类的对象的地址及其成员变量的地址
相关文章推荐
- C++学习笔记_1:派生类与基类关系
- 学习笔记之深入浅出MFC 第8章 C++重要性质----基类与派生类:谈继承(Inheritance)
- 派生类到基类的转换(c++学习笔记)
- C++ Primer 学习笔记_65_面向对象编程 -概述、定义基类跟派生类
- C++学习笔记(6)----基类和派生类的构造函数和析构函数的执行顺序
- C++学习笔记(5)——基类、派生类的构造函数、析构函数的调用顺序
- 派生类到基类的转换(c++学习笔记)
- C++的学习笔记(第七章:类与对象)
- C++学习笔记——类和对象(二)
- C++ 虚函数在基类与派生类对象间的表现及其分析
- Inside The C++ Object Model 学习笔记 -- 关于对象
- 《面向对象基础:C++实现》学习笔记之四
- C++ 基类、派生类对象指针的声明与使用
- 【菜鸟C++学习笔记】10.类、对象和成员
- c++ 学习笔记(47)-C++中两个类中互相包含对方对象的指针问题
- C++ primer第二次阅读学习笔记(第17章:用于大型程序的工具:命名空间)
- C++学习笔记--函数对象
- C++学习笔记——类和对象(一)
- PHP 面向对象程序设计(oop)学习笔记 (五) - PHP 命名空间
- C++学习笔记---内部类与名字空间