0

Wy du v ned MyClass::MyClass() { } , f MyClass::MyClass() { } xcluded otput gvs er thnk u )

#include <iostream> using namespace std; class MyClass { public: MyClass(); void myPrint(); }; MyClass::MyClass() { } void MyClass::myPrint() { cout <<"Hello"<<endl; } int main() { MyClass obj; obj.myPrint(); }

27th Dec 2016, 6:45 AM
ram
2 Antworten
+ 2
And what is a problem? In this code you have output hello 2 times, because constructor is invoke when you create obj(MyClass obj;) and second time you invoke mPrint() in code(obj.MyPrint)
6th Jan 2017, 9:21 AM
Dmitry Mandzyuk
Dmitry Mandzyuk - avatar
+ 1
O, you need to realise function myPrint in class, not in construtor. This was your question? Why your code make error?
7th Jan 2017, 2:05 PM
Dmitry Mandzyuk
Dmitry Mandzyuk - avatar