Can one constructor of a class call another constructor of the same class to initialize this object?
Answer:
Onward C++11 Yes, let see an example,
#include <iostream> using namespace std; class Test { int a, b; public: Test(int x, int y) { a= x; b =y; } Test(int y) : Test( 7, y) {} void displayXY() { cout <<"a = "<<a<<endl; cout <<"b = "<<b<<endl; } }; int main() { Test obj(27); obj.displayXY(); return 0; }
Output:
a = 7b = 27
Note: Using some tricks you can also do in C++03. If you want to know how or know the answer then please write in the comment box.
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Answer:
Onward C++11 Yes, let see an example,
Output:
a = 7
b = 27
Note: Using some tricks you can also do in C++03. If you want to know how or know the answer then please write in the comment box.
need an explanation for this answer? contact us directly to get an explanation for this answer