---
title: "Concept of class with example"
subject: "Object Oriented Programming"
chapter: 1
key: "semester_3/object_oriented_programming:class-concept-example"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/class-concept-example
site: BCT Engineering
---

# Concept of class with example

Object Oriented Programming · Chapter 1: Introduction to Object Oriented Programming

- Asked in: 2072 Kartik, 6 marks
- Video: [Lec-42: Introduction to Classes & Objects in C++ Programming | OOPS Concept in Easiest Way](https://www.youtube.com/watch?v=TOG6xCEJU3M) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/class-concept-example
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-1

## Question

With a suitable example, explain the concept of class in C++.

## Short answer

A class is a user-defined type that bundles data members and member functions into a single unit. For example, class Student has private roll and marks, public getData() and display(); objects s1 and s2 each hold their own data, accessed only through public methods.

## Answer

A **class** is a user-defined data type that groups data members (attributes) and member functions (behaviour) into a single unit. It is a blueprint; no memory is allocated for it until **objects** are created. Members can be `private`, `protected` or `public`; by default members of a class are private.

**General form**

```cpp
class ClassName {
private:
    // data members (hidden)
public:
    // member functions (interface)
};   // semicolon is required
```

**Example**

```cpp
#include <iostream>
using namespace std;

class Student {
private:
    int roll;
    float marks;
public:
    void getData(int r, float m);      // declaration
    void display() {                   // defined inside the class (inline)
        cout << "Roll: " << roll << ", Marks: " << marks << endl;
    }
};

void Student::getData(int r, float m) { // defined outside using ::
    roll = r;
    marks = m;
}

int main() {
    Student s1, s2;                     // objects
    s1.getData(1, 78.5);
    s2.getData(2, 85);
    s1.display();
    s2.display();
    return 0;
}
```

**Output**

```text
Roll: 1, Marks: 78.5
Roll: 2, Marks: 85
```

**Points:**
- Each object has its own copy of the data members; member functions are shared by all objects.
- Private members are reached only through public functions (data hiding).
- A member function can be defined inside the class, or outside using the scope resolution operator `::`.
- Objects are accessed with the dot operator (`s1.display()`).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is true of a class?
   A. It occupies memory as soon as declared
   B. It is a blueprint; no memory is allocated until objects are created
   C. It is a variable
   D. It is a function
   Answer: B. A class is a user-defined data type; objects get the memory. (Key point: Class meaning)

2. What is the default access of members in a class?
   A. public
   B. protected
   C. private
   D. none (inaccessible to all but friends only)
   Answer: C. By default class members are private. (Key point: Default access)

3. How is a member function defined outside the class?
   A. Using the dot operator
   B. Using the arrow only
   C. It cannot be defined outside
   D. Using the scope resolution operator, e.g. Student::getData
   Answer: D. ClassName::function names the class. (Key point: Defining outside)

4. Which is true about objects s1 and s2?
   A. Each has its own data members; member functions are shared
   B. They share data members; functions are copied
   C. Each has its own functions and data
   D. They share everything
   Answer: A. Data is per object; functions exist once. (Key point: Data and function copies)
