0
0
C++programming~10 mins

Object creation and destruction flow in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A[]
B{}
C()
D<>
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.
2fill in blank
medium

Complete 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'
A{}
B()
C[]
D<>
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using parentheses () instead of curly braces {} for the destructor body.
Using invalid brackets like [] or <>.
3fill in blank
hard

Fix 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'
A<>
B{}
C[]
D()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting parentheses () after constructor name.
Using curly braces {} instead of parentheses ().
4fill in blank
hard

Fill 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'
A()
B~MyClass()
C~MyClass
Ddelete
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using delete keyword on stack objects.
Calling destructor without parentheses.
Omitting parentheses when creating object.
5fill in blank
hard

Fill 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'
A()
B{}
C;
D() {}
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.