On the c++_ of the subclass and the constructor of the base class __jquery

Source: Internet
Author: User

1. If a class provides a constructor, the class does not provide a default constructor.


2. The derived class calls the parameterless constructor of the base class by default

#include <iostream>
#include <cstdio>

class cpoint{public
:
	cpoint (int x) {
		printf ( "has synax\n");
	}

	CPoint () {
		printf ("No synax\n");
	}

;

Class Point:public cpoint{public
: Point
	(int x) {
	}
};

Point P (4);

int main () {return

	0;
}





3. The Jocky class has only a parameter constructor, and a derived class can have an error if there is no parameter constructor (and no default argument exists).

#include <iostream>
#include <cstdio>

class cpoint{public
:
	cpoint (int x) {
		printf ( "has synax\n");
	}

	/*
	CPoint () {
		printf ("No synax\n");
	}
	*/
};

Class Point:public cpoint{public
: Point
	(int x) {
	}
};

Point P;

int main () {return

	0;
}



(see 2) because the derived class defaults to calling the parameterless constructor of the base class, does not exist, and the parent class parameter constructor does not get the parameter value, the error is caused.

The following two kinds of methods are handled

1 The parameters of the base class need to be initialized to assign values to the parameters of the base class through the parameter initialization table.

2 Change the constructor of the base class so that it has a default parameter.


Method 1 (assigning an initial value to a base class constructor by initializing the table) —-———— implemented as follows:

#include <iostream>
#include <cstdio>

class cpoint{public
:
	cpoint (int x) {
		printf ( "has synax\n");
	}
	
	CPoint () {
	printf ("No synax\n");
	}
	
;

Class Point:public cpoint{public
: Point
	(): CPoint (1) {
	}
};

Point P;

int main () {return

	0;
}




Method 2 (change the constructor of the base class so that it has a default parameter.) )----implemented as follows

#include <iostream>
#include <cstdio>

class cpoint{public
:
	cpoint (int x = 0) {
		printf ("has synax\n");
	}
	/*
	CPoint () {
		printf ("No synax\n");
	}
	*/
};

Class Point:public cpoint{public
: Point
	() {
	}
};

Point P;

int main () {return

	0;
}


The above code is compiled through VS2013

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.