码迷,mamicode.com
首页 > 编程语言 > 详细

一个C++类继承的例子-图形

时间:2015-01-24 11:31:59      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

本文是我做家教时的题目,希望没有版权问题。这是一个简单的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++类继承的例子-图形

标签:

原文地址:http://www.cnblogs.com/lishuai0214/p/4245543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!