A class bundles data and behavior—similar to Java classes but with value semantics, stack allocation, and manual control over copying when you need it.
Minimal class
class Point {
public:
int x = 0;
int y = 0;
void translate(int dx, int dy) {
x += dx;
y += dy;
}
};
Member functions receive a hidden this pointer to the current object.
Important interview questions and answers
- Q: class vs struct?
A: Default access differs (privatevspublic); otherwise equivalent. - Q: Stack vs heap objects?
A:Point p;on stack;auto p = std::make_unique<Point>();on heap with RAII.
Self-check
- What does the this pointer refer to?
- Default access for class members?
Tip: Default to private data with public methods—encapsulation like Java, but watch value vs reference semantics.
Interview prep
- class vs struct?
Default member access differs—private for class, public for struct; otherwise equivalent.
- What is this?
Pointer to the current object inside non-static member functions.