首页 > 代码库 > 一个C++类继承的例子-图形
一个C++类继承的例子-图形
本文是我做家教时的题目,希望没有版权问题。这是一个简单的C++类继承的例子,继承关系为图形->三角形->左(右)三角形,图形->矩形->正方形,里面还包括填充图形等类,同时展示了多态性。
注意,本程序需要用支持C++11的编译器编译,如VS2013,DEV C++等
1、主程序
//prog.cpp#include "shapes.hpp"using namespace std;int main() { vector<Rectangle*> rectangles; rectangles.push_back(new Rectangle(3, 5)); // Die beiden Argumente geben Breite und Höhe an rectangles.push_back(new Rectangle(5, 7)); // Die beiden Argumente geben Breite und Höhe an rectangles.push_back(new FilledRectangle(7, 5));// Die beiden Argumente geben Breite und Höhe an rectangles.push_back(new Square(6)); // Das Argument gibt die Seitenlänge an rectangles.push_back(new FilledSquare(7)); // Das Argument gibt die Seitenlänge an // for (Rectangle* r : rectangles) { // r->print(); // } vector<Triangle*> triangles; // Bei den Dreiecken soll es sich um gleichschenklige, rechtwinklige // Dreiecke handeln. Der rechte Winkel wird von von den gleichlangen Seiten // eingeschlossen. triangles.push_back(new LeftTriangle(10)); // Das Argument gibt die Länge der triangles.push_back(new RightTriangle(7)); // beiden gleichlangen Seiten an. // for (Triangle* t : triangles) { // t->print(); // } vector<Shape*> shapes; // für jedes r in rectangles for (Rectangle* r : rectangles) { shapes.push_back(r); } // für jedes t in triangles for (Triangle* t : triangles) { shapes.push_back(t); } // für jedes s in shapes for (Shape *s: shapes) { processShape(*s); } //////////////////////////// for (Shape *s: shapes) { delete s; }}
2、头文件,hpp文件,现在已经升级为hpp了,呵呵
//shapes.hpp#ifndef SHAPES_HPP#define SHAPES_HPP#include <iostream>#include <vector>class Shape{public: //virtual void print() const = 0; virtual void print() const; virtual double area() const = 0; virtual double perimeter() const = 0;};class Rectangle:public Shape{protected: int width; int height; public: Rectangle(int width, int height); virtual void print() const; virtual double area() const; virtual double perimeter() const;};class FilledRectangle:public Rectangle{public: FilledRectangle(int width, int height); virtual void print() const;};class Square:public Rectangle{public: Square(int length);};class FilledSquare:public FilledRectangle{public: FilledSquare(int length);};class Triangle:public Shape{protected: int length; //////////////// public: Triangle(int length); virtual double area() const; virtual double perimeter() const;};class LeftTriangle:public Triangle{public: LeftTriangle(int lenght); virtual void print() const;};class RightTriangle:public Triangle{public: RightTriangle(int length); virtual void print() const;};void processShape(const Shape& s);#endif
3、cpp文件
////shapes.cpp#include "shapes.hpp"using namespace std;void Shape::print() const{ //Error;}Rectangle::Rectangle(int width, int height){ this->width = width; this->height = height;}void Rectangle::print()const{ for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i > 0 && i < height - 1 && j > 0 && j < width - 1) printf(" "); else printf("*"); } printf("\n"); }}double Rectangle::area() const{ return width * height;}double Rectangle::perimeter() const{ return 2 * (width + height);}FilledRectangle::FilledRectangle(int width, int height):Rectangle(width, height){}void FilledRectangle::print() const{ for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { printf("*"); } printf("\n"); }}Square::Square(int length):Rectangle(length, length){}FilledSquare::FilledSquare(int length):FilledRectangle(length, length){}Triangle::Triangle(int length){ this->length = length;}double Triangle::area() const{ return length * length / 2;}double Triangle::perimeter() const{ return (2 + 1.414) * length; ///////////////}LeftTriangle::LeftTriangle(int length):Triangle(length){}void LeftTriangle::print() const{ for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (j == 0 || i == j || i == length - 1) printf("*"); else printf(" "); } printf("\n"); }}RightTriangle::RightTriangle(int length):Triangle(length){}void RightTriangle::print()const{ for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (j == length - 1 || length - i - 1 == j || i == length - 1) printf("*"); else printf(" "); } printf("\n"); }}void processShape(const Shape& s) { s.print(); cout << "Flaeche: " << s.area() << endl; cout << "Umfang: " << s.perimeter() << endl; cout << endl << endl;}
结果如下:
一个C++类继承的例子-图形
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。