0
0
C++programming~10 mins

Parameterized 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 parameterized constructor for class Car.

C++
class Car {
public:
    int year;
    Car([1]) {
        year = y;
    }
};
Drag options to blanks, or click blank then click option'
Aint y
Byear
CCar()
Dvoid y
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the class name as a parameter type
Forgetting to specify the parameter type
Using void as a parameter type
2fill in blank
medium

Complete the constructor initialization list to set the member variable.

C++
class Book {
    int pages;
public:
    Book(int p) : [1] {}
};
Drag options to blanks, or click blank then click option'
Apages = p
Bpages(p)
Cp(pages)
Dpages:p
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using assignment inside the initialization list
Reversing the order of member and parameter
Using colon instead of parentheses
3fill in blank
hard

Fix the error in the parameterized constructor declaration.

C++
class Student {
    int id;
public:
    Student([1]) {
        id = i;
    }
};
Drag options to blanks, or click blank then click option'
Aint i
Bi int
Cint
Dvoid i
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Writing the variable name before the type
Omitting the variable name
Using void as a parameter type
4fill in blank
hard

Fill both blanks to complete the constructor that initializes two member variables.

C++
class Point {
    int x, y;
public:
    Point(int a, int b) : [1], [2] {}
};
Drag options to blanks, or click blank then click option'
Ax(a)
Ba(x)
Cy(b)
Db(y)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Reversing member and parameter names
Using assignment instead of initialization list
Omitting one of the member initializations
5fill in blank
hard

Fill all three blanks to complete the constructor initializing three members with parameters.

C++
class Rectangle {
    int width, height, area;
public:
    Rectangle(int w, int h) : [1], [2], [3] {
        area = width * height;
    }
};
Drag options to blanks, or click blank then click option'
Awidth(w)
Bheight(h)
Carea(0)
Darea(w*h)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Initializing area with w*h in the list (should be done in body)
Mixing parameter and member names
Forgetting to initialize all members