0
0
C++programming~10 mins

Creating objects in C++ - Interactive 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() {
    [1] myCar;
    return 0;
}
Drag options to blanks, or click blank then click option'
ACar
Bint
Cvoid
Dclass
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'int' or 'void' instead of the class name.
Writing 'class' before the object name.
2fill in blank
medium

Complete the code to create an object book1 of class Book using the default constructor.

C++
class Book {
public:
    Book() {}
};

int main() {
    [1] book1;
    return 0;
}
Drag options to blanks, or click blank then click option'
Aint
Bvoid
CBook
Dclass
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'int' or 'void' instead of the class name.
Trying to call constructor like a function without object.
3fill in blank
hard

Fix the error in creating an object circle1 of class Circle.

C++
class Circle {
public:
    Circle(double r) {}
};

int main() {
    Circle [1];
    return 0;
}
Drag options to blanks, or click blank then click option'
Acircle1(5.0)
Bcircle1
Ccircle1 = Circle()
Dcircle1 = 5.0
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Not providing the required constructor argument.
Trying to assign a value without calling the constructor.
4fill in blank
hard

Fill both blanks to create an object student1 of class Student using the constructor with parameters.

C++
class Student {
public:
    Student(std::string n, int a) {}
};

int main() {
    [1] [2]("Alice", 20);
    return 0;
}
Drag options to blanks, or click blank then click option'
AStudent
Bstudent1
CStudent()
Dstudent1()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using parentheses after the class name in declaration.
Using parentheses after the object name without class name.
5fill in blank
hard

Fill all three blanks to create an object employee1 of class Employee using the constructor with parameters.

C++
class Employee {
public:
    Employee(std::string id, double s) {}
};

int main() {
    [1] [2]([3]);
    return 0;
}
Drag options to blanks, or click blank then click option'
AEmployee
Bemployee1
C"E123", 45000
DEmployee()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting parameters or using empty parentheses.
Using parentheses after class name instead of object name.