C++ 構(gòu)造函數(shù)各種怪式一網(wǎng)打盡
1.正常無參(無初始值)
View Code
#include "iostream" #include "string" using namespace std; class Point { public: Point() { cout<<"Default constructor called."<<endl; cout<<x<<" "<<y<<endl; } private: int x, y; }; int main() { Point *ptr1=new Point(); delete ptr1; }
2.正常有參
View Code
#include "iostream" #include "string" using namespace std; class Point { public: Point(int x, int y) { cout<<"Default constructor called."<<endl; cout<<x<<" "<<y<<endl; } private: int x, y; }; int main() { Point *ptr1=new Point(1, 2); delete ptr1; }
3.不正常無參(有初始值)
View Code
#include "iostream" #include "string" using namespace std; class Point { public: Point():x(0),y(0) { cout<<"Default constructor called."<<endl; cout<<x<<" "<<y<<endl; } private: int x, y; }; int main() { Point *ptr1=new Point(); delete ptr1; }
4.不正常有參
View Code
#include "iostream" #include "string" using namespace std; class Point { public: Point(int x, int y):x(x),y(y) { cout<<"Default constructor called."<<endl; cout<<x<<" "<<y<<endl; } private: int x, y; }; int main() { Point *ptr1=new Point(1, 2); delete ptr1; }
posted on 2012-04-19 20:29 More study needed. 閱讀(239) 評論(0) 收藏 舉報

浙公網(wǎng)安備 33010602011771號