0
0
C++programming~10 mins

Object interaction 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 create an object of class Car named myCar.

C++
class Car {};

int main() {
    Car [1];
    return 0;
}
Drag options to blanks, or click blank then click option'
AmyCar
Bcar1
CCar
Dobject
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the class name as the object name.
Forgetting to name the object.
2fill in blank
medium

Complete the code to call the startEngine method on the object myCar.

C++
class Car {
public:
    void startEngine() {}
};

int main() {
    Car myCar;
    myCar.[1]();
    return 0;
}
Drag options to blanks, or click blank then click option'
ArunEngine
BstopEngine
CengineStart
DstartEngine
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a wrong method name.
Forgetting the parentheses after the method name.
3fill in blank
hard

Fix the error in the code to access the speed attribute of the object car1.

C++
class Car {
public:
    int speed;
};

int main() {
    Car car1;
    car1.[1] = 100;
    return 0;
}
Drag options to blanks, or click blank then click option'
Acar1.speed
BSpeed
Cspeed
Dthis.speed
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using uppercase 'Speed' instead of 'speed'.
Trying to use 'this' pointer outside class methods.
4fill in blank
hard

Fill both blanks to create a method setSpeed that sets the speed attribute.

C++
class Car {
public:
    int speed;
    void [1](int [2]) {
        speed = [2];
    }
};
Drag options to blanks, or click blank then click option'
AsetSpeed
Bspeed
CnewSpeed
DupdateSpeed
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the attribute name as parameter name without distinction.
Using wrong method names.
5fill in blank
hard

Fill all three blanks to create an object car2, set its speed to 80, and print it.

C++
#include <iostream>

class Car {
public:
    int speed;
    void setSpeed(int s) { speed = s; }
    int getSpeed() { return speed; }
};

int main() {
    Car [1];
    [1].[2](80);
    std::cout << [1].[3]() << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Acar2
BsetSpeed
CgetSpeed
Dspeed
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different object names in different places.
Calling methods with wrong names.