C++
C++ is a general-purpose, object-oriented programming language developed
by Bjarne Stroustrup in 1983. It is an extension of the C programming
language and is used to develop system software, application software, games,
and database applications.
Features of C++ :
Object-Oriented Programming (OOP)
Simple and Easy to Learn
Platform Independent
Fast Execution
Supports Reusability
Supports Inheritance
Supports Polymorphism
Dynamic Memory Allocation
Portable Language
Applications of C++ :
Operating Systems
Game Development
Banking Applications
Database Management Systems
Embedded Systems
Graphics Applications
Web Browsers
OOP :
Object-Oriented Programming (OOP) is a programming paradigm that
organizes a program using objects and classes. It combines data and functions
into a single unit called an object, making programs reusable, secure,
modular, and easy to maintain.
[Link] concepts of Object-Oriented Programming (OOP) :
1. Object
An Object is the basic unit of Object-Oriented Programming (OOP). It is an
instance of a class. An object contains data members (properties) and member
functions (methods).
Characteristics:
[Link] data (Properties)
[Link] actions (Methods)
[Link] memory only when created
Example:
A Car is an object.
Properties (Data Members): Model,Year of Manufacture,Color
Methods (Functions): Start(),Move(),Stop()
2. Class
A Class is a blueprint or template used to create objects. It defines the data
members and member functions of an object.
Memory is not allocated when a class is created. Memory is allocated only
when an object is created.
Example
Class: Car
Objects: Alto, Swift, WagonR
All these are objects created from the Car class.
Advantages:
1. Organizes code
2. Reusable
3. Easy to maintain
3. Inheritance
Inheritance is the process of creating a new class from an existing class. The
existing class is called the Parent (Base) Class, and the new class is called the
Child (Derived) Class.
Example:
Parent Class: Car
Child Classes: Diesel Car, Petrol Car
The child classes inherit the properties and methods of the parent class and
can also have their own additional features.
Advantages:
1. Code reusability
2. Reduces code duplication
3. Easy maintenance
4. Easy to add new features
Types of Inheritance:
Single Inheritance:
A derived class inherits from one base class.
Multiple Inheritance:
A derived class inherits from more than one base class.
Multilevel Inheritance:
A class is derived from another derived class, forming a chain of inheritance.
Hierarchical Inheritance:
Multiple derived classes inherit from a single base class.
Hybrid Inheritance:
A combination of two or more types of inheritance, such as multiple and
multilevel inheritance.
4. Data Abstraction
Data Abstraction means showing only the essential information and hiding
unnecessary implementation details from the user.
Example:
When using an ATM, the user enters the PIN and amount, but the internal
processing is hidden.
5. Data Encapsulation
Encapsulation is the process of combining data and functions into a single unit
called a Class. The data is protected and can only be accessed through the
class methods.
Example:
Student Class
Data: Name, Age, Marks
Methods: setData(), getData()
6. Polymorphism
Polymorphism means "one interface, many forms." A single function or
operator can perform different tasks depending on the context.
Example:
10 + 20 = 30 (Addition of numbers)
"Hello" + "World" = "HelloWorld" (Concatenation of strings)
The same + operator performs different operations.
7. Overloading
Overloading is a type of polymorphism where multiple functions have the
same name but different parameters.
Example:
sum(int a, int b);
sum(int a, int b, int c);
sum(float a, float b);
All functions have the same name but different parameter lists.
8. Dynamic Binding
Dynamic Binding (Late Binding) means that the method to be executed is
determined at run time rather than at compile time.
Example:
If sound() is called on an Animal object, the actual method executed depends
on whether the object is a Dog or a Cat.
9. Reusability
Reusability is the ability to use existing code in different programs or
applications without rewriting it.
Advantages:
1. Saves development time
2. Reduces errors
3. Improves code quality
4. Simplifies maintenance
10. Message Passing :
Message Passing is the communication between objects through method calls.
One object sends a message to another object to request an action.
Steps:
1. Create classes.
2. Create objects.
3. Objects communicate by sending messages.
Example:
[Link]("Ravi");
Employee → Object
salary() → Message (Method)
"Ravi" → Information (Argument)
The Employee object receives the message and performs the required action.