From 09faded414c44d9ed4d91f5c7d1e599c72e278df Mon Sep 17 00:00:00 2001 From: p2jvg4k7s <274055206@qq.com> Date: Wed, 26 Jun 2024 10:27:46 +0800 Subject: [PATCH] Add 3.cpp --- 3.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 3.cpp diff --git a/3.cpp b/3.cpp new file mode 100644 index 0000000..8ebccc5 --- /dev/null +++ b/3.cpp @@ -0,0 +1,41 @@ +//声明抽象基类Shape,包含2个虚函数shapeName( )和perimeter( ),分别返回形状名和周长。由它派生出1个子类:Trapezoid(等腰梯形) +//用一个非类内函数printPerimeter(Shape& s),以基类引用作为函数形式参数,输出周长,图形的数据在创建对象时给定。 +#include +#include +using namespace std; +class Shape +{public: + virtual string shapeName( ){} + virtual double perimeter( ){} +}; + + +/**************补充Trapezoid类定义**************/ +class Trapezoid:public Shape +{ +public: + Trapezoid(float i,float j,float k):a(i),b(j),c(k){} + string shapeName() + { + return "Trapezoid"; + } + double perimeter() + { + return a+b+2*c; + } +private: + float a,b,c; +}; + +/**************补充Trapezoid类定义**************/ + +void printPerimeter(Shape& s) //以基类引用作为函数形参 +{cout<<"perimeter of "<