C++学习笔记(1)——基类、派生类的对象空间
来源:互联网 发布:淘宝包邮够邮费吗 编辑:程序博客网 时间:2024/05/22 02:23
观察下列程序中产生的两个对象的地址空间及其内容
/**//************************************************************************
* 基类和派生类例子
************************************************************************/
#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;
}
* 基类和派生类例子
************************************************************************/
#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)——基类、派生类的对象空间
- C++学习(1)--基类、派生类的对象空间
- 面向对象的程序设计-学习笔记-22-派生类
- OBJ-C类的继承和派生学习笔记
- Java学习笔记——thread派生类的适用
- C++学习笔记(5)——基类、派生类的构造函数、析构函数的调用顺序
- C++学习笔记(2)——派生类的继承权与访问域
- C++学习笔记(4)——派生类的构造函数和析构函数
- PostGIS学习笔记(一)——空间对象的表达方式
- C++学习笔记-类的继承(派生类)
- 抽象,派生类的学习笔记
- 面向对象的程序设计-学习笔记-23-派生类的构造函数和析构函数
- 面向对象与C++程序设计-类的继承与派生学习笔记
- Objective-C 学习笔记(1)——类与对象
- C++学习笔记——继承与派生(二)
- 继承与派生类 学习笔记1
- C++primer学习:面象对象程序设计(1):定义基类和派生类
- Objective-C学习笔记(十九)——对象方法和类方法的相互调用
- nullToSpace,isNull,replaceSpaceToNBSP,getDay
- 一个真实的故事:IT人离开IT还能干什么
- Oracle经验技巧集
- asp.net学习资源
- 别怕我伤心
- C++学习笔记(1)——基类、派生类的对象空间
- Oracle性能调优
- 转:可称1~40磅的4块砝码
- 解决两个难懂的安全性问题
- PHP5新特性: 更加面向对象化的PHP
- [讨论]导完Excel并退出,怎么关闭Excel进程
- Other 全部存档
- Oracle备份与恢复案例
- 有关jsp+struts+hibernate+MSSQLServer项目中Tomcat5的server.xml配置