⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex12_2.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#pragma hdrstop

// base class describing a geometric rectangle
class Rectangle
{
	protected:
		// length and width of the rectangle
		float l, w;
	public:
		// constructor. initialize l and w
		Rectangle(float length, float width): l(length),w(width)
		{}
		
		// return area of rectangle
		float Area(void)
		{
			return l*w;
		}
		
		// volume of rectangle is 0
		float Volume(void)
		{
			return 0.0;
		}
};


// Box is a rectangular base and a specified height
class Box: public Rectangle
{
	protected:
		// height of the box
		float h;
	public:
		// constructor. initialize base class and h
		Box(float length, float width, float height):
				Rectangle(length,width),h(height)
		{}
		
		// area is sum of area of the six sides
		float Area(void)
		{
			return 2.0*(l*w+l*h+w*h);
		}
		
		// volume is area of base * height
		float Volume(void)
		{
			return Rectangle::Area() * h;
		}
};

void main(void)
{
	char figType;
	float l, w, h;
	
	// prompt for the type of figure to be constructed
	cout << "Enter the type of figure ('R' for Rectangle, 'B' for Box): ";
	cin >> figType;
	// test the type of figure
	if (figType == 'R')
	{
		// rectangle. read length and width, build the rectangle and print
		// its area and volume
		cout << "Enter the length and width of the rectangle: ";
		cin >> l >> w;
		
		Rectangle R(l,w);
		
		cout << "The area and volume of the rectangle are "
			 << R.Area() << "  " << R.Volume() << endl;
	}
	else
	{
		// box. read length, width and height, build the box and print
		// its area and volume
		cout << "Enter the length and width and height of the box: ";
		cin >> l >> w >> h;
		
		Box B(l,w,h);
		
		cout << "The area and volume of the box are "
			 << B.Area() << "  " << B.Volume() << endl;
	}
}

/*
<Run>

Enter the type of figure ('R' for Rectangle, 'B' for Box): B
Enter the length and width and height of the box: 2 2 3
The area and volume of the box are 32  12
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -