C + + Inheritance

Source: Internet
Author: User

Key points: inheritance, multiple inheritance, What's inherited

//What is inheritance#include <iostream>using namespacestd;classPolygon {protected:    intwidth, height;  Public:    voidSet_values (intAintb) {width=a; height=b;} };classRectangle: PublicPolygon {//rectangle inherits from Polygon Note:::. Public details publicly represents the highest permission a subclass can get from a base class, and if there is a contradiction to private, other classes that should be non-fee-inheriting can also get exposed members from the base class Public:    intArea () {returnWidth *height;} };classTriangle: PublicPolygon {//triangles inherit from polygon triangles belong to polygon derived classes inherit public from the base class as public Public:    intArea () {returnWidth * Height/2; }  }; intMain () {Rectangle rect;  Triangle Trgl; Rect.set_values (4,5); Trgl.set_values (4,5); cout<< Rect.area () <<'\ n'; cout<< Trgl.area () <<'\ n'; return 0;}
A rectangle is a polygon with four sides of the rectangle
A triangle is a polygon, a triangle has three edge attributes, many of which are subclasses, with fewer attributes for the base class

Multiple inheritance, inherited base class without cross-commonality
Dog with skin four legs with eyes and mouth
A fish with a skin, a mouth, and a fish fin. The top three can be combined as a base class, or it can be separated as a feature of the last of the three base classes as subclasses

The constructor of the subclass is automatically dispatched as the constructor destructor of the parent class

C + + Inheritance

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.