Constructor ending with a semicolon? Is it correct? (MORE ON CLASSES, FRIEND FUNCTION TOPIC)C++ | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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); };

21st Dec 2017, 10:32 AM
veerendra singh
veerendra singh - avatar
11 Respostas
+ 5
@Veerendra it is fixed now in lesson factory šŸ˜ŠšŸ˜Š
21st Dec 2017, 1:31 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 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
21st Dec 2017, 10:53 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 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
21st Dec 2017, 10:59 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
sorry my mistake i miss to read that semicolon yeah it is a typo they need to fix it
21st Dec 2017, 11:09 AM
GAWEN STEASY
GAWEN STEASY - avatar
0
I'm asking about constructors not friend functions
21st Dec 2017, 10:43 AM
veerendra singh
veerendra singh - avatar
0
so you mean that above code is correct?
21st Dec 2017, 10:56 AM
veerendra singh
veerendra singh - avatar
0
Gwen actually I'm not still getting it like in the lesson of constructor it never says to put semicolon after the braces
21st Dec 2017, 10:58 AM
veerendra singh
veerendra singh - avatar
0
look you are also not putting semicolon after constructor definition
21st Dec 2017, 11:02 AM
veerendra singh
veerendra singh - avatar
0
I understand your program
21st Dec 2017, 11:02 AM
veerendra singh
veerendra singh - avatar
0
and the concept of constructors too,but the code in which sololearn has placed a semicolon after constructor definition is bothering me
21st Dec 2017, 11:04 AM
veerendra singh
veerendra singh - avatar
0
thanks and I bothered you very much sorry for that
21st Dec 2017, 11:11 AM
veerendra singh
veerendra singh - avatar