How c ++ constructor works

Source: Internet
Author: User
Tags gety
# Include <iostream. h> Class A {int A; public: A (int x): A (x) {cout <;}~ A () {cout <a ;}}; Class B: A {int B, C; const int D; A X, Y; public:
B(int v): b(v),y(b+2),x(b+1),          d(b),A(v){        c=v;        cout<<b<<c<<d;        cout<<'C';    }    ~B( ) { cout<<'D'; }};void main(void){   B z(1);  } 


 

What does this code output?

1 first a (v) is 1

2 a x: 2

3 a y: 3

4 B (INT V): 1 1 1 c

5 ~ B (): d

6 A y analysis structure: 3

7A x analysis structure: 2

8 A (V) structure: 1

C ++ constructor follows a principle

1. virtual base class first

2 base classes

3. Define the sequence of members (initialization List)

4. derived class Constructor

At the same time, the Destructor is opposite to the destructor.

The type of objects actually pointed to by the parent class pointer is different. The behavior of virtual functions called by the parent class pointer is different, resulting in polymorphism.
# Include <iostream. h> [Example 6.7] class point {int X, Y; public: int getx () {return X;} int Gety () {return y;} void show () {cout <"show a point \ n";} Point (int x, int y) {point: x = x; point: Y = y ;}}; class circle: Public point {int R; public: int getr () {return r ;}
void show( ) { cout<<"Show a circle\n"; }Circle(int x, int y, int r):Point(x, y) { Circle::r=r; }};void main(void){    Circle c(3, 7, 8);    Point *p=&c;    cout<<"The circle with radius "; cout<<c.getr( );    cout<<" is at ("<<p->getx( );cout<<", "<<p->gety( )<<")\n";    p->show( );}

What does this function output?

Because there are no virtual functions, even if the point class Pointer Points to the Circle class, when we call the show function, the point class function is also used, because the compiler considered it a point type before

No dynamic search is available.

PThe compiled program can only check the semantics statically according to the type definition. Because the parent class pointer can direct to the subclass object, whether it points to the parent class object or the subclass objectIt can only be determined at runtimeTherefore, during compilation, only the objects pointed to by the parent class pointer can be treated as parent class objects. Therefore, when accessing data members or function members of these objects,Cannot surpass the parent class pairThe access permission specified for the corresponding member.

If the base class does not have a parent-child relationship with the derived class, the base class pointer defined by a common function cannot direct to the object of the derived class, but must be forcibly converted to direct to the object of the derived class.

Reload, overwrite, and hide member functions
It is easy to confuse overload, override, and hide member functions. c ++ programmers must understand
Concept, otherwise the error will be hard to prevent.
8.2.1 heavy load and coverage
Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
Override refers to the function of a derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;

(4) basic functions must have virtual keywords.

  • Previous Article: C ++ knowledge point Review
  • Next article: how to sort interview training pancakes
  • View comments * the above comments only represent their personal opinions, and do not represent the opinions or positions of the csdn website


    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.