+ 1
Constructor ending with a semicolon? Is it correct? (MORE ON CLASSES, FRIEND FUNCTION TOPIC)C++
class MyClass { public: MyClass() { regVar = 0; }; private: int regVar; friend void someFunc(MyClass &obj); };
11 Respuestas
+ 5
@Veerendra it is fixed now in lesson factory 😊😊
+ 3
A constuctor/destructor is a function too. A Function without a return
value And like outside a class definition a function
implementation requires no semicolon. if we place semicolon after constructor then it will terminate the body of constructor and the function is exit without execute any statement in it
+ 3
// Cpp program to illustrate the
// concept of Constructors
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{ // Default constructor called automatically
// when the object is created
construct c;
cout << "a: "<< c.a << endl << "b: "<< c.b;
return 1;
}
this program will help you to understand more
+ 3
sorry my mistake i miss to read that semicolon yeah it is a typo they need to fix it
0
I'm asking about constructors not friend functions
0
so you mean that above code is correct?
0
Gwen actually I'm not still getting it
like in the lesson of constructor it never says to put semicolon after the braces
0
look you are also not putting semicolon after constructor definition
0
I understand your program
0
and the concept of constructors too,but the code in which sololearn has placed a semicolon after constructor definition is bothering me
0
thanks
and
I bothered you very much sorry for that