面向对象程序设计实践(C++)——基本几何形状

目录

    • 概述
    • 知识要点
    • 实现
        • `Shape.h`
        • `Line.h`
        • `Circle.h`
        • `Rectangle.h`
        • `Square.h`
        • `Shape.cpp`
        • `Line.cpp`
        • `Circle.cpp`
        • `Rectangle.cpp`
        • `Square.cpp`
    • 测试
        • `Main.cpp`
        • 运行情况

概述

编写 C++ 程序完成 “形状”的以下功能

  • 声明一个基类Shape(形状),其中包含一个方法来计算面积
  • Shape派生三个类:矩形类Rectangle、圆形类Circle和线段类Line
  • 从矩形类派生正方形类Square
  • 分别实现派生类构造函数、析构函数和其他方法,其中
    • 矩形对象可由两个线段对象初始化,分别作为其长和宽
    • 正方形对象可由一个线段对象初始化,作为其边长
    • 对不同对象计算面积(线段除外)
    • 实现对输出流的重载,输出相应对象的几何信息

知识要点

  • 继承、多态
  • 重载
  • 友元

实现

  • Shape.h

#pragma once
#include 
#include 

class Shape {
private:
	static int shapeNum;
public:
	Shape();
	static int getShapeNum();
	virtual double getArea();
	virtual std::string toString();
	friend std::ostream& operator<<(std::ostream& os_, Shape& sh_);
};
  • Line.h

#pragma once
#include "Shape.h"

class Line: public Shape {
protected:
	double length;
public:
	Line();
	Line(double len_);
	Line(const Line& line_);
	void setLength(double len_);
	double getLength();
	std::string toString();
	friend std::ostream& operator<<(std::ostream& os_, Line& line_);
};
  • Circle.h

#pragma once
#include "Shape.h"

class Circle: public Shape {
protected:
	double radius;
public:
	Circle();
	Circle(double rad_);
	Circle(const Circle& cir_);
	void setRadius(double rad_);
	double getRadius();
	double getArea();
	std::string toString();
	friend std::ostream& operator<<(std::ostream&, Circle& cir_);
};
  • Rectangle.h

#pragma once
#include "Shape.h"
#include "Line.h"

class Rectangle: public Shape {
protected:
	double length;
	double width;
public:
	Rectangle();
	Rectangle(double len_, double wid_);
	Rectangle(Line& l1_, Line& l2_);
	Rectangle(const Rectangle& rect_);
	void setLength(double len_);
	void setWidth(double wid_);
	double getLength();
	double getWidth();
	double getArea();
	std::string toString();
	friend std::ostream& operator<<(std::ostream& os_, Rectangle& rect_);
};
  • Square.h

#include "Rectangle.h"
#include "Line.h"

class Square: public Rectangle {
public:
	Square();
	Square(double side_);
	Square(Line& l_);
	Square(const Square& squ_);
	void setSideLength(double side_);
	double getSideLength();
	double getArea();
	std::string toString();
	friend std::ostream& operator<<(std::ostream& os_, Square& squ_);
};
  • Shape.cpp

#include "Shape.h"

int Shape::shapeNum{0};
Shape::Shape() {
	shapeNum++;
}

int Shape::getShapeNum() {
	return shapeNum;
}

double Shape::getArea() {
	return 0.0;
}

std::string Shape::toString() {
	return "";
}

std::ostream& operator<<(std::ostream& os_, Shape& sh_) {
	os_ << sh_.toString();
	return os_;
}
  • Line.cpp

#include "Line.h"

Line::Line()
	: Shape(), length(1) {
}

Line::Line(double len_)
	: Shape(), length(len_) {
}

Line::Line(const Line& line_)
	: Shape(), length(line_.length) {
}

void Line::setLength(double len_) {
	length = len_;
}

double Line::getLength() {
	return length;
}

std::string Line::toString() {
	return " Length = " + std::to_string(length);
}

std::ostream& operator<<(std::ostream& os_, Line& line_) {
	os_ << line_.toString();
	return os_;
}
  • Circle.cpp

#include "Circle.h"

Circle::Circle()
	: Shape(), radius(1) {
}

Circle::Circle(double rad_)
	: Shape(), radius(rad_) {
}

Circle::Circle(const Circle& cir_)
	: Shape(), radius(cir_.radius) {
}

void Circle::setRadius(double rad_) {
	radius = rad_;
}

double Circle::getRadius() {
	return radius;
}

double Circle::getArea() {
	return 3.1415926 * radius * radius;
}

std::string Circle::toString() {
	return " Radius = " + std::to_string(radius) + ", Area = " + std::to_string(getArea());
}

std::ostream& operator<<(std::ostream& os_, Circle& cir_) {
	os_ << cir_.toString();
	return os_;
}
  • Rectangle.cpp

#include "Rectangle.h"

Rectangle::Rectangle()
	: Shape(), length(1), width(1) {
}

Rectangle::Rectangle(double len_, double wid_)
	: Shape(), length(len_), width(wid_) {
}

Rectangle::Rectangle(Line& l1_, Line& l2_)
	: Shape(), length(l1_.getLength()), width(l2_.getLength()) {
}

Rectangle::Rectangle(const Rectangle& rect_)
	: Shape(), length(rect_.length), width(rect_.width) {
}

void Rectangle::setLength(double len_) {
	length = len_;
}

void Rectangle::setWidth(double wid_) {
	width = wid_;
}

double Rectangle::getLength() {
	return length;
}

double Rectangle::getWidth() {
	return width;
}

double Rectangle::getArea() {
	return length * width;
}

std::string Rectangle::toString() {
	return " Length = " + std::to_string(length) + ", Width = " + std::to_string(width) + ", Area = " + std::to_string(getArea());
}

std::ostream& operator<<(std::ostream& os_, Rectangle& rect_) {
	os_ << rect_.toString();
	return os_;
}
  • Square.cpp

#include "Square.h"

Square::Square()
	: Rectangle(1, 1) {
}

Square::Square(double side_)
	: Rectangle(side_, side_) {
}

Square::Square(Line& l_)
	: Rectangle(l_.getLength(), l_.getLength()) {
}

Square::Square(const Square& squ_)
	: Rectangle(squ_.length, squ_.width) {
}

void Square::setSideLength(double side_) {
	length = width = side_;
}

double Square::getSideLength() {
	return length;
}

double Square::getArea() {
	return Rectangle::getArea();
}

std::string Square::toString() {
	return " SideLength = " + std::to_string(length) + ", Area = " + std::to_string(getArea());
}

std::ostream& operator<<(std::ostream& os_, Square& squ_) {
	os_ << squ_.toString();
	return os_;
}

测试

  • Main.cpp

#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include "Line.h"
using namespace std;

int main() {
	Line l1{3}, l2{5};
	Rectangle r1, r2{1, 2}, r3{l1, l2}, r4{r3};
	Circle c1, c2{4};
	Square s1, s2{2}, s3{l1}, s4{s3};;
	Shape sh;
	cout << l1 << endl << l2 << endl;
	cout << r1 << endl << r2 << endl << r3 << endl << r4 << endl;
	cout << c1 << endl << c2 << endl;
	cout << s1 << endl << s2 << endl << s3 << endl << s4 << endl;
	cout << sh << endl;
	cout << "ShapeNum = " << Shape::getShapeNum() << endl;
	return 0;
}
  • 运行情况

<Type: Line> Length = 3.000000
<Type: Line> Length = 5.000000
<Type: Rectangle> Length = 1.000000, Width = 1.000000, Area = 1.000000
<Type: Rectangle> Length = 1.000000, Width = 2.000000, Area = 2.000000
<Type: Rectangle> Length = 3.000000, Width = 5.000000, Area = 15.000000
<Type: Rectangle> Length = 3.000000, Width = 5.000000, Area = 15.000000
<Type: Circle> Radius = 1.000000, Area = 3.141593
<Type: Circle> Radius = 4.000000, Area = 50.265482
<Type: Square> SideLength = 1.000000, Area = 1.000000
<Type: Square> SideLength = 2.000000, Area = 4.000000
<Type: Square> SideLength = 3.000000, Area = 9.000000
<Type: Square> SideLength = 3.000000, Area = 9.000000
<Type: Shape>
ShapeNum = 13

你可能感兴趣的:(C++,面向对象)