"21 days C + +" Note June 27

Source: Internet
Author: User

Inherited

    • Public succession, is-a relations, for example, He is a man;

Derived classes can access non-private members of the base class, as well as instances of derived classes;

    • Private inheritance and protection inheritance, has-a relationship, Car have a motor;

Private inheritance also allows derived classes to access the base class non-private members, but the derived class instance cannot access any members of the base class, and the subclass of the derived class cannot access any members of the base class;

To protect inheritance, unlike private inheritance, subclasses of subclasses are non-private members that can access the base class;

Note: Use private or protected inheritance only when necessary, and try to replace it with a combination or aggregation (the ' base class object as a member property of ' subclass ') when encountering has-a situations

Class Motor{public:void switchignition (); void Pumpfuel (); void Firecylinders ();}; Class Car{private:motor Heartofcar;public:void Move () {heartofcar.firecylinders (); Heartofcar.pumpfuel (); Heartofcar.switchignition ();}};
    • Cut: Copies the subclass object display to the parent class object, or passes the parameter to the parent class object, leaving only the base part, which is called the cut
class Platypus: Public mammal, public Reptile, public bird{};//multiple inheritance
    • Overloading, overwriting, hiding: overloading is a function of a class with a different parameter in the same name, overwriting a function with the same name as the parent class, and hiding that the subclass hides a function with the same name but different parameters in the parent class, that is, the instance cannot be accessed.

Polymorphic

    • Using virtual functions to achieve polymorphic behavior, by invoking a virtual function by reference or pointer to achieve polymorphic

Treats a derived class object as a base class object and executes the override function of a derived class

classfish{ Public:    Virtual voidSwim () {cout<<"Fish Swim ."<<Endl; }};classTuna: Publicfish{ Public:    Virtual voidSwim () {cout<<"Tuna is fast"<<Endl; }};classCarp: Publicfish{ Public:    Virtual voidSwim () {cout<<"Carp is slow"<<Endl; }};intMain () {Tuna T;    Carp C; Fish&AMP;F1 =T; F1.    Swim (); return 0;}
    • For instantiating a derived class object in a free store using new, if it is paid to the base class pointer,
    • and call Delete from this pointer, the destructor of the derived class will not be called.
    • This can cause problems such as resources not being released, memory leaks, and so on.
    • To avoid this problem, declare the destructor as a virtual function.
classfish{ Public: Fish () {cout<<"constructed Fish"<<Endl; }    ~Fish () {cout<<"destroyed Fish"<<Endl; }};classTuna: Publicfish{ Public: Tuna () {cout<<"constructed Tuna"<<Endl; }    ~Tuna () {cout<<"destroyed Tuna"<<Endl; }};voidDeletefishmemory (Fish *pfish) {    DeletePfish;}intMain () {Tuna*ptuna =NewTuna;    Deletefishmemory (Ptuna); return 0;}/*output: **constructed fish**constructed tuna**destroyed Fish*/

Abstract base class and pure virtual function

  A base class that cannot be instantiated is called an abstract base class, and can be created by declaring a pure virtual function.

Class fish{public:virtual void Swim () = 0;}; Class Tuna:public Fish{public:void Swim () {cout << "Tuna swims fast in the sea" << Endl;}};

Solving diamond problems with virtual inheritance (ambiguity)

    

classanimal{ Public: Animal () {cout<<"ANimal Constructor"<<Endl; }    intAge ;};classMammal: Public Virtualanimal{};classBird: Public Virtualanimal{};classReptile: Public Virtualanimal{};classPlatypus: PublicMammal, PublicBird, Publicreptile{ Public: Platypus () {cout<<"Platypus Constructor"<<Endl; }};intMain () {platypus DUCKBILLEDP; Duckbilledp.age= -; return 0;}

    • When inheriting multiple base classes derived from the same class, if virtual inheritance is not used, the parent class of multiple base classes is inherited, and virtual inheritance is used, only one is inherited.
    • The virtual keyword has two different concepts, one for polymorphism and the other for virtual inheritance resolution ambiguity

Technically speaking, C + + does not support virtual copy constructors

operator type and operator overloading

    • Monocular operator

1, (+ +) increment operator

Date operator + + () {} single-current-prefix operator overloaded in class

The Date operator + + (int) {} monocular suffix operator is overloaded in a class

2, the conversion operator, the following example converts a date to a const char* with a conversion operator

classdate{Private:    intDay ; intMonth; intYear ; stringdateinstring; Public: Date (intInputday,intInputmonth,intinputyear): Day (Inputday), Month (Inputmonth), year (inputyear) {}operator Const Char*() {Ostringstream formatteddate; Formatteddate<< Day <<" / "<< Month <<" / "<<Year ; Dateinstring=Formatteddate.str (); returnDateinstring.c_str (); }};intMain () {Date Holiday ( -, A, .); cout<<"Holiday is on:"<< Holiday <<Endl; return 0;}//results: Holiday is on:25/12/2011

"21 days C + +" Note June 27

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.