tuxing,虚函数

cpp 复制代码
#include<iostream>
using namespace std;
class tuxing{
	protected:		
		double r;
	public: 
		tuxing(int r1)
		{
			r=r1;
		}
		tuxing()
			{		
			}
		virtual double biaomianji()
		{
			return 0;
		}
	virtual double tiji()
	{
		return 0;
	}
};
class zhengfangti:public tuxing{
	public:		
		zhengfangti(double r1):tuxing(r1)
			{		
			}
	virtual double biaomianji()
	{
		return r*r*6;
	}
	virtual double tiji()
	{
		return r*r*r;
	}
};
class yuan:public tuxing{
	public:
		yuan(double r1):tuxing(r1)
		{		
		}
	virtual double biaomianji()
		{
			return r*r*3.14/4;
		}
		virtual double tiji()
		{
			return r*r*r*3.14*3/4;
		}
};
class yuanzhu:public tuxing 
{
	protected:
		double h;
	public:		
	yuanzhu(double r1,double h1):tuxing(r1)
	{
		h=h1;	
	}
	virtual double biaomianji()
			{
				return 2*r*3.14*h;
			}
			virtual double tiji()
			{
				return r*r*3.14*h;
			}	
};
void TopPrint(tuxing&r )
{
cout<<r.biaomianji()<<endl;
cout<<r.tiji()<<endl;
}
int main()
{	
	tuxing p1(4),*p;	
	zhengfangti p2(4);
	yuan p3(4);
	yuanzhu p4(4,3);
	p=&p2;
  	   cout<<p->tiji()<<endl;
       cout<<p->biaomianji()<<endl;
	p=&p3;
		cout<<p->tiji()<<endl;
	    cout<<p->biaomianji()<<endl;
	p=&p4;
		cout<<p->tiji()<<endl;
	    cout<<p->biaomianji()<<endl;
TopPrint(p2);
TopPrint(p3);	
TopPrint(p4);	
	return 0;
}
相关推荐
轻浮j1 分钟前
Sentinel底层原理以及使用算法
java·算法·sentinel
潜意识起点4 分钟前
Java数组:静态初始化与动态初始化详解
java·开发语言·python
Abelard_16 分钟前
LeetCode--347.前k个高频元素(使用优先队列解决)
java·算法·leetcode
小猪写代码19 分钟前
C语言:递归函数(新增)
算法·c#
点云SLAM21 分钟前
C++创建文件夹和文件夹下相关操作
开发语言·c++·算法
2301_8091774726 分钟前
2025.01.15python商业数据分析
开发语言·python
CodeClimb39 分钟前
【华为OD-E卷 - 猜字谜100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
_小柏_41 分钟前
C/C++基础知识复习(46)
c语言·开发语言·c++
SomeB1oody1 小时前
【Rust自学】6.4. 简单的控制流-if let
开发语言·前端·rust
明月逐人归4641 小时前
输出语句及变量定义
开发语言·python