+ 2
C++ class question
Hey, please can someone explain in lame terms this bit of code to me? #include <iostream> using namespace std; class MyClass { public: MyClass(); // Why do we need this constructor? void myPrint(); }; MyClass::MyClass() {. // What exactly is this and why do we need it? Why is the double colon operator used? } void MyClass::myPrint() {. // What is the point of this function?Why is the double colon used? cout <<"Hello"<<endl; } int main() { MyClass obj; obj.myPrint(); }
2 ответов
+ 3
Myclass() is called constructor, it is a special member function of class which has the same name as that of class. It has no return type. It is used to initialise variables because it automatically gets called the moment you create object of the class.
Myclass::Myclass() is called definition of constructor.It is where you actually initialise the variables. "::" symbol tells the scope of function. i.e it tells which class the function belongs to.
void Myclass::print() is just an ordinary function. By the name it suggests, it is used to display something.
0
Hi!
Can me someone help me to understand why I have by Code Block this problem
#include "MyClass.h"
#include <iostream>
using namespace std;
MyClass::MyClass() {
}
void MyClass::myPrint(){
cout<<"Hello"<<endl;
}
// After run I have this error >>
---Error: no member function "void MyClass :: myPrint ()" declared in class "MyClass" |---