0
0
C++programming~10 mins

Class definition syntax 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 define a class named Car.

C++
class [1] {
public:
    int speed;
};
Drag options to blanks, or click blank then click option'
ACar
Bcar
CVehicle
DSpeed
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase 'car' instead of 'Car'.
Using a different class name like 'Vehicle'.
2fill in blank
medium

Complete the code to declare a public integer member named 'age' inside the class.

C++
class Person {
public:
    [1] age;
};
Drag options to blanks, or click blank then click option'
Afloat
Bstring
Cint
Dbool
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'float' or 'string' instead of 'int'.
Using 'bool' which is for true/false values.
3fill in blank
hard

Fix the error in the class definition by completing the missing keyword.

C++
class Animal {
    [1]:
    int legs;
};
Drag options to blanks, or click blank then click option'
Aprivate
Bfriend
Cprotected
Dpublic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'private' which hides members.
Using 'friend' which is not an access specifier.
4fill in blank
hard

Fill both blanks to define a class with a private string member 'name' and a public integer member 'id'.

C++
class Employee {
    [1]:
    std::string name;

    [2]:
    int id;
};
Drag options to blanks, or click blank then click option'
Aprivate
Bpublic
Cprotected
Dfriend
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Mixing up private and public keywords.
Using 'friend' which is not an access specifier.
5fill in blank
hard

Fill all three blanks to define a class 'Book' with a private string 'title', a public integer 'pages', and a protected float 'price'.

C++
class Book {
    [1]:
    std::string title;

    [2]:
    int pages;

    [3]:
    float price;
};
Drag options to blanks, or click blank then click option'
Aprivate
Bpublic
Cprotected
Dfriend
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'friend' as an access specifier.
Confusing protected and private keywords.