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)