0
0
C++programming~10 mins

Procedural vs OOP approach 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 declare a function that adds two integers.

C++
int add(int a, int b) {
    return a [1] b;
}
Drag options to blanks, or click blank then click option'
A-
B/
C*
D+
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
2fill in blank
medium

Complete the code to define a struct named Point with two integer members x and y.

C++
struct [1] {
    int x;
    int y;
};
Drag options to blanks, or click blank then click option'
APoint
BCircle
CRectangle
DShape
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using unrelated names like Circle or Rectangle.
3fill in blank
hard

Fix the error in the class method declaration to make it a constructor.

C++
class Rectangle {
public:
    int width, height;
    [1](int w, int h) {
        width = w;
        height = h;
    }
};
Drag options to blanks, or click blank then click option'
Avoid Rectangle
BRectangle
Cinit
DsetSize
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type like void to the constructor.
4fill in blank
hard

Fill both blanks to complete the procedural function that calculates area and the OOP method that returns area.

C++
int calculateArea(int [1], int [2]) {
    return [1] * [2];
}

class Rectangle {
public:
    int width, height;
    int area() {
        return width * height;
    }
};
Drag options to blanks, or click blank then click option'
Awidth
Bheight
Clength
Dsize
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different or inconsistent parameter names.
5fill in blank
hard

Fill all three blanks to complete the OOP class with constructor and method to calculate area.

C++
class Circle {
public:
    double [1];
    Circle(double [2]) {
        [1] = [2];
    }
    double area() {
        return 3.14159 * [1] * [1];
    }
};
Drag options to blanks, or click blank then click option'
Aradius
Br
CradiusValue
Ddiameter
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the same name for parameter and member without this->, causing confusion.