---
title: "Simple program illustrating OOP concept"
subject: "Object Oriented Programming"
chapter: 1
key: "semester_3/object_oriented_programming:oop-simple-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/oop-simple-program
site: BCT Engineering
---

# Simple program illustrating OOP concept

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

- Asked in: 2074 Chaitra, 3 marks
- Video: [Introduction To Classes And Objects | C++ Tutorial](https://www.youtube.com/watch?v=W1CjYKmTB-c) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/oop-simple-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-1

## Question

Write a simple program that illustrates the object oriented concept.

## Short answer

A Rectangle class has private length and breadth, public setData and area methods. In main(), objects r1 and r2 are created, dimensions set, and areas printed. This illustrates class/object, encapsulation, and message passing.

## Answer

The program below defines a class `Rectangle` (encapsulation: data is private, functions are public), creates objects of it, and calls member functions (message passing).

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

class Rectangle {
private:
    float length, breadth;          // data hidden from outside
public:
    void setData(float l, float b) {   // set values
        length = l;
        breadth = b;
    }
    float area() {                     // behaviour of the object
        return length * breadth;
    }
};

int main() {
    Rectangle r1, r2;                  // two objects of class Rectangle
    r1.setData(5, 3);
    r2.setData(10, 4.5);
    cout << "Area of r1 = " << r1.area() << endl;
    cout << "Area of r2 = " << r2.area() << endl;
    return 0;
}
```

**Output**

```text
Area of r1 = 15
Area of r2 = 45
```

**OOP concepts shown:**
- **Class and object** - `Rectangle` is the class; `r1` and `r2` are objects.
- **Encapsulation and data hiding** - `length` and `breadth` are private and reached only through `setData()`.
- **Message passing** - `r1.area()` sends a message to object `r1`.

## Check yourself

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

1. Why are length and breadth declared private in Rectangle?
   A. To hide data so it is reached only via setData()
   B. So that main() can change them directly
   C. To make the class run faster
   D. Because classes cannot have public data
   Answer: A. Private members give data hiding; access is only through public member functions. (Key point: Encapsulation in program)

2. In the program, what are r1 and r2?
   A. Classes
   B. Objects (instances) of class Rectangle
   C. Member functions
   D. Private data members
   Answer: B. Rectangle is the class (blueprint); r1 and r2 are objects created from it. (Key point: Objects)

3. What OOP concept is shown by r1.area()?
   A. Inheritance
   B. Operator overloading
   C. Message passing to object r1
   D. Dynamic binding
   Answer: C. Calling a member function through an object is message passing. (Key point: Message passing)

4. What is printed for r2 after setData(10, 4.5)?
   A. Area of r2 = 14.5
   B. Area of r2 = 15
   C. Area of r2 = 4.5
   D. Area of r2 = 45
   Answer: D. area() returns length * breadth = 10 * 4.5 = 45. (Key point: Output)

## Related in Object Oriented Programming

- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
- [Compare procedural and object oriented programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-vs-oop-compare) — Object Oriented Programming, chapter 1
- [Transpose of matrix using OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amatrix-transpose-oop) — Object Oriented Programming, chapter 1
- [How OOP differs from POP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-vs-pop-differ) — Object Oriented Programming, chapter 1
- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
- [Concept of class with example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-concept-example) — Object Oriented Programming, chapter 1

## Related in other subjects

- [Object oriented system and core concepts](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoo-system-concepts-explained) — Object Oriented Analysis and Design, chapter 1
- [Algorithmic vs object-oriented decomposition](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aalgorithmic-vs-oo-decomposition) — Object Oriented Analysis and Design, chapter 1
- [Stored program concept and processing cycle](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Astored-program-processing-cycle) — Microprocessor, chapter 1, asked 6×
- [Is OOP enough in OOAD](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoop-enough-in-ooad) — Object Oriented Analysis and Design, chapter 1
