Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a constructor for class MyClass.
C++
class MyClass { public: MyClass() [1] { // constructor body } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using parentheses () instead of curly braces {} for the constructor body.
Using square brackets [] or angle brackets <> which are invalid here.
β Incorrect
The constructor body is enclosed in curly braces {} in C++.
2fill in blank
mediumComplete the code to define a destructor for class MyClass.
C++
class MyClass { public: ~MyClass() [1] { // destructor body } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using parentheses () instead of curly braces {} for the destructor body.
Using invalid brackets like [] or <>.
β Incorrect
Destructor bodies are defined with curly braces {} in C++.
3fill in blank
hardFix the error in the constructor definition syntax.
C++
class MyClass { public: MyClass [1] { // constructor body } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Omitting parentheses () after constructor name.
Using curly braces {} instead of parentheses ().
β Incorrect
Constructor declarations require parentheses () after the name, even if no parameters.
4fill in blank
hardFill both blanks to create an object and call its destructor explicitly.
C++
MyClass obj[1]; obj[2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using delete keyword on stack objects.
Calling destructor without parentheses.
Omitting parentheses when creating object.
β Incorrect
Objects are created with parentheses (). The destructor can be called explicitly as ~MyClass().
5fill in blank
hardFill all three blanks to define a class with constructor, destructor, and create an object.
C++
class MyClass { public: MyClass[1] [2]; ~MyClass() [3]; }; MyClass obj();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using semicolon instead of curly braces for function bodies.
Omitting parentheses in constructor declaration.
Confusing declaration and definition syntax.
β Incorrect
Constructor declaration uses (), definition uses {}. Destructor also uses {} for body.