Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1498716
  • 博文数量: 329
  • 博客积分: 2773
  • 博客等级: 少校
  • 技术积分: 4219
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:17
个人简介

淡定从容,宁静致远

文章分类

全部博文(329)

文章存档

2016年(4)

2015年(50)

2014年(68)

2013年(45)

2012年(162)

分类: C/C++

2012-12-18 22:07:52

#include
using namespace std;
class A
{
 public:
  int x;
  int y;
 public:
  void set(int x1,int y1)
  {
   x=x1;
   y=y1;
  }
  virtual void print()
  {
  
   cout<<"A中的print()"<  } 
};
class B:public A
{
 private:
  int z;
  void set()
  {
  
  }
   void print()
  {
  
   cout<<"B中的print()"<  }
   void printB()
  {
  
   cout<<"B中的printB()"<  
  }
};
class C:public A
{
 private:
  int z;
  void set()
  {
  
  }
   void print()
  {
  
   cout<<"C中的print()"<  }
};
int main()
{
 A *p;
 B b;
 C c;
 p=&b;
 p->print();
 p=&c;
 p->print();
 cout<<"虚函数表地址="<<*((void**)&b)</* A a;
 cout<<"sizeof(a)="< cout<<"sizeof(A)="<
 cout<<"&a="<<&a<
 cout<<"&a.x="<<&a.x< return 0;
}
 
******************************************************************************
#include
using namespace std;
class tx
{
 protected:
  double s;
  double l;
 public:
 virtual void print()=0;
};
class yuan:public tx
{
 private:
  double pi;
  double r;
 public:
  yuan(double r1):pi(3.14)
  {
   r=r1;
  }
  void print()
  {
  
   s=pi*r*r;
   cout<<"r="<   cout<<"s="<
   cout<<"r="<  }
};
int main()
{
 yuan yuan1(10);
 tx *p;
 p=&yuan1;
 p->print();
 return 0;
}
************************************************************************************
#include
using namespace std;
class tx
{
 protected:
  double s;
  double l;
 public:
  virtual void Aa(int x=100)
  {
   cout<<"x="<
   cout<<"Tx"<  }
  virtual void js()
  {
  
  }
  virtual void print()
  {
  
  }
  virtual ~tx()
  {
  
   cout<<"tx析构"<  } 
  
  

};
class yuan:public tx
{
 private:
  const double pi;
  double r;
 public:
  virtual void Aa(int x=200)
  {
   cout<<"x="<  
   cout<<"yuan"<  }
  yuan():pi(3.14)
  {
   r=0;
   
  }
  yuan(double r1):pi(3.14)
  {
   if(r1>=0)
   {
    r=r1;
   }
   else
   {
    r=0;
    cout<<"半径不能为负数,初始化错误"<
   }
  }
  void js()
  {
   s=pi*r*r;
   l=2*pi*r;
  
  }
  void print()
  {
   cout<<"s="<
   cout<<"l="<  
  }
  ~yuan()
  {
  
   cout<<"yuan析构"<  }

};
class cfx:public tx
{
 private:
  double a;
  double b;
 public:
  cfx()
  {
   a=0;
   b=0;
  }
  cfx(double a1,double b1)
  {
   if(a1>=0 && b1>=0)
   {
    a=a1;
    b=b1;
   }
   else
   {
    a=0;
    b=0;
    cout<<"初始化错误"<   
   }
  
  }
  void js()
  {
   s=a*b;
   l=2*(a+b);
  }
  void print()
  {
   cout<<"s="<   cout<<"l="<  }
};
int main()
{
 tx *p=new yuan;
 delete p;
 
/* tx *p;
 int x=0;
 while(1)
 { 
 cout<<"请输入计算图形类型"<
 cout<<"(1)圆"< cout<<"(2)长方形"<
 cout<<"(3)退出"< cin>>x;
 if(x==1)
 {
  double r;
  cout<<"请输入圆半径"<  cin>>r;
  p=new yuan(r);
 
 }
 if(x==2)
 {
  double a,b;
  cout<<"请输入长方形长"<  cin>>a;
  
  cout<<"请输入长方形宽"<  cin>>b;
  p=new cfx(a,b);
 }
 if(x==3)
 {
 
  delete p;
  p=NULL;
  break;
 }
 
 p->js();
 p->print();
 delete p;
 p=NULL;
 }*/
 return 0;
}
阅读(1532) | 评论(0) | 转发(1) |
0

上一篇:函数模板

下一篇:友元

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