Example of a C + + class inheritance-graphics

Source: Internet
Author: User

This article is my tutor when the topic, I hope there is no copyright issues. This is a simple example of C + + class inheritance, the inheritance relationship is a graph----triangle---------------------rectangle----square, which also includes such classes as fill graphics, and shows polymorphism.

Note that this program needs to be compiled with a compiler that supports c++11 , such as Vs2013,dev C + +, etc.

1. Main program

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 definition an rectangles.push_back (new Rectangle (5, 7)); Die beiden Argumente geben Breite und definition an rectangles.push_back (new Filledrectangle (7, 5));//Die beiden Argument            E geben Breite und definition 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 A//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;    Dealt Jedes R in rectangles for (rectangle* r:rectangles) {shapes.push_back (R);    }//Dealt jedes T in triangles for (triangle* t:triangles) {shapes.push_back (t);    }//dealt jedes s in shapes for (Shape *s:shapes) {processshape (*s);    }////////////////////////////for (Shape *s:shapes) {delete S; }}

 2, header file, hpp file, has now been upgraded to HPP, hehe

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 files

Shapes.cpp#include "SHAPES.HPP" using namespace std;void Shape::p rint () const{//error;}    Rectangle::rectangle (int width, int height) {this->width = width; This->height = height;}        void Rectangle::p rint () 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) PR            Intf ("");        else printf ("*");    } printf ("\ n"); }}double Rectangle::area () const{return width * height;} Double Rectangle::p erimeter () const{return 2 * (width + height);} Filledrectangle::filledrectangle (int width, int height): Rectangle (width, height) {}void filledrectangle::p rint () 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::p erimeter () const{return (2 + 1.414) * length; }lefttriangle::lefttriangle (int length): Triangle (length) {}void lefttriangle::p rint () 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::p rint () const{for (int i = 0; i < l Ength; 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;}

The results are as follows:

Example of a C + + class inheritance-graphics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.