0
0
C++programming~10 mins

Object lifecycle 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 the class.

C++
class MyClass {
public:
    [1]() {
        // Constructor body
    }
};
Drag options to blanks, or click blank then click option'
AMyClass
BMyClass()
Cvoid MyClass
D~MyClass
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type like void to the constructor.
Using the destructor name (~MyClass) instead of the constructor.
2fill in blank
medium

Complete the code to declare a destructor for the class.

C++
class MyClass {
public:
    [1]() {
        // Destructor body
    }
};
Drag options to blanks, or click blank then click option'
AMyClass
B~MyClass
Cvoid ~MyClass
DDestructor
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting the tilde (~) before the class name.
Adding a return type like void to the destructor.
3fill in blank
hard

Fix the error in the constructor definition to properly initialize the member variable.

C++
class MyClass {
    int value;
public:
    MyClass(int v) {
        value [1] v;
    }
};
Drag options to blanks, or click blank then click option'
A:
B==
C=
D->
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '==' which is a comparison operator, not assignment.
Using ':' inside the constructor body instead of an initializer list.
4fill in blank
hard

Fill both blanks to use an initializer list to initialize the member variable.

C++
class MyClass {
    int value;
public:
    MyClass(int v) [1] value[2] v) {}
};
Drag options to blanks, or click blank then click option'
A:
B=
C(
D{
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '=' instead of ':' to start the initializer list.
Using braces '{}' instead of parentheses '()' for initialization.
5fill in blank
hard

Complete the code to define a copy constructor that copies the member variable.

C++
class MyClass {
    int value;
public:
    MyClass(const MyClass& other) : value( [1] ) {}
};
Drag options to blanks, or click blank then click option'
A:
Bother.value
C(
Dvalue
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting the colon to start the initializer list.
Using the wrong variable name or missing parentheses.