sy5_继承的应用_2_虚函数_Shape
(2)声明抽象基类Shape,由它派生出3个派生类,Circle(圆形),Square(正方形)和Triangle(三角形),用虚函数分别输出图形的名称并计算几种图形的面积和周长。要求在主函数中使用基类指针数组,使它的每一个元素指向一个派生类的对象,然后调用其成员函数测试计算面积和周长的功能。
整段代码:
Shape.h
#pragma once
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void ShapeName() = 0;
virtual float area() = 0;
virtual float Circumference() = 0;
};
#endif // !SHAPE_H
Circle.h
#pragma once
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include"Shape.h"
using namespace std;
class Circle:public Shape
{
protected:
float Radius;
public:
Circle();
void ShapeName();
float area();
float Circumference();
friend istream& operator>>(istream& input, Circle& C);
friend ostream& operator<<(ostream& output, const Circle& C);
};
#endif // !CIRCLE_H
Square.h
#pragma once
#ifndef SQUARE_H
#define SQUARE_H
#include <iostream>
#include"Shape.h"
using namespace std;
class Square :public Shape
{
protected:
float Width,Height;
public:
Square();
void ShapeName();
float area();
float Circumference();
friend istream& operator>>(istream& input, Square& Si);
friend ostream& operator<<(ostream& output, const Square& Si);
};
#endif // !SQUARE_H
Triangle.h
#pragma once
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include <iostream>
#include"Shape.h"
#include <math.h>
using namespace std;
class Tirangle :public Shape
{
protected:
float a, b,c;
public:
Tirangle();
void ShapeName();
float area();
float Circumference();
friend istream& operator>>(istream& input, Tirangle& T);
friend ostream& operator<<(ostream& output, const Tirangle& T);
};
#endif // !TRIANGLE_H
Circle.cpp
#include"Shape.h"
#include"Circle.h"
Circle::Circle()
{
this->Radius = 0;
}
void Circle::ShapeName()
{
cout << "这是Circle(圆形)" << endl;
}
float Circle::area()
{
return 3.14 * Radius * Radius;
}
float Circle::Circumference()
{
return 2 * 3.14 * Radius;
}
istream& operator>>(istream& input, Circle& C)
{
cout << "请输入圆的半径R:";
input >> C.Radius;
return input;
}
ostream& operator<<(ostream& output, const Circle& C)
{
output << "圆的半径:" << C.Radius << endl;
return output;
}
Square.cpp
#include"Shape.h"
#include"Square.h"
Square::Square()
{
this->Width = 0;
this->Height = 0;
}
void Square::ShapeName()
{
cout << "这是Square(正方形)" << endl;
}
float Square::area()
{
return Width * Height;
}
float Square::Circumference()
{
return 2 * (Width + Height);
}
istream& operator>>(istream& input, Square& Si)
{
cout << "长方形的长和宽:";
input >> Si.Height>> Si.Width;
return input;
}
ostream& operator<<(ostream& output, const Square& Si)
{
output << "长=" << Si.Height << "," << "宽=" << Si.Width << endl;
return output;
}
Triangle.cpp
#include"Shape.h"
#include"Triangle.h"
Tirangle::Tirangle()
{
this->a = 0;
this->b = 0;
this->c = 0;
}
void Tirangle::ShapeName()
{
cout << "这是Tirangle(三角形)" << endl;
}
float Tirangle::area()
{
float P = 0;
float S = 0;
P = (a + b + c) / 2;
S = sqrt(P*(P - a)*(P - b)*(P- c));
return S;
}
float Tirangle::Circumference()
{
return a+b+c;
}
istream& operator>>(istream& input, Tirangle& T)
{
cout << "请输入三角形的三条边:";
input >> T.a >> T.b >> T.c;
while (!((T.a + T.b) > T.c && (T.b + T.c) > T.a && (T.a + T.c) > T.b))
{
cout << "请输入正确的三角形三边关系:";
cin.clear();
input >> T.a >> T.b >> T.c;
}
return input;
}
ostream& operator<<(ostream& output, const Tirangle& T)
{
output << "三角形三边a=" << T.a << "," << "b=" << T.b <<"," << "c=" << T.c << endl;
return output;
}
main.cpp
#include <iostream>
#include "Shape.h"
#include "Circle.h"
#include "Square.h"
#include "Triangle.h"
using namespace std;
int main()
{
Shape* shape[3];
Circle circle;
cin >> circle;
cout << circle;
shape[0] = &circle;
shape[0]->ShapeName();
cout << "圆的面积:" << shape[0]->area() << endl;
cout << "圆的周长:" << shape[0]->Circumference() << endl;
cout << "-----------------------------------" << endl;
Square square;
cin >> square;
cout << square;
shape[1] = □
shape[1]->ShapeName();
cout << "正方形的面积:" << shape[1]->area() << endl;
cout << "正方形的周长:" << shape[1]->Circumference() << endl;
cout << "-----------------------------------" << endl;
Tirangle triangle;
cin >> triangle;
cout << triangle;
shape[2] = ▵
shape[2]->ShapeName();
cout << "三角形形的面积:" << shape[2]->area() << endl;
cout << "三角形的周长:" << shape[2]->Circumference() << endl;
}