UNIT 2: Principles of Object-Oriented Programming (OOP)
Basic Concepts of OOP
Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of
objects and classes. The core idea behind OOP is to model real-world entities and their
behaviors within a program. OOP provides several benefits like reusability, scalability, and
maintainability. The four main principles of OOP are:
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
1. Encapsulation
Encapsulation refers to the bundling of data (attributes) and methods (functions) that operate on
the data into a single unit known as a class. It restricts direct access to some of an object's
components and can prevent the accidental modification of data. Encapsulation is implemented
using access modifiers such as private, protected, and public.
Private: Members are accessible only within the class.
Protected: Members are accessible within the class and derived classes.
Public: Members are accessible from anywhere.
2. Inheritance
Inheritance is a mechanism where a new class (called a derived class) inherits properties and
behaviors (methods) from an existing class (called a base class). This allows for code reuse and
helps in creating a hierarchical relationship between classes.
Single Inheritance: A class inherits from one base class.
Multiple Inheritance: A class can inherit from more than one base class (allowed in
C++ but not in some other languages like Java).
Multilevel Inheritance: A class is derived from another derived class.
Hierarchical Inheritance: Multiple classes inherit from the same base class.
3. Polymorphism
Polymorphism refers to the ability of different objects to respond to the same function or method
call in different ways. There are two types of polymorphism:
Compile-time Polymorphism (Static Polymorphism): Achieved through function
overloading and operator overloading.
Run-time Polymorphism (Dynamic Polymorphism): Achieved using inheritance and
virtual functions.
4. Abstraction
Abstraction is the concept of hiding the internal details of a system and exposing only the
essential features. It allows the programmer to focus on the higher-level functionality without
needing to understand the implementation details.
Abstract Classes: Classes that cannot be instantiated directly but can be inherited by
other classes to provide common functionality.
Pure Virtual Functions: Functions declared in an abstract class that do not have a
definition and must be overridden by derived classes.
Comparison of Procedural Programming and OOP
Aspect Procedural Programming Object-Oriented Programming (OOP)
Basic Unit Functions and procedures Objects and classes
Top-down approach (procedures
Approach Bottom-up approach (objects focus)
focus)
Data Handling Data is separate from functions Data is bundled with functions in classes
Limited (Code reuse by High (Code reuse through inheritance and
Reusability
functions) polymorphism)
Functions are separate, less
Modularity Classes and objects are modular
modular
Difficult to maintain large
Flexibility Easier to modify and extend
projects
Data Security No access restrictions Encapsulation ensures data protection
Advantages of OOP
Modularity: The program is divided into discrete classes, making it easier to manage.
Reusability: Classes and objects can be reused across different programs.
Maintainability: Easier to update and maintain due to encapsulation and inheritance.
Flexibility: Polymorphism allows flexibility in the code, where one interface can work
with different types.
Security: Encapsulation helps secure data by restricting access to certain details of the
class.
OOP Languages
Common Object-Oriented Programming languages include:
C++: One of the most popular OOP languages, offering features like inheritance,
polymorphism, encapsulation, and operator overloading.
Java: A purely object-oriented language with a strong emphasis on portability and security.
C#: A modern OOP language from Microsoft, primarily used for .NET applications.
Python: An easy-to-use OOP language, popular in data science and web development.
Ruby: A language known for its simplicity and ease of use in web development.
Class: A blueprint for creating objects. It defines properties (data members) and behaviors
(methods/functions) that the objects created from it will have.
Example:
class Car {
private:
string color;
int speed;
public:
void setColor(string c) {
color = c;
}
void setSpeed(int s) {
speed = s;
}
void display() {
cout << "Color: " << color << ", Speed: " << speed << endl;
}
};
Object: An instance of a class. When a class is defined, no memory is allocated. Memory is
allocated when an object of the class is created.
Example:
Car myCar; // myCar is an object of class Car
myCar.setColor("Red");
myCar.setSpeed(120);
myCar.display();
Inheritance
In C++, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such way, you can reuse, extend or
modify the attributes and behaviors which are defined in other class.
In C++, the class which inherits the members of another class is called derived class and
the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.
Advantage of C++ Inheritance
Code reusability: Now you can reuse the members of your parent class. So, there is no
need to define the member again. So less code is required in the class.
Types Of Inheritance
C++ supports five types of inheritance:
o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance
Derived Classes
A Derived class is defined as the class derived from the base class.
The Syntax of Derived class:
class derived_class_name : visibility-mode base_class_name
{
// body of the derived class.
}
Where,
derived_class_name: It is the name of the derived class.
Visibility mode: The visibility mode specifies whether the features of the base class are
publicly inherited or privately inherited. It can be public or private.
base_class_name: It is the name of the base class.
o When the base class is privately inherited by the derived class, public members of the
base class becomes the private members of the derived class. Therefore, the public
members of the base class are not accessible by the objects of the derived class only by
the member functions of the derived class.
o When the base class is publicly inherited by the derived class, public members of the
base class also become the public members of the derived class. Therefore, the public
members of the base class are accessible by the objects of the derived class as well as by
the member functions of the base class.
Note:
o In C++, the default mode of visibility is private.
o The private members of the base class are never inherited.
C++ Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited
from the only one base class.
Where 'A' is the base class, and 'B' is the derived class.
C++ Single Level Inheritance Example:
Inheriting Fields
When one class inherits another class, it is known as single level inheritance. Let's see
the example of single level inheritance which inherits the fields only.
Example
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Compile and Run
Output:
Salary: 60000
Bonus: 5000
function of class B.
How to make a Private Member Inheritable
The private member is not inheritable. If we modify the visibility mode by making it
public, but this takes away the advantage of data hiding.
C++ introduces a third visibility modifier, i.e., protected. The member which is declared
as protected will be accessible to all the member functions within the class as well as the
class immediately derived from it.
Visibility modes can be classified into three categories:
o Public: When the member is declared as public, it is accessible to all the functions of the
program.
o Private: When the member is declared as private, it is accessible within the class only.
o Protected: When the member is declared as protected, it is accessible within its own
class as well as the class immediately derived from it.
Base class visibility Derived class visibility
Base Visibility mode Public Private Protected
Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
Public Public Private Protected
Visibility of Inherited Members
Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another derived class.
Multi Level Inheritance Example
When one class inherits another class which is further inherited by another class, it is
known as multi level inheritance in C++. Inheritance is transitive so the last derived class
acquires all the members of all its base classes.
Example
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Compile and Run
Output:
Eating...
Barking...
Weeping...
Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes
from two or more classes.
Syntax of the Derived class:
class D : visibility B-1, visibility B-2, ?
{
// Body of the class;
}
Let's see a simple example of multiple inheritance.
Example
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}
Compile and Run
Output:
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30
In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.
Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.
Example
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};
class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
class D : public B, public C
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Compile and Run
Output:
Enter the value of 'a' :
10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000
Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class from a
base class.
Syntax of Hierarchical inheritance:
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
Example
#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <<m<< std::endl;
std::cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}
Compile and Run
Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms. It
is a greek word. In object-oriented programming, we use 3 main concepts: inheritance,
encapsulation, and polymorphism.
Key Concepts of Encapsulation:
1. Private Members: The attributes and methods are typically made private to prevent
unauthorized access.
2. Public Interface: Public methods (often called getter and setter) provide access to the
private attributes, ensuring controlled manipulation.
3. Getter and Setter: Methods that allow you to get or set the value of a private member
variable.
Why Use Encapsulation?
Data Hiding: Restricts access to an object's internal state. This protects the data from
unintended interference or misuse.
Control Over Data: You can control how data is accessed or modified using getter and
setter methods.
Improved Code Maintainability: Encapsulation makes the code more modular and
easier to manage because you can change the internal implementation without affecting
the outside code that uses the class.
Real Life Example Of Polymorphism
Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a classroom,
mother or daughter in a home and customer in a market. Here, a single person is behaving
differently according to the situations.
There are two types of polymorphism in C++:
o Compile time polymorphism: The overloaded functions are invoked by matching the
type and number of arguments. This information is available at the compile time and,
therefore, compiler selects the appropriate function at the compile time. It is achieved by
function overloading and operator overloading which is also known as static binding or
early binding. Now, let's consider the case where function name and prototype is same.
class A // base class declaration.
{
int a;
public:
void display()
{
cout<< "Class A ";
}
};
class B : public A // derived class declaration.
{
int b;
public:
void display()
{
cout<<"Class B";
}
};
o Run time polymorphism: Run time polymorphism is achieved when the object's method
is invoked at the run time instead of compile time. It is achieved by method overriding
which is also known as dynamic binding or late binding.
Differences b/w compile time and run time polymorphism.
compile time run time polymorphism.
The function to be invoked is known at the compile The function to be invoked is known at the run time.
time.
It is also known as overloading, early binding and It is also known as overriding, Dynamic binding and
static binding. late binding.
Overloading is a compile time polymorphism Overriding is a run time polymorphism where more
where more than one method is having the same than one method is having the same name, number of
name but with the different number of parameters parameters and the type of the parameters.
or the type of the parameters.
It is achieved by function overloading and operator It is achieved by virtual functions and pointers.
overloading.
It provides fast execution as it is known at the It provides slow execution as it is known at the run
compile time. time.
It is less flexible as mainly all the things execute at It is more flexible as all the things execute at the run
the compile time. time.
C++ Function Overloading
Function Overloading is defined as the process of having two or more function with the same
name, but different in parameters is known as function overloading in C++. In function
overloading, the function is redefined by using either different types of arguments or a different
number of arguments. It is only through these differences compiler can differentiate between the
functions.
The advantage of Function overloading is that it increases the readability of the program
because you don't need to use different names for the same action.
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C; // class object declaration.
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}
C++ Operator Overloading
In C++, we can define how operators behave for user-defined types like class and structures. For
example,
The + operator, when used with values of type int, returns their sum. However, when used with
objects of a user-defined type, it is an error.
In this case, we can define the behavior of the + operator to work with objects as well.
This concept of defining operators to work with objects and structure variables is known
as operator overloading.
Overloadable/Non-overloadableOperators
Following is the list of operators which can be overloaded −
+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new [] delete delete []
Following is the list of operators, which can not be overloaded −
:: .* . ?:
Syntax for C++ Operator Overloading
The syntax for overloading an operator is similar to that of function with the addition of
the operator keyword followed by the operator symbol.
Here,
returnType operator symbol (arguments) {
... .. ...
}
returnType - the return type of the function
operator - a special keyword
symbol - the operator we want to overload (+, <, -, ++, etc.)
arguments - the arguments passed to the function
A virtual function is a member function in the base class that we expect to redefine in derived
classes.
For example,
class Base {
public:
void print() {
// code
}
};
class Derived : public Base {
public:
void print() {
// code
}
};
The print() method in the Derived class shadows the print() method in the Base class.
However, if we create a pointer of Base type to point to an object of Derived class and call
the print() function, it calls the print() function of the Base class.
In other words, the member function of Base is not overridden.
int main() {
Derived derived1;
Base* base1 = &derived1;
// calls function of Base class
base1->print();
return 0;
}
To avoid this, we declare the print() function of the Base class as virtual by using
the virtual keyword.
class Base {
public:
virtual void print() override {
// code
}
};
If the virtual function is redefined in the derived class, the function in the derived class is
executed even if it is called using a pointer of the base class object pointing to a derived class
object. In such a case, the function is said to be overridden.
Virtual functions are an integral part of polymorphism in C++. To learn more, check our
tutorial on C++ Polymorphism.
Example 1: C++ virtual Function
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() override {
cout << "Derived Function" << endl;
}
};
int main() {
Derived derived1;
// pointer of Base type that points to derived1
Base* base1 = &derived1;
// calls member function of Derived class
base1->print();
return 0;
}
Main Function
The main() function is the entry point of any C++ program. When the program is run, execution
begins from the main() function. Every C++ program requires a main() function to run.
#include <iostream> // Preprocessor directive to include input-output stream
// The main function where execution starts
int main() {
std::cout << "Hello, World!" << std::endl; // Program statement to print message
return 0; // Return 0 to indicate successful execution
}
#include <iostream>: This is a preprocessor directive that includes the Input/Output
stream library needed for printing to the console (std::cout).
int main(): The main function which returns an integer. By convention, it returns 0 to
indicate successful execution.
std::cout << "Hello, World!" << std::endl;: This is a statement that prints "Hello,
World!" to the console, followed by a newline.
Comments
Comments are used to explain and annotate code. They are ignored by the compiler but help
human readers understand the program.
Single-Line Comment: Used for short comments.
// This is a single-line comment
Multi-Line Comment: Used for longer comments or explanations.
/*
This is a multi-line comment.
It spans across multiple lines.
*/
Inline Comments: Placed on the same line as the code.
int a = 10; // Variable declaration and initialization
Compilation Process
When you write a C++ program, the process to compile and execute it typically involves the
following steps:
1. Preprocessing: The preprocessor handles directives like #include, #define, and
conditional compilation.
2. Compilation: The compiler translates the source code (.cpp file) into an object file (.o or
.obj), which contains machine code instructions.
3. Linking: The linker takes the object files and links them together to create an executable
(.exe or a program file for other operating systems).
4. Execution: The operating system runs the resulting executable program