Object-Oriented Programming (C++)
Unit 1: Introduction to Object-Oriented Programming
1. Introduction to Programming Approaches
A programming approach is a method or style used to design and develop computer programs. As software became larger and more complex, different programming approaches were developed to make programs easier to write, understand, test, and maintain.
The two important approaches are:
- Structured Programming
- Object-Oriented Programming (OOP)
Structured programming mainly organizes a program around functions and procedures, whereas object-oriented programming organizes a program around objects and classes.
2. Structured Programming Approach
Structured programming is a programming approach in which a large program is divided into smaller and manageable parts called functions or procedures.
It focuses mainly on what operations a program needs to perform.
The basic idea is:
Break a large problem into smaller functions and solve each function separately.
Languages such as C, Pascal, and early versions of BASIC commonly use structured or procedural programming concepts.
Example
A student management system may contain functions such as:
void addStudent();
void deleteStudent();
void displayStudent();
void calculateMarks();
Here, the program is organized around functions that perform different operations.
3. Main Features of Structured Programming
3.1 Function-Oriented
Structured programming gives importance to functions or procedures. A large problem is divided into several smaller functions.
input();
calculate();
display();
Each function performs a particular task.
3.2 Top-Down Approach
Structured programming generally follows a top-down approach. In this approach, the overall problem is first identified and then divided into smaller subproblems.
Student Management System
|
+-- Student Registration
|
+-- Marks Calculation
|
+-- Result Generation
|
+-- Report Display
3.3 Focus on Procedures
The major focus is on procedures or functions rather than the data.
The programmer thinks about:
What should the program do?
- Read data
- Calculate data
- Display data
- Update data
3.4 Use of Global Data
In traditional structured programs, data may be shared among several functions, especially when global variables are used.
int marks;
void input()
{
cin >> marks;
}
void display()
{
cout << marks;
}
The variable can be accessed by multiple functions.
3.5 Use of Control Structures
Structured programming uses three fundamental control structures:
Sequence
Statements execute one after another.
Statement 1
Statement 2
Statement 3
Selection
A decision is made based on a condition.
- if
- if-else
- switch
Iteration
A group of statements is repeated.
- for
- while
- do-while
4. Advantages of Structured Programming
1. Simple to Understand
Small programs are relatively easy to understand because they are divided into functions.
2. Easy to Implement
The programmer can solve individual tasks using separate functions.
3. Code Reusability
Functions can sometimes be reused in different parts of a program.
4. Easier Testing
Individual functions can be tested separately.
5. Suitable for Small and Medium Programs
Structured programming is effective for relatively small programs where complexity is limited.
5. Limitations of Structured Programming
1. Difficult to Manage Large Programs
As the size of a program increases, the number of functions and relationships between them can become difficult to manage.
2. Data Security is Weak
Data can often be accessed or modified by many functions, especially when global data is used.
3. Data and Functions are Separate
The data and operations performed on that data are not naturally grouped together.
4. Difficult Maintenance
Changing one part of a large program may affect several other functions.
5. Less Suitable for Complex Real-World Systems
Real-world entities such as students, employees, bank accounts, and vehicles contain both data and behavior. Structured programming does not naturally represent these entities as single units.
6. Object-Oriented Programming Approach
Object-Oriented Programming (OOP) is a programming approach in which a program is designed around objects and classes.
An object represents an entity that contains:
- Data
- Functions or behavior
Example: Student Object
Data:
Name
Roll Number
Age
Marks
Behavior:
Read data
Display data
Calculate result
Thus, OOP combines data and the functions that operate on that data into a single unit.
Basic Idea of OOP
Represent a problem using objects that contain both data and behavior.
Example: Bank Account
Data:
Account Number
Account Holder
Balance
Functions:
Deposit()
Withdraw()
DisplayBalance()
Instead of keeping the data and functions completely separate, they are organized within a class/object structure.
7. Class
A class is a user-defined data type that acts as a blueprint or template for creating objects.
It defines:
- Data members
- Member functions
Example
class Student
{
public:
string name;
int roll;
void display()
{
cout << name << endl;
cout << roll << endl;
}
};
Here, Student is a class. It describes what information
and behavior a student object will have.
8. Object
An object is an instance of a class.
Student s1;
Student s2;
Here:
Student→ Classs1→ Objects2→ Object
A class is like a blueprint, while an object is the actual entity created from that blueprint.
Class
↓
Student
↓
Objects
├── Ram
├── Shyam
└── Hari
All objects have the properties and behaviors defined by the class, but each object can have different values.
9. Object-Oriented Programming Approach in Practice
OOP follows an approach in which a problem is analyzed in terms of objects.
For example, consider a college management system.
Possible objects include:
- Student
- Teacher
- Course
- Department
- Exam
- Result
Each object contains relevant data and operations.
Example
Student
----------------
Name
Roll Number
Address
Marks
----------------
Register()
Display()
CalculateResult()
This makes the program closer to the way we understand real-world systems.
10. Bottom-Up Approach in OOP
OOP commonly follows a bottom-up approach. In this approach, smaller components or objects are designed first and then combined to form a complete system.
Student Object
Teacher Object
Course Object
↓
College Management System
The individual objects are developed first and then integrated.
Top-Down vs Bottom-Up
Structured Programming
Large Problem
↓
Smaller Problems
↓
Functions
Object-Oriented Programming
Classes
↓
Objects
↓
Complete System
The distinction is a useful general guideline rather than an absolute rule. Real software projects may use both styles.
11. Structured Programming vs Object-Oriented Programming
| Structured Programming | Object-Oriented Programming |
|---|---|
| Mainly function-oriented | Mainly object-oriented |
| Focuses on procedures | Focuses on objects |
| Generally uses top-down approach | Generally uses bottom-up approach |
| Data and functions are usually separate | Data and functions are combined |
| Data protection is comparatively weak | Provides better data protection through encapsulation and access control |
| Less suitable for very large systems | Suitable for large and complex systems |
| Reusability mainly through functions | Reusability through classes, inheritance, and other mechanisms |
| Program is divided into functions | Program is divided into classes and objects |
| Focuses on what operations to perform | Focuses on objects, data, and behavior |
| Example: C | Examples: C++, Java, C# |
12. Characteristics of Object-Oriented Languages
Object-oriented languages provide several features that help programmers develop large and maintainable software systems.
- Objects
- Classes
- Encapsulation
- Data Abstraction
- Inheritance
- Polymorphism
- Dynamic Binding
- Message Passing
- Modularity
- Reusability
13. Objects
An object is an instance of a class. It represents a real-world or conceptual entity.
Examples:
- Student
- Employee
- Car
- Bank Account
- Book
- Computer
An object generally has:
- State
- Behavior
- Identity
State
State represents the current data or properties of an object.
Name = Ram
Roll = 10
Marks = 75
Behavior
Behavior represents what the object can do.
Register()
Display()
CalculateMarks()
Identity
Identity allows one object to be distinguished from another object.
Student s1;
Student s2;
Even if both objects have similar data, s1 and
s2 are separate objects.
14. Classes
A class is a blueprint used to create objects. A class contains data members and member functions.
class Car
{
public:
string color;
void start()
{
cout << "Car started";
}
};
Here:
Caris the class.coloris a data member.start()is a member function.
Car c1;
Car c2;
15. Encapsulation
Encapsulation is the process of combining data and the functions that operate on that data into a single unit.
In C++, a class is commonly used to achieve encapsulation.
class BankAccount
{
private:
double balance;
public:
void deposit(double amount)
{
balance += amount;
}
};
Here, balance and the function that modifies it are part
of the same class.
Encapsulation also supports controlling access to internal data using:
- private
- public
- protected
Benefits of Encapsulation
- Protects internal data
- Controls access to data
- Makes programs easier to maintain
- Reduces unwanted modification
- Improves organization
16. Data Abstraction
Abstraction means showing only the necessary information to the user while hiding unnecessary implementation details.
For example, when using an ATM, the user can:
- Withdraw money
- Deposit money
- Check balance
The user does not need to know the internal implementation of the banking software.
Example
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
};
The user only needs to call:
c.add(10, 20);
The internal implementation can remain hidden.
Encapsulation vs Abstraction
| Encapsulation | Abstraction |
|---|---|
| Combines data and methods together | Hides unnecessary implementation details |
| Focuses on data protection and access control | Focuses on essential features |
| Achieved using classes and access specifiers | Can be achieved using classes, interfaces, and abstract designs |
17. Inheritance
Inheritance is a mechanism through which one class can acquire properties and behaviors of another class.
The existing class is commonly called:
- Base class
- Parent class
The new class is commonly called:
- Derived class
- Child class
Example
class Animal
{
public:
void eat()
{
cout << "Eating";
}
};
class Dog : public Animal
{
public:
void bark()
{
cout << "Barking";
}
};
Animal
↑
Dog
Dog inherits the eat() function from
Animal.
Advantages of Inheritance
- Code reusability
- Easier extension of existing classes
- Reduces duplication
- Supports hierarchical classification
18. Polymorphism
The word polymorphism means many forms.
It allows the same interface, function name, or operation to behave differently depending on the situation.
18.1 Compile-Time Polymorphism
Examples include:
- Function overloading
- Operator overloading
Example
int add(int a, int b);
float add(float a, float b);
The same function name add() works with different parameter types.
18.2 Run-Time Polymorphism
Runtime polymorphism is commonly achieved using function overriding and virtual functions.
Animal
↓
Dog
↓
Cat
A common function such as sound() can behave differently
for different derived objects.
Importance of Polymorphism
- Provides flexibility
- Supports extensibility
- Reduces complex conditional logic
- Makes programs easier to maintain
19. Dynamic Binding
Binding means connecting a function call with the function implementation.
When the connection is determined during program execution, it is called dynamic binding or late binding.
It is particularly important in runtime polymorphism.
Example Concept
Animal *a;
If a refers to a particular derived object and a virtual
function is called, the appropriate overridden function can be selected
at runtime.
Static vs Dynamic Binding
| Static Binding | Dynamic Binding |
|---|---|
| Determined at compile time | Determined at runtime |
| Also called early binding | Also called late binding |
| Less flexible | More flexible |
20. Message Passing
Objects communicate with each other by sending messages or requesting operations from one another.
A message generally involves:
Object + Function/Operation + Arguments
Example
student.display();
Here, the student object is requested to perform
the display() operation.
account.deposit(5000);
The object account receives a request to perform
deposit() with the value 5000.
Message passing helps different objects work together to accomplish a task.
21. Modularity
Modularity means dividing a large software system into smaller independent and manageable components.
In OOP, classes and objects help provide modularity.
College Management System
|
+-- Student Class
|
+-- Teacher Class
|
+-- Course Class
|
+-- Result Class
Advantages of Modularity
- Easier development
- Easier testing
- Easier debugging
- Easier maintenance
- Better organization
22. Reusability
Reusability means using existing code again instead of writing the same code repeatedly.
OOP provides reusability through:
- Classes
- Objects
- Inheritance
- Function libraries
- Templates and other language features
For example, if an Employee class already provides
common employee functionality, a derived class can reuse that
functionality rather than implementing everything from scratch.
23. Advantages of Object-Oriented Programming
1. Data Security
Encapsulation and access control can restrict direct access to internal data.
2. Code Reusability
Inheritance and reusable classes reduce repeated code.
3. Easy Maintenance
Organizing software into classes makes individual components easier to modify.
4. Modularity
Large programs can be divided into smaller classes and components.
5. Flexibility
Polymorphism allows the same interface to work with different types of objects.
6. Extensibility
Existing systems can be extended by adding new classes and functionality.
7. Real-World Modeling
Objects can represent real-world entities such as:
- Student
- Teacher
- Car
- Bank Account
- Product
8. Suitable for Large Software
OOP is particularly useful for developing large and complex software systems.
24. Limitations of Object-Oriented Programming
1. More Complex for Beginners
Concepts such as classes, inheritance, polymorphism, and dynamic binding can be difficult initially.
2. More Planning Required
Large object-oriented systems often require careful design of classes and relationships.
3. More Memory Usage in Some Designs
Objects may require additional memory for their data and associated runtime mechanisms.
4. Small Programs May Become Unnecessarily Complex
For a very small task, creating several classes may be more complicated than writing a few simple functions.
5. Development Can Take More Time
Designing a good class hierarchy and object relationships may require additional time.
25. Why OOP is Important in C++
C++ supports both procedural programming and object-oriented programming.
This makes C++ a flexible programming language.
Procedural Example
int add(int a, int b)
{
return a + b;
}
Object-Oriented Example
class Student
{
// data and functions
};
Therefore, C++ can be used for both small procedural programs and large object-oriented applications.
26. Real-World Example of OOP
Consider a Banking System.
Instead of thinking only about functions such as:
deposit()
withdraw()
checkBalance()
OOP identifies objects such as:
Bank
Account
Customer
Transaction
An Account object may contain:
Account Number
Account Holder
Balance
And operations:
deposit()
withdraw()
checkBalance()
The system can then contain relationships between different objects. This makes the design easier to understand because it models the system using entities that exist in the problem domain.
27. Important OOP Terms at a Glance
| Term | Meaning |
|---|---|
| Class | Blueprint for creating objects |
| Object | Instance of a class |
| Encapsulation | Combining data and methods and controlling access |
| Abstraction | Hiding unnecessary implementation details |
| Inheritance | Acquiring properties and behavior from another class |
| Polymorphism | Ability to represent or use one interface in multiple forms |
| Dynamic Binding | Selecting the appropriate method at runtime |
| Message Passing | Communication between objects |
| Modularity | Dividing software into manageable components |
| Reusability | Using existing code or components again |
28. Exam-Oriented Questions
Short Questions
- What is structured programming?
- What is OOP?
- What is a class?
- What is an object?
- What is encapsulation?
- What is abstraction?
- What is inheritance?
- What is polymorphism?
- What is dynamic binding?
- What is message passing?
- What is modularity?
- What is code reusability?
Long Questions
- Explain the structured programming approach with its features, advantages, and limitations.
- Explain the object-oriented programming approach with suitable examples.
- What is OOP? Explain its major characteristics.
- Differentiate between structured programming and object-oriented programming.
- Explain classes and objects with suitable C++ examples.
- Explain encapsulation and abstraction. Differentiate between them.
- Explain inheritance and its importance in OOP.
- What is polymorphism? Explain compile-time and runtime polymorphism.
- Explain dynamic binding and message passing.
- Explain the major characteristics of object-oriented languages.
- Discuss the advantages and limitations of object-oriented programming.
- Explain how OOP can be used to model real-world problems.
29. Quick Revision
Remember the major characteristics of OOP as:
C O A I P D M M R
- C → Classes
- O → Objects
- A → Abstraction
- I → Inheritance
- P → Polymorphism
- D → Dynamic Binding
- M → Message Passing
- M → Modularity
- R → Reusability
Four Major OOP Concepts
The four most commonly emphasized OOP concepts are:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
30. Unit 1 Summary
Structured programming organizes programs primarily around functions and procedures, while object-oriented programming organizes programs around classes and objects.
In structured programming, the focus is mainly on the operations performed by a program. In OOP, data and the operations associated with that data are organized together.
The major characteristics of object-oriented programming include objects, classes, encapsulation, abstraction, inheritance, polymorphism, dynamic binding, message passing, modularity, and reusability.
OOP is especially useful for large and complex applications because it promotes better organization, code reuse, data protection, maintainability, and extensibility.
C++ is particularly important because it supports both procedural and object-oriented programming approaches.
0 Comments