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

📄 b_9_1.cpp

📁 C++应用教程原码,里面包含该书中有十三章内容的代码,详细具体
💻 CPP
字号:
#include "stdafx.h"
#include <iostream>
#include <string>
#include<iomanip>
using namespace std;
const double PI=3.1416;
class shape{
public:
	virtual double area()=0;
};
class Circle:public shape {
public:
	Circle(double x)
		:r(x){ }
	double area()
	{ return PI*r*r;}
private:
	double r;
};
class Triangle:public shape
{
public:
	Triangle(double i,double j)
		:high(i),width(j){ }
	double area()
	{ return 0.5*high*width;	}
private:
	double high,width;
};
class Rectangle:public shape {
public:
	Rectangle(double i,double j)
		:high(i),width(j){ }
	double area()
	{ return high*width;}
private:
	double high,width;
};
void main()
{
	shape *ptr;
	Triangle triangle(8,11);
	Rectangle rectangle(8,11);
	Circle circle(7);
	ptr=&circle;
	cout<<"圆的面积是:"<<ptr->area()<<endl;
	ptr=&rectangle;
	cout<<"矩形的面积是:"<<ptr->area()<<endl;
	ptr=&triangle;
	cout<<"三角形的面积是:"<<ptr->area();
    cin.get(); //等待结束,以便调测程序,可以删除
}

⌨️ 快捷键说明

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