C + + Plant inheriting polymorphic diamond inheritance

Source: Internet
Author: User

#pragma  once//header file #include <iostream> #include <string>using namespace std;////  1. Implement the member functions of the following classes// 2. Implements the override of a virtual function and calls// 3. Handles diamond inheritance issues.   Plant Class botany{public:botany (const string&  name); Virtual ~botany (); Virtual void display (); Botany (const botany& b);p rotected:string _name;//name static size_t _scount;}; class tree : public botany{public://... Implement the default member function tree (const string&  name, const int hight);virtual void  Display (); Virtual ~tree (); Tree (const tree& t);p rotected:int _hight;//  height};class flower : public  botany{public://... Implement the default member function flower (const string&, const string&  color);virtual void  Display (); Virtual ~flower (); Flower (const flower& f);p rotected:string _colour;//  color};//  white orchid, that is, the tree is sometimes spent. Class miCheliaalba : public flower, public tree{public:micheliaalba (const string&  name, const string& color, const int hight);virtual void  Display (); Virtual ~micheliaalba (); Micheliaalba (const micheliaalba& m);p rotected:// ...}; #include  <iostream>//function file #include<string> #include "plant.h" using namespace std;size_ T botany::_scount = 0;void botany::D isplay () {cout<< "botany: " << _ Name << endl;cout << _scount << endl;} Botany::botany (const string&  name): _name (name) {++_scount;cout <<  "Botany"  << endl;} Tree::tree (const string&  name, const int hight):  Botany (name),  _ Hight (hight) {cout <<  "Tree" &NBSP;&LT;&LT;&NBSP;ENDL;} Flower::flower (Const string&  name,&nbsP;const string&  color): Botany (name), _colour (color) {cout <<  "Flower"   << endl;} Micheliaalba::micheliaalba (const string&  name, const string&   Color, const int hight): Flower (Name,color),  tree (name,hight) {cout <<  " Micheliaalba "&NBSP;&LT;&LT;&NBSP;ENDL;} Void tree::D isplay () {cout <<  "Tree:"  << _name << endl; Cout << _scount << endl;} Void flower::D isplay () {cout <<  "flower "  << _name <<  endl;cout << _scount << endl;} Void micheliaalba::D isplay () {cout <<  "micheliaalba "  << flower::  _name << "  " <<Flower::_colour<< "  " <<Tree::_hight< < endl;cout << _scount << endl;}  botany::~botany () { cout <<  "~botany" &NBSP;&LT;&LT;&NBSP;ENDL;}  tree:: ~tree ()  { cout <<  "~tree"  << endl; }  Flower:: ~flower ()  { cout <<  "~flower"  << endl; }  Micheliaalba::~micheliaalba ()  { cout <<  "~micheliaalba"  << endl;  } botany::botany (const botany& b)  :_name (b._name)  { } Tree::Tree ( const tree& t)  : botany (t._name)  , _hight (t._hight)  { }  Flower::flower (const flower& f)  : botany (f._name)  , _colour (f._colour)   { } micheliaalba::micheliaalba (const micheliaalba& m)  : tree (m.Tree::_name,m . _hight)  , flower (m.flower::_name,m._colour)  { }   #include  <iostream> Main function    test file #include<string>#include "plant.h" Using namespace std;void test () {micheliaalba m ("White orchid",  "Red", "M"); Display (); MICHELIAALBA&NBSP;M2 (m); m2. Display (); M.display ();} Int main () {test (); return 0;}


C + + Plant inheriting polymorphic diamond 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.