Back to track
#101 Medium Object-Oriented C++

Define a Class

Open the editor

Problem

Create a class with one private int and a public setter/getter.

Input

No input.

Output

Exactly what the described program prints.

Hints

  1. 1

    Use private for data

  2. 2

    Provide set/get methods

Worked solution

Reference solution

One correct implementation. Alternative approaches that satisfy the constraints are equally valid — the reviewer judges behaviour, not style.

#include <iostream>
using namespace std;

class Counter {
private:
  int value = 0;
public:
  void set(int v) { value = v; }
  int get() const { return value; }
};

int main() {
  Counter c;
  c.set(5);
  cout << c.get() << '\n';
  return 0;
}

© 2026 ChitChat