Class member function of same type of the class
The code defines class member functions of it's type of class, it executes and outputs as below message:
constructor printData
Spec1 int: 2
Spec2 int: 4
Spec1 int: 3
Spec2 int: 9
constructor printData
Spec1 int: 4
Spec2 int: 16
I am so confused that why it only prompts it's constructor "constructor printData" twice, consider it calls "spec" which is the same type of class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
using namespace std;
class printData {
int x;
public:
printData spec(int i) {
this->x=i;
cout << "Spec1 int: " << this->x << endl;
}
printData spec(int i,int j) {
this->x=i*j;
cout << "Spec2 int: " << this->x << endl;
}
printData ( )
{
cout << "constructor printData\n";
}
};
int main(void) {
printData pd;
pd.spec(2);
pd.spec(2,2);
pd.spec(3).spec(3,3);
printData *pde;
pde=new printData;
pde->spec(4).spec(4,4);
delete pde;
return 0;
}
|
You did forgot the copy constructor.
Topic archived. No new replies allowed.