0
0
C++programming~10 mins

Classes and objects 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 class named Car.

C++
class [1] {
public:
    int speed;
};
Drag options to blanks, or click blank then click option'
ASpeed
Bcar
CVehicle
DCar
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase names for class names.
Using unrelated names like Vehicle or Speed.
2fill in blank
medium

Complete the code to create an object named myCar of class Car.

C++
Car [1];
Drag options to blanks, or click blank then click option'
ACar
BmyCar
Ccar1
Dobject
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the class name instead of the object name.
Using generic names like object.
3fill in blank
hard

Fix the error in the code to access the speed attribute of myCar.

C++
myCar.[1] = 100;
Drag options to blanks, or click blank then click option'
Aspeed()
BSpeed
Cspeed
DgetSpeed
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using uppercase attribute names.
Adding parentheses to access attributes.
4fill in blank
hard

Fill both blanks to define a constructor that sets speed to 0.

C++
class Car {
public:
    int speed;
    [1] [2] {
        speed = 0;
    }
};
Drag options to blanks, or click blank then click option'
ACar()
Bvoid
C()
D{}
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type like void to the constructor.
Using parentheses without braces for the body.
5fill in blank
hard

Fill all three blanks to create a method named accelerate that increases speed by 10.

C++
class Car {
public:
    int speed;
    void [1][2] [3] {
        speed += 10;
    }
};
Drag options to blanks, or click blank then click option'
Aaccelerate
B()
C{}
DspeedUp
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using incorrect method names.
Omitting parentheses or braces.