Bird
Raised Fist0
C++programming~20 mins

Class definition syntax in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Class Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of simple class member access
What is the output of this C++ code?
C++
class Box {
public:
    int length;
    Box(int l) { length = l; }
};

int main() {
    Box b(5);
    std::cout << b.length << std::endl;
    return 0;
}
A5
B0
CCompilation error
DUndefined behavior
Attempts:
2 left
πŸ’‘ Hint
Look at how the constructor sets the length and how it's accessed.
πŸ“ Syntax
intermediate
2:00remaining
Identify the syntax error in class definition
Which option contains a syntax error in the class definition?
C++
class Person {
public
    std::string name;
    int age;
};
AMissing semicolon after class name
BMissing semicolon after public keyword
CMissing return type for constructor
DMissing curly braces around class body
Attempts:
2 left
πŸ’‘ Hint
Check the line with the access specifier.
πŸ”§ Debug
advanced
2:00remaining
Why does this class cause a compilation error?
Given the code below, why does it cause a compilation error?
C++
class Counter {
private:
    int count;
public:
    Counter() { count = 0; }
    void increment() { count++; }
};

int main() {
    Counter c;
    c.count = 5;
    c.increment();
    std::cout << c.count << std::endl;
    return 0;
}
Aincrement() is not declared as static
BMissing semicolon after class definition
CConstructor does not initialize count
Dcount is private and cannot be accessed directly outside the class
Attempts:
2 left
πŸ’‘ Hint
Check the access level of count and where it is accessed.
🧠 Conceptual
advanced
2:00remaining
Class member initialization order
What will be the output of this code?
C++
class Test {
public:
    int x;
    int y;
    Test() : y(10), x(y + 5) {}
};

int main() {
    Test t;
    std::cout << t.x << ", " << t.y << std::endl;
    return 0;
}
Ax=garbage, y=10
Bx=5, y=10
Cx=garbage value, y=10 (because x initialized before y)
DUninitialized x, y=10
Attempts:
2 left
πŸ’‘ Hint
Members are initialized in the order they are declared, not the order in initializer list.
❓ Predict Output
expert
2:00remaining
Output of class with static member
What is the output of this program?
C++
class Sample {
public:
    static int count;
    Sample() { count++; }
};

int Sample::count = 0;

int main() {
    Sample a;
    Sample b;
    Sample c;
    std::cout << Sample::count << std::endl;
    return 0;
}
A3
B0
CCompilation error due to static member
D1
Attempts:
2 left
πŸ’‘ Hint
Static members are shared across all instances.

Practice

(1/5)
1. What is the correct way to start defining a class named Car in C++?
easy
A. class Car { };
B. define Car { };
C. Car class { };
D. class = Car { };

Solution

  1. Step 1: Recognize the class keyword usage

    In C++, a class is defined using the keyword class followed by the class name and curly braces.
  2. Step 2: Check the syntax correctness

    The correct syntax is class Car { }; with a semicolon at the end.
  3. Final Answer:

    class Car { }; -> Option A
  4. Quick Check:

    class keyword + name + braces + semicolon = correct [OK]
Hint: Use 'class ClassName { };' to define a class [OK]
Common Mistakes:
  • Omitting the semicolon after the class definition
  • Using 'define' instead of 'class'
  • Placing '=' between class and name
2. Which of the following is the correct syntax to declare a public member function start() inside a class?
easy
A. public void start();
B. void public start();
C. void start() public;
D. public: void start();

Solution

  1. Step 1: Understand access specifier placement

    In C++, access specifiers like public: are labels followed by a colon, not keywords before functions.
  2. Step 2: Identify correct function declaration

    The function declaration inside the class is void start(); placed under the public: label.
  3. Final Answer:

    public: void start(); -> Option D
  4. Quick Check:

    Access specifier label + function declaration = correct [OK]
Hint: Use 'public:' label before members, not before return type [OK]
Common Mistakes:
  • Writing 'public' before return type without colon
  • Placing 'public' after function parentheses
  • Using 'void public' which is invalid
3. What will be the output of this code?
class Box {
public:
  int length;
};

int main() {
  Box b;
  b.length = 10;
  std::cout << b.length << std::endl;
  return 0;
}
medium
A. Compilation error
B. 10
C. 0
D. Undefined behavior

Solution

  1. Step 1: Analyze class and member access

    The class Box has a public integer member length, so it can be accessed and assigned outside the class.
  2. Step 2: Trace the main function execution

    An object b is created, b.length is set to 10, then printed. This will output 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Public member assigned 10, printed 10 [OK]
Hint: Public members can be accessed and printed directly [OK]
Common Mistakes:
  • Assuming private by default causes error
  • Forgetting to assign value before printing
  • Expecting zero because of uninitialized variable
4. Identify the error in this class definition:
class Person {
  string name;
public:
  void setName(string n) {
    name = n;
  }
};
medium
A. Missing semicolon after class definition
B. Member variable 'name' should be public
C. Missing #include <string> and std:: prefix
D. Function setName should return string

Solution

  1. Step 1: Check for syntax errors

    The class ends with a semicolon, so no missing semicolon error.
  2. Step 2: Check usage of string type

    The code uses string without including <string> header and without std:: prefix, which causes compilation error.
  3. Final Answer:

    Missing #include <string> and std:: prefix -> Option C
  4. Quick Check:

    Use #include and std::string for string type [OK]
Hint: Always include <string> and use std::string in C++ [OK]
Common Mistakes:
  • Assuming string is built-in without include
  • Thinking private members must be public to work
  • Expecting setName to return a value
5. You want to define a class Rectangle with private members width and height, and a public function area() that returns the area. Which is the correct class definition?
hard
A. class Rectangle { private: int width, height; public: int area() { return width * height; } };
B. class Rectangle { public: int width, height; int area() { return width * height; } };
C. class Rectangle { int width, height; int area() { return width * height; } };
D. class Rectangle { private: int width, height; int area() { return width * height; } };

Solution

  1. Step 1: Identify private members declaration

    Private members must be declared under private: label to restrict access.
  2. Step 2: Confirm public function declaration

    The function area() should be public to allow access from outside.
  3. Step 3: Check for correct syntax and placement

    class Rectangle { private: int width, height; public: int area() { return width * height; } }; correctly places width and height as private and area() as public with proper syntax.
  4. Final Answer:

    class Rectangle { private: int width, height; public: int area() { return width * height; } }; -> Option A
  5. Quick Check:

    Private data + public method = correct class design [OK]
Hint: Use 'private:' for data, 'public:' for functions in class [OK]
Common Mistakes:
  • Forgetting access specifiers, making members public by default
  • Placing function inside private when it should be public
  • Omitting semicolon after class definition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard