基类与派生类对象的关系
2011-01-09 17:32
204 查看
#include <iostream> using namespace std; class A { int a; public: void setA(int x) {a=x;} int getA() {return a;} }; class B:public A { int b; public: void setB(int x) {b=x;} int getB() {return b;} }; void f1(A a,int x) {a.setA(x);} void f2(A *pA,int x) {pA->setA(x);} void f3(A &rA,int x) {rA.setA(x);} void main() { A a1,*pA; B b1,*pB; a1.setA(1); b1.setA(2); a1=b1; cout<<a1.getA()<<endl; cout<<b1.getA()<<endl; a1.setA(10); cout<<a1.getA()<<endl; cout<<b1.getA()<<endl; pA=&b1; pA->setA(20); cout<<pA->getA()<<endl; cout<<b1.getA()<<endl; A &ra=b1; ra.setA(30); cout<<pA->getA()<<endl; cout<<b1.getA()<<endl; b1.setA(7); cout<<b1.getA()<<endl; f1(b1,100); cout<<b1.getA()<<endl; f2(&b1,200); cout<<b1.getA()<<endl; f3(b1,300); cout<<b1.getA()<<endl; }
相关文章推荐
- 关于基类与派生类之间对象、指针等转化关系的小结
- 基类与派生类对象的关系
- 基类对象与派生类对象的使用关系
- 基类中this指针和派生类对象的关系,同名隐藏原则和作用域的示例
- 基类对象和派生类对象之间的赋值关系
- C++:基类与派生类对象之间的赋值兼容关系
- 关于基类与派生类之间对象、指针等转化关系的小结
- C++-继承:基类与派生类对象的关系&&继承与组合
- 关于C++中,基类 派生类的关系 重载 覆盖 面对对象开发的必须要理解的非常好
- 基类对象和派生类对象之间的赋值关系
- C++primer学习:面象对象程序设计(1):定义基类和派生类
- C++中基类,派生类 类成员对象的构造函数,析构函数的顺序
- 虚函数在基类与派生类对象的执行过程
- C++学习笔记(1)——基类、派生类的对象空间
- 派生类与基类的转换关系
- 关于通过不含虚析构函数的基类类型的指针删除派生类对象的问题
- 派生类和基类的关系
- 面向对象--多继承&派生类对象内存布局分析&各基类指针所指向的位置分析(解决面试宝典第四版P147问题)
- 关于基类和派生类之间的关系
- 友元关系与继承以及基类派生类定义构造函数时应该注意的事项