0
0
C++programming~10 mins

Default constructor 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 default constructor for class Car.

C++
class Car {
public:
    [1]() {}
};
Drag options to blanks, or click blank then click option'
Avoid
B~Car
CCar
Dint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a return type like void or int for the constructor.
Using the destructor name (~Car) instead of the constructor.
2fill in blank
medium

Complete the code to create an object car1 using the default constructor.

C++
Car [1];
Drag options to blanks, or click blank then click option'
Acar1()
Bcar1
CCar
DCar()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding parentheses after the object name like car1().
Trying to call the class name instead of the object name.
3fill in blank
hard

Fix the error in the default constructor declaration.

C++
class Bike {
public:
    [1]() {}
};
Drag options to blanks, or click blank then click option'
Avoid
B~Bike
Cint
DBike
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type to the constructor.
Using the destructor name instead of the constructor.
4fill in blank
hard

Fill both blanks to define a default constructor that sets wheels to 4.

C++
class Vehicle {
public:
    int wheels;
    [1]() {
        wheels [2] 4;
    }
};
Drag options to blanks, or click blank then click option'
AVehicle
Bwheels
C=
D==
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '==' instead of '=' for assignment.
Using a different name than the class for the constructor.
5fill in blank
hard

Fill all three blanks to create a default constructor that initializes brand and sets speed to 0.

C++
class Bike {
public:
    std::string [1];
    int [2];
    [3]() {
        brand = "Yamaha";
        speed = 0;
    }
};
Drag options to blanks, or click blank then click option'
Abrand
Bspeed
CBike
Dvoid
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'void' as a return type for the constructor.
Mixing up variable names or constructor name.