Surrogate. cpp

Source: Internet
Author: User

After reading chapter 5th of "C ++ meditation" "ruminations on C ++", I put the code here according to the idea of proxy, so that I can check it later.

 

 

 /** <Br/> * @ file surrogate. CPP <br/> * @ brief <br/> * @ author liugang <br/> * @ version 1.0 <br/> * @ date 2009-11-29 <br/> */<br /># include <iostream> <br/> using namespace STD; <br/> //////////////////////////////////// //// // <br/> // base class <br/> class vehicle {<br/> public: <br/> virtual double weight () const = 0; <br/> virtual void start () = 0; <br/> Virtual Vehicle * Copy () const = 0; <br/> virtual ~ Vehicle () {}< br/> }; <br/> //////////////////////////////////// /// // <br/> // surrogate class <br/> class vehiclesurrogate: public Vehicle {<br/> Public: <br/> vehiclesurrogate (); <br/> vehiclesurrogate (const Vehicle &); <br/> ~ Vehiclesurrogate (); <br/> vehiclesurrogate (const vehiclesurrogate &); <br/> vehiclesurrogate & operator = (const vehiclesurrogate &); <br/> // Implement Vehicle <br/> double weight () const; <br/> void start (); <br/> vehicle * Copy () const {return New vehiclesurrogate (* This) ;}< br/> PRIVATE: <br/> vehicle * VP; <br/>}; <br/> vehiclesurrogate :: vehiclesurrogate (): VP (0) <br/>{< br/>}< br/> vehiclesurrogate: vehiclesu Rrogate (const Vehicle & V): <br/> VP (V. Copy () <br/>{< br/>}< br/> vehiclesurrogate ::~ Vehiclesurrogate () <br/>{< br/> Delete VP; <br/>}< br/> vehiclesurrogate: vehiclesurrogate (<br/> const vehiclesurrogate & V ): <br/> VP (v. VP? V. VP-> copy (): 0) <br/>{< br/>}< br/> vehiclesurrogate & vehiclesurrogate: Operator = (const vehiclesurrogate & V) <br/>{< br/> If (this! = & V) {<br/> Delete VP; <br/> Vp = (V. VP? V. VP-> copy (): 0); <br/>}< br/> return * This; <br/>}< br/> double vehiclesurrogate: weight () const <br/> {<br/> If (Vp = 0) <br/> throw "Empty vehiclesurrogate. weight () "; <br/> return VP-> weight (); <br/>}< br/> void vehiclesurrogate: Start () <br/> {<br/> If (Vp = 0) <br/> throw "Empty vehiclesurrogate. start () "; <br/> VP-> Start (); <br/>}< br/> /////////////////////////////// /// // <br/> // Class automobile <br/> class automobile: public vehicle {<br/> Public: <br/> // automobile (){}; <br/> // automobile (double weight) {fweight = weight;} <br/> // automobile (double weight): fweight (weight) {}< br/> // automobile (double weight = 6.0): fweight (weight) {}< br/> //~ Automobile () {}// here It isn' t necessary, because there is no dynamic memory to free; <br/> // double weight () const {return fweight ;} <br/> double weight () const {return 6.0 ;}< br/> void start () {cout <"start... "<Endl ;}< br/> vehicle * Copy () const {return new automobile (* This) ;}< br/> PRIVATE: <br/> // double fweight; <br/> }; <br/> //////////////////////////////////// /// // <br/> int main (INT argc, char * argv []) <br/>{< br/> int num_vehicles = 0; <br/> vehiclesurrogate parking_lot [1000]; <br/> automobile X; <br/> parking_lot [num_vehicles ++] = x; <br/> // parking_lot [num_vehicles ++] = vehiclesurrogate (x); <br/> return 0; <br/>}< br/>/* VIM: Set Ts = 8 SW = 8 :*/

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.