Proxy of C ++ meditation records

Source: Internet
Author: User

When reading the C ++ meditation record, there is a requirement to store different objects in the same array, and some behavior can be performed. My personal feeling is a bit like the observer pattern.

For example, different car objects in the parking lot must be stored in the car factory array. And achieve the basic common behavior of some cars.

By implementing a deep copy of the original object, the pointer of the car base object in the agent class points to the deep copy object and achieves the basic behavior of the car through polymorphism. The Code is as follows:

The basic class pure virtual function of a car, used for inheritance, cannot instantiate a specific object, when the pointer object can be declared)

/*************************************** ************************** * **********************/Class Vechle {public: virtual void Weight () = 0; virtual void Start () = 0; virtual Vechle * Copy () const = 0 ;};

Implementation of various specific vehicles

/************************************ Specific transportation tool implementation, car ************************************/class smallCar: public Vechle {public: SmallCar (){};~ SmallCar () {}; void Weight (); void Start (); Vechle * Copy () const ;}; /************************* implement the virtual functions of the base class, realize polymorphism ************************/void SmallCar: Weight () {cout <"Car Weight 400" <endl;} void SmallCar: Start () {cout <"car started" <endl ;} // The Copy function is used to Copy a Copy of the object itself. Vechle * SmallCar: Copy () const {// return a Copy of the object itself return new SmallCar (* this );} /************************************ specific transportation tool implementation, big car ***********************************/class bigBus: public Vechle {public: BigBus (){};~ BigBus () {}; void Weight (); void Start (); Vechle * Copy () const ;}; /************************* implement the virtual functions of the base class, implementation of polymorphism ************************/void BigBus: Weight () {cout <"" <endl;} void BigBus: Start () {cout <"" <endl ;} // The Copy function is used to Copy a Copy of the object itself. Vechle * BigBus: Copy () const {// return a Copy of the object itself return new BigBus (* this );}

Automobile agent:

/******************************** Parking lot implementation, you can park a variety of vehicles. You can define a parking lot array to save various types of cars *************************** * ***/class VechleSurrogate {public: vechleSurrogate ();~ Sums (); VechleSurrogate (const Vechle & tVechle); VechleSurrogate (const sums & counts); VechleSurrogate & operator = (const VechleSurrogate & counts); void Weight (); void Start (); private: Vechle * m_Vechle;}; VechleSurrogate: VechleSurrogate () {m_Vechle = 0;} VechleSurrogate ::~ VechleSurrogate () {if (NULL! = M_Vechle) {delete m_Vechle ;}// copy and construct VechleSurrogate: VechleSurrogate (const Vechle & tVechle) {m_Vechle = tVechle. copy () ;}// Copy the constructor/* VechleSurrogate: VechleSurrogate (const VechleSurrogate & tVechleSurrogate) {m_Vechle = tVechleSurrogate. m_Vechle? Topology. m_Vechle-> Copy (): 0 ;}* // Copy constructor VechleSurrogate & VechleSurrogate: operator = (const VechleSurrogate & tVechleSurrogate) {if! = This) {if (NULL! = This-> m_Vechle) {delete this-> m_Vechle;} m_Vechle = tVechleSurrogate. m_Vechle? TVechleSurrogate. m_Vechle-> Copy (): 0;} return * this;} // call void VechleSurrogate: Weight () {m_Vechle-> Weight ();} void VechleSurrogate: Start () {m_Vechle-> Start ();}

Implementation of Automobile Plants

Int main (int argc, char * argv []) {char c; VechleSurrogate ArrayVechleSurrogate [3]; SmallCar * tSmallCar = new SmallCar (); BigBus * tBigBus = new BigBus (); arrayVechleSurrogate [0] = * tSmallCar; ArrayVechleSurrogate [1] = * tBigBus; ArrayVechleSurrogate [0]. start (); ArrayVechleSurrogate [1]. start (); ArrayVechleSurrogate [0]. weight (); ArrayVechleSurrogate [1]. weight (); cout <endl <"press any key to exit ....... "<endl; c = getchar (); return 0 ;}

Test Results

650) this. width = 650; "title =" qq 30928210355.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/19362Bb5-0.jpg "alt =" 210428518.jpg"/>

This article is from the "Feng qing yang song" blog, please be sure to keep this source http://2309998.blog.51cto.com/2299998/1303187

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.