Plus Two Computer Science · Chapter 02

Concepts of Object Oriented Programming

2 sections, 9 sub-topics
12 review questions
2.1

Programming Paradigm

A programming paradigm is how to organize a program. As programs grow, some approach is needed to manage complexity. C++ supports several — most importantly the procedural and object-oriented paradigms.

2.1.1

Procedure-Oriented Programming Paradigm

The procedural approach organises a program as a series of well-structured steps, grouped into functions, and then grouped further into modules.Data and the functions that act on it are never bound together, procedural code models real-world objects poorly.

2.1.2

Object-Oriented Programming (OOP) Paradigm

OOP It binds data and the functions that operate on it into a single unit called an object.

2.2

Basic Concepts of OOP

2.2.1

Objects

Anything that have properties and behaviour can be treated as an object. Objects rather than functions makes program design onto the real world.

2.2.2

Classes

A class is the blueprint that defines everything about an object — its data and the functions that act on it — and any number of objects (instances) can be created from one class. It's declared like a structure, but with the keyword class, and unlike a structure, it specifies both data and functions together.

class circle
{
private:
float r;
public:
void get_radius()
{
cin >> r;
}
void display_area()
{
cout << 3.14*r*r;
}
};
circle C1; // object of the class
C1.get_radius(); // sending a message

Calling a member function of one object from another (or from main) is called passing a message — e.g. S1.update_fees("Rahul", 1000);

2.2.3

Data Abstraction

Abstraction means showing only the essential features of an object and hiding the rest — like using a television's power button and volume control without knowing how it decodes a broadcast signal internally. C++ classes achieve this by exposing public methods (the interface) while keeping the actual code (the implementation) hidden, so the implementation can change later without breaking anything that calls the interface.

2.2.4

Data Encapsulation

Encapsulation binds data and the functions that manipulate it into one unit, and keeps both safe from outside interference. C++ implements this through access specifiers inside a class: members are private by default (invisible outside the class), protected members are visible to derived classes only, and public members are accessible from anywhere in the program.

2.2.5

Modularity

Modularity partitions a program into independently written modules that are later linked together and communicate by passing messages. In OOP, a class is the unit of modularity — a school system, for example, can separate everything related to students and teachers into two independent modules.

2.2.6

Inheritance

Inheritance lets one class (the derived class) acquire the properties and functions of another (the base class), supporting hierarchical classification and reusability — features can be added to an existing class without modifying it. Forms include single, multiple, multilevel, hierarchical, and hybrid inheritance.

class Cone : public Circle
{
// Cone derives from Circle
private:
float s; // only the new member is needed
public:
void get_cone_data()
{
get_radius();
cin >> s;
}
void display_cone_area()
{
cout << 3.14*r*(s+r);
}
};
2.2.7

Polymorphism

"Poly" (many) + "morph" (shapes): polymorphism is the ability of the same message or function call to behave differently depending on the object it's applied to — calling area() on a circle, rectangle, or triangle each returns the correct result for that shape. It comes in two forms: compile-time (early binding) polymorphism, resolved by the compiler via function or operator overloading, and run-time (late binding) polymorphism, resolved during execution using pointers and inheritance.

int area(int s)
{
return s * s;
} // area of square
int area(int s1, int s2)
{
return s1 * s2;
} // rectangle — same name, different behaviour(output based on number of paramenters)

Self-Assessment

Question & Answer Review

Tap a question to reveal the answer — a quick way to check understanding before moving to Chapter 3.

01What is a programming paradigm?
The organising approach used to structure a program — such as procedural or object-oriented — chosen to manage complexity as programs grow larger.
02What are the main limitations of the procedural paradigm?
Data is undervalued and left globally exposed; adding a new data element can require changes across many functions; creating new data types is difficult (poor extensibility); and it models real-world objects poorly since data and functions aren't bound together.
03What is an object in OOP?
A single unit that bundles data (its properties/state) together with the functions that operate on that data (its behaviour/methods).
04What is a class?
A blueprint that defines the data and functions common to all objects of a particular type; individual objects created from it are called instances.
05How does a class differ from a structure?
A structure specifies only data; a class specifies both data and the functions that act on that data. A structure is declared with struct, a class with class.
06What is data abstraction?
Showing only the essential features of an object to the outside world (its interface) while hiding the implementation details — like using a TV's controls without knowing how it decodes a signal internally.
07What is encapsulation, and how does C++ implement it?
Binding data and the functions that operate on it into a single unit and protecting both from outside interference — implemented in C++ through the private, protected, and public access specifiers inside a class.
08What is modularity?
Partitioning a program into independent modules — implemented in OOP through classes — that can be developed separately and later linked together, communicating by passing messages.
09What is inheritance, and what are base and derived classes?
The process by which one class (the derived class) acquires the properties and functions of another (the base class), supporting reusability — new features can be added without modifying the original class.
10What is polymorphism?
The ability of the same function or message to behave differently depending on the object or data type it's applied to — e.g. calling area() correctly whether the object is a circle, rectangle, or triangle.
11What's the difference between compile-time and run-time polymorphism?
Compile-time (early binding) polymorphism — function and operator overloading — is resolved by the compiler during compilation. Run-time (late binding) polymorphism is resolved during execution, using pointers and inheritance.
12What is function overloading?
Defining multiple functions with the same name but different signatures (different number or types of parameters), so the same name behaves differently depending on how it's called — e.g. area(int) for a square versus area(int, int) for a rectangle.