+ 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(); }

2nd Nov 2016, 11:57 AM
Daniel Stanciu
Daniel Stanciu - avatar
2 Antworten
+ 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.
3rd Nov 2016, 1:46 AM
kamal joshi
kamal joshi - avatar
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" |---
5th Aug 2020, 9:15 PM
Bbrbk