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

Default Constructor

Open the editor

Problem

Provide a default constructor that sets values.

Input

No input.

Output

Exactly what the described program prints.

Hints

  1. 1

    Use Point() : x(0), y(0) {}

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 Point {
public:
  Point() : x(0), y(0) {}
  int x;
  int y;
};

int main() {
  Point p;
  cout << p.x << "," << p.y << '\n';
  return 0;
}

© 2026 ChitChat