C++派生类的派生类(孙子)中的虚函数同样有效(可以在孙子类中修改其功能)

本文详细解析了C++中抽象基类与多态的运用,通过具体示例,展示了如何定义抽象基类Shape,以及如何通过派生类Point和Globe实现多态。深入探讨了虚函数的作用及其实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 #include<iostream>
 using namespace std;
 
 const float PI=3.14;
 //define abstract class
 class Shape
 {
 	public:
 		virtual float area()  {return 0;}//抽象基类中的三个虚函数,其中一个是纯虚函数。 
 		virtual float volume()  {return 0;}
 		virtual void shapeName() =0;
  };
  //定义Point类 
  class Point:public Shape
  {
  	public:
  		Point(float=0,float=0);
  		void setPoint(float a,float b);
  		
		  
  		float getX()const {return x;}
  		float getY()const {return y;}
  		virtual void shapeName()  {cout<<"Point:";}
  		friend ostream& operator<< (ostream& o,const Point& p){
  			o<<"["<<p.x<<" , "<<p.y<<"]";
  			return o;
		  }
	protected:
		float x,y;
  };
  
  Point::Point(float a,float b):x(a),y(b){
  }
  
  void Point::setPoint(float a,float b){
  	x=a,y=b;
  }
  
  //定义Globe类
  class Globe:public Point
  {
  	public:
  		float getR()const {return r;}
  		Globe(float=0,float=0,float=0);
  		//多态的前提是多态中的虚函数和抽象基类中的虚函数的生命完全一样(包括后面的const ) 
  		virtual float area() ; //抽象基类中的三个虚函数,其中一个是纯虚函数。 
 		virtual float volume()  ;
 		virtual void shapeName() {cout<<"Globe:";} 
  		//virtual float area();
  		//virtual float volume();
  		//virtual void shapeName();
  		friend ostream& operator<< (ostream& ,Globe&);
  	protected:
  		float r;
   };
   
   Globe::Globe(float a,float b,float c):Point(a,b),r(c){
   }
   
   
   float Globe::area() 
   {
   	return (4*PI*r*r);
   }
   
   float Globe::volume() 
   {
   	return (1.33*PI*r*r*r);
   }
   
   ostream& operator<< (ostream& o, Globe& g)
   {
  	o<<"["<<g.x<<" , "<<g.y<<"]"<<", "<<"r="<<g.r<<"\n"
  	 <<"area="<<g.area()<<"\n"
  	 <<"volume="<<g.volume();
  	return o;
   }
  int main()
  {
  	Point point(3.2,4.5);
  	Globe globe(3.3,4.6,5);
  	point.shapeName();
  	cout<<point<<endl;
  	cout<<"------------------------------------------"<<endl;
  	cout<<"------------------------------------------"<<endl;
  	globe.shapeName();
  	cout<<globe<<"\n\n\n"<<endl;
  	

  	
  	Shape* p;
  	//将p指向point 
  	p=&point;
  	p->shapeName();  
  	//cout<<"x="<<p->getX()<<","<<"y="<<p->getY()<<endl;  //此处修改为通过point对象访问派生类的新函数 
  	cout<<"x="<<point.getX()<<", "<<"y="<<point.getY()<<endl;  //此处妄想通过p指针访问派生类的新函数 
	cout<<"area="<<point.area()<<"\n"   //此处继承下来的函数也改为point对象修改 
  	    <<"volume="<<p->volume()<<endl;
  	cout<<"---------------------------------------------------------"<<endl;
  	//将p指向globe 
	p=&globe;
  	p->shapeName();  
  	//cout<<"x="<<p->getX()<<","<<"y="<<p->getY()<<endl;  //此处修改为通过point对象访问派生类的新函数 
  	cout<<"x="<<globe.getX()<<", "<<"y="<<globe.getY()<<", "<<"r="<<globe.getR()<<endl;  //此处妄想通过p指针访问派生类的新函数 
	cout<<"area="<<p->area()<<"\n"   //此处继承下来的函数也改为point对象修改 
  	    <<"volume="<<p->volume()<<endl;
  	
  	return 0;
  }

其中,Globe是从Point派生来的,Point从抽象基类Shape派生类的。只要派生类中的虚函数声明和抽象基类中的虚函数的声明完全一样,那么无论派生几代,其虚函数都可以帮助类实现多态化。 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值