C ++ learning code

Source: Internet
Author: User

1. Reload operators for complex data operations

#include<iostream>using namespace std;using std::cout;using std::endl;class Complex{private:    double real;    double image;public:    Complex(double real=0.0,double image=0.0)    {        this->real=real;this->image=image;    }    void display()    {        cout<<"("<<real<<","<<image<<")"<<endl;    }    friend Complex operator +(Complex A,Complex B)    {        return Complex(A.real+B.real,A.image+B.image);    }    friend Complex operator -(Complex A,Complex B)    {        return Complex(A.real-B.real,A.image-B.image);    }    friend Complex operator -(Complex A)    {        return Complex(-A.real,-A.image);    }    friend Complex operator ++(Complex A)    {        return Complex(++A.real,A.image);    }    friend Complex operator ++(Complex A,int)    {        return Complex(A.real++,A.image);    }};int main(){    int i;    Complex A(100.0,200.0),B(-10.0,20.0),C;    cout<<"A=",     A.display();    cout<<"B=",     B.display();    C=A+B;    cout<<"C=A+B=",C.display();    C=A-B;    cout<<"C=A-B=",C.display();    C=-A+B;    cout<<"C=-A+B=",C.display();    C=A++;    cout<<"C=A++=",C.display();    C=++A;    cout<<"C=++A=",C.display();    C=A+5;    cout<<"C=A+5=",C.display();    cin>>i;}

2. Implement Dynamic Association of virtual functions

Virtual functions make the operations of member functions generalized. When the pointer of the base class points to the object of the derived class, the base class pointer is used to access the virtual member function, and the access pointer actually points to the member function of the object, instead of member functions in the base class. If it is not defined as a virtual function, the base class pointer accesses the member functions of the base class, regardless of whether it is a base class or a derived class object.

#include<iostream>using namespace std;class Point{private:    int X,Y;public:    Point(int X=0,int Y=0)    {        this->X=X;this->Y=Y;    }    virtual double area()    {        return 0.0;    }};const double PI=3.14159;class Circle:public Point{private:    double radius;public:    Circle(int X,int Y,double R):Point(X,Y)    {        radius=R;    }    double area()    {        return PI*radius*radius;    }};int main(){    int i;    Point P1(10,10);    cout<<"P1.area()="<<P1.area()<<endl;    Circle C1(10,10,20);    cout<<"C1.area()="<<C1.area()<<endl;    Point *pp;    pp=&C1;    cout<<"pp->area()="<<pp->area()<<endl;    Point &rp=C1;    cout<<"rp.area()="<<rp.area()<<endl;    cin>>i;}

3. virtual base class

To avoid data inconsistency caused by different copies of data members with the same name inherited from different channels, set the common base class to a virtual base class.

In the entire inheritance structure, all the derived classes that inherit the virtual base class directly or in brief must be listed in the initialization list of the constructor. However, only the constructor of the farthest derived class used to create the object of the derived class calls the constructor of the virtual base class, the call to the constructor of this virtual base class listed in other non-virtual base classes of the derived class is ignored to ensure that the public virtual base class object is initialized only once.

#include<iostream>using namespace std;class Automobile{private:    int power;public:    Automobile(int power)    {        this->power=power;        cout<<"Automobile constructing..."<<endl;    }    void show()    {        cout<<"  power:"<<power;    }};class Car:virtual public Automobile{private:    int seat;public:    Car(int power,int seat):Automobile(power)    {        this->seat=seat;        cout<<"Car constructing..."<<endl;    }    void show()    {        cout<<"car";        Automobile::show();        cout<<"  seat:"<<seat<<endl;    }};class Wagon:virtual public Automobile{private:    int load;public:    Wagon(int power,int load):Automobile(power)    {        this->load=load;        cout<<"Wagon constructing..."<<endl;    }    void show()    {        cout<<"Wagon";        Automobile::show();        cout<<"  load:"<<load<<endl;    }};class StationWagon:public Car,public Wagon{public:    StationWagon(int CPower,int WPower,int seat,int load):Automobile(CPower)\            ,Wagon(WPower,load),Car(CPower,seat)    {        cout<<"StationWagon constructing..."<<endl;    }    void show()    {        cout<<"StationWagon:"<<endl;        Car::show();        Wagon::show();    }};int main(){    int i;    StationWagon  SW(105,108,3,8);    SW.show();    cin>>i;}

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.