Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233610
  • 博文数量: 127
  • 博客积分: 34
  • 博客等级: 民兵
  • 技术积分: 655
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-03 10:53
文章分类

全部博文(127)

文章存档

2013年(19)

2012年(108)

分类:

2012-12-19 23:15:58

原文地址:继承 作者:青竹玉简

#include
using namespace std;
class A
{
 protected:
  int x;
  int y;
 public:
  static int z;
 public:
  void setA(int x1,int y1)
  {
  // cout<<"sizeof(*this)="<
//   cout<<"this="<   x=x1;
   y=y1;
  
  }
  void printA()
  {
   cout<<"x="<  
   cout<<"y="<
 //  cout<<"sizeof(*this)="<   
 //  cout<<"this="<  }
  A()
  {
  
   cout<<"A无参构造函数"<  
  }
  ~A()
  {
  
   cout<<"A析构"<  }
  A(int x1,int y1)
  {
   x=x1;
   y=y1;
  
   cout<<"A有参构造函数"<  }
};
int A::z=0;
class B:protected A
{
 public:
  int m;
  int n;
  A a;
  void setB(int m1,int n1)
  {
     x=1000;
   y=2000;
   m=m1;
   n=n1;
   setA(100,200); 
   cout<<"sizeof(*this)="<
   cout<<"this="<  
  }
  void printB()
  {
   printA();
   cout<<"m="<  
   cout<<"n="<
   //cout<<"sizeof(*this)="<
  // cout<<"this="<  }
  B()
  {
  
   cout<<"B无参构造函数"<  }
  B(int x1,int y1,int m1,int
    n1):A(x1,y1)
  {
   m=m1;
   n=n1;
   
   cout<<"B有参构造函数"<  
  }
  ~B()
  {
  
   cout<<"B析构"<  }
};
class C:protected B
{
 public:
  void printC()
  {
   x=100;
   printA();
  
  }

};
int main()
{
// A a;
 B b(100,200,300,400);
 b.printB();
// b.setA(100,200);
// b.printA();
// b.setB(100,2000);
// b.printB();
// cout<<"&b="<<&b< cout<<"sizeof(B)="<
// b.setB(500,600);
// b.printB();
// cout<<"&a.z="<<&a.z<
// cout<<"&b.z="<<&b.z<
// b.set(100,200);
// b.print();
// cout<<"sizeof(b)="</* cout<<"&b.x="<<&b.x<
 cout<<"&b.y="<<&b.y< cout<<"&b.m="<<&b.m< cout<<"&b.n="<<&b.n<
 cout<<"&b"<<&b<// cout<<"sizeof(A)="< 
// cout<<"sizeof(B)="< return 0;
}
阅读(647) | 评论(0) | 转发(0) |
0

上一篇:类和对象

下一篇:函数模板

给主人留下些什么吧!~~