0% found this document useful (0 votes)
30 views8 pages

Understanding Object-Oriented Programming

The Object-Oriented Approach (OOP) is a programming paradigm that organizes software design around objects, which encapsulate data and behavior. It contrasts with functional programming, which focuses on functions and immutability, and procedural programming, which emphasizes procedures and global data. Key OOP concepts include classes, inheritance, polymorphism, and encapsulation, which enhance code reusability and maintainability.

Uploaded by

dinglevarshney12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views8 pages

Understanding Object-Oriented Programming

The Object-Oriented Approach (OOP) is a programming paradigm that organizes software design around objects, which encapsulate data and behavior. It contrasts with functional programming, which focuses on functions and immutability, and procedural programming, which emphasizes procedures and global data. Key OOP concepts include classes, inheritance, polymorphism, and encapsulation, which enhance code reusability and maintainability.

Uploaded by

dinglevarshney12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Approach

The Object-Oriented Approach (OOP) is a programming paradigm that organizes software design around
data, or objects, rather than functions and logic. An object can be defined as a data field that has unique
attributes and behavior. Think of it as a blueprint for a real-world entity, like a car. The "Car" object would have
attributes like color, model, and year, and behaviors like accelerate, brake, and turn. �

Relating to Other Paradigms

Functional Programming

Functional programming is a paradigm where programs are constructed by applying and composing
functions. It treats computation as the evaluation of mathematical functions and avoids changing state and
mutable data. A key concept is immutability, meaning data cannot be changed after it's created.

 Relationship to OOP: While both paradigms are popular, they approach problem-solving differently.
OOP focuses on bundling data and behavior into objects, while functional programming focuses on the
flow of data through a series of pure functions. Functional languages often use higher-order functions
(functions that can take other functions as arguments) to achieve complex operations, whereas OOP uses
inheritance and polymorphism.

Data Decomposition

Data decomposition, also known as procedural programming, is a traditional approach where a program is
broken down into a set of procedures (or routines or subroutines). The focus is on the steps taken to solve a
problem. Data is often global and can be accessed and modified by any procedure, which can lead to problems
with data integrity and makes it difficult to manage complex applications.

 Relationship to OOP: OOP evolved partly to overcome the limitations of data decomposition. The key
difference is that OOP encapsulates data within objects, meaning it's hidden and can only be accessed
or modified through the object's defined methods. This prevents unintended data corruption and makes
code more robust and easier to maintain. In data decomposition, the data and the procedures that act on
the data are separate.

Differentiation in Table Form

Object-Oriented Functional Data Decomposition


Feature
Approach Programming (Procedural)
Treats computation as the Focuses on a sequence of
Bundles data and methods
Core Concept evaluation of procedures or
(behavior) into objects.
mathematical functions. instructions.
Data is often global and
Encapsulates data within Immutable data; avoids
Data Handling can be accessed by any
objects; data is hidden. changing state.
procedure.
Encapsulation, Immutability, Pure
Modularity, Sequential
Key Principles Inheritance, Functions, First-class
execution.
Polymorphism. Functions.
Focuses on the "what to
Models real-world entities Focuses on the "how to
Problem-Solving do" rather than the "how
and their interactions. do it" step-by-step.
to do it."
Example Language Java, C++, Python Haskell, Lisp, Scala C, Pascal, FORTRAN
Achieved through Achieved through higher- Achieved through calling
Code Reusability inheritance and order functions and procedures or
polymorphism. composition. subroutines.

Basic Concept

Class

A class is a blueprint or a template for creating objects. It defines the properties (also known as attributes or
fields) and behaviors (methods) that an object of that class will have. Think of a class as the design for a car: it
specifies that a car will have a color, a model, and methods like drive() and brake(), but it's not a car itself.

Object

An object is a specific instance of a class. It's a real-world entity created from the class blueprint. Following the
car example, an object would be an actual car: a red Toyota Camry that you can actually drive and brake. Each
object has its own unique state (the values of its properties). �

Abstraction

Abstraction is the process of hiding complex implementation details and showing only the essential features of
an object. It focuses on what an object does rather than how it does it. For example, when you press the
accelerator pedal in a car, you don't need to know the intricate mechanics of how the engine works; you just
need to know that pressing it makes the car go faster. �

Encapsulation

Encapsulation is the bundling of data (properties) and methods that operate on that data into a single unit (the
class). It also involves restricting direct access to some of an object's components, which prevents data from
being accidentally modified. This is often achieved through access modifiers like private or public. It's like a
capsule where the medication is enclosed and protected. �

Inheritance

Inheritance is a mechanism where a new class (the subclass or child class) inherits properties and methods
from an existing class (the superclass or parent class). This promotes code reusability and establishes a "is-a"
relationship. For instance, a SportsCar class could inherit from a Car class, gaining all its basic properties and
methods while also adding its own unique features, like a spoiler or a turboBoost() method. ��
Polymorphism

Polymorphism literally means "many forms." In OOP, it allows an object to take on many forms. This means
that a single interface can be used for different underlying data types. A common example is method
overriding, where a subclass provides a specific implementation of a method that is already defined in its
superclass. For example, a Dog class and a Cat class could both inherit from an Animal class and have a
makeSound() method. However, when you call makeSound() on a Dog object, it would bark, and when you call
it on a Cat object, it would meow. ��

C and C++ are both powerful, statically-typed programming languages that share a common heritage. C++ was
developed as an extension of C, so it includes almost all of C's features and syntax. The key difference,
however, lies in their programming paradigms.

C is a procedural language, while C++ is a multi-paradigm language that primarily supports Object-
Oriented Programming (OOP).

Here's a breakdown of the main differences:

Programming Paradigm

 C: It's a procedural language that focuses on a sequence of steps or procedures (functions) to solve a
problem. It uses a top-down approach, breaking a program into smaller functions.
 C++: It is a hybrid language that supports both procedural and object-oriented programming. Its core
focus on OOP allows it to use a bottom-up approach, building a program with classes and objects.

Object-Oriented Programming (OOP)

This is the most significant difference. C++ was created to add OOP concepts to C.

 C: It does not support OOP concepts like classes, objects, inheritance, polymorphism, and
encapsulation. It's not possible to define classes or use these features to structure code.
 C++: It fully supports OOP. You can define classes to create objects, use inheritance to reuse code, and
implement polymorphism for flexible behavior. This is crucial for managing large and complex
applications.

Memory Management

Both languages require manual memory management, but they use different methods.

 C: Uses library functions like malloc() and calloc() for dynamic memory allocation and free() for
deallocation.
 C++: Uses the new operator for allocation and the delete operator for deallocation. It also has features
like smart pointers to help manage memory more safely.
Input/Output

 C: Uses standard library functions like printf() and scanf() for console I/O.
 C++: Uses a more flexible and type-safe I/O stream library with cout and cin.

Data Handling and Security

 C: Data is a "free entity," meaning it can be accessed and modified by any function. There is no concept
of data hiding, making it less secure.
 C++: Uses encapsulation to bundle data with the functions that operate on it, and access modifiers
(private, public, protected) to control visibility and protect data from outside manipulation. This
makes programs more secure and easier to maintain.

Feature C C++
Programming Paradigm Procedural Multi-paradigm (procedural & object-oriented)
Object-Oriented
Not supported Supported (classes, objects, inheritance, etc.)
Programming (OOP)
Uses malloc(), calloc(), and
Memory Management Uses new and delete operators
free() functions
Less secure; data is not hidden or More secure; uses encapsulation and access
Data Security
protected modifiers (private, public, etc.)
Input/Output Uses functions like printf() and Uses a type-safe stream library (cout and cin)
scanf()
File Extension .c .cpp, .cc, or .cxx
Keywords Has 32 keywords. Has 63 keywords, including those for OOP.
Supports built-in data types and Supports all of C's data types plus user-defined
Data Types
struct. data types like classes.
Not supported. You can't have
Supported. You can have multiple functions
Function Overloading two functions with the same
with the same name but different parameters.
name.
Lacks built-in exception handling. Has built-in exception handling using try-
Exception Handling
It relies on return codes. catch blocks.
C code can generally be compiled C++ code, due to its additional features,
Compatibility
and run by a C++ compiler. cannot be compiled by a C compiler.

Cin and Cout

cin and cout are objects in C++ used for input and output operations, respectively. They are part of the
<iostream> standard library.

 cout (Console Output) is an object of the ostream class. It's used to display information on the standard
output device, which is typically the console screen. The << operator, known as the insertion operator,
is used with cout to send data to the output stream.
C++

#include <iostream>

int main() {
int number = 10;
std::cout << "The number is: " << number;
// Output: The number is: 10
return 0;
}

 cin (Console Input) is an object of the istream class. It's used to read data from the standard input
device, which is typically the keyboard. The >> operator, known as the extraction operator, is used
with cin to extract data from the input stream and store it in a variable.

C++

#include <iostream>

int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
// User enters a number, which is stored in the 'number' variable
return 0;
}

new and delete

The new and delete operators are used for dynamic memory management in C++. They allow you to allocate
and deallocate memory on the heap at runtime.

 newis an operator used to allocate memory for an object or an array of objects. It returns a pointer to the
newly allocated memory. If memory allocation fails, it throws a std::bad_alloc exception.

C++

#include <iostream>

int main() {
int* ptr = new int; // Dynamically allocates memory for a single
integer
*ptr = 5;
std::cout << "Value at allocated memory: " << *ptr << std::endl;
// ...
return 0;
}
 delete is an operator used to deallocate memory that was previously allocated with new. It's crucial to
call delete for every new call to prevent memory leaks, where memory is not released and becomes
unavailable for use. When deleting an array, you must use the [] syntax.

C++

#include <iostream>

int main() {
int* ptr = new int;
// ... some code
delete ptr; // Deallocates the memory pointed to by ptr

int* arr = new int[5]; // Dynamically allocates memory for an


array
// ... some code
delete[] arr; // Deallocates the array memory
return 0;
}

Operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical manipulation.
They are essential for creating expressions and manipulating data in C++.

Types of Operators

Operator Operators Description Example


Type
Arithmetic +, -, *, /, % Used for mathematical calculations. int x = 5 + 3;
Relational ==, !=, >, <, >=, Compares two operands and returns a Boolean bool result = (10 >
<= result. 5);
Logical &&, ` , !`
Assignment =, +=, -=, *=, /=, Assigns a value to a variable. x = 10;, x += 5;
%=
Bitwise &, |, ^, ~, <<, >> Work on individual bits of an operand. int y = x << 2;

1. Arithmetic Operators

These operators are used for basic mathematical operations.

 + (Addition): Adds two operands.


 - (Subtraction): Subtracts the second operand from the first.
 * (Multiplication): Multiplies two operands.
 / (Division): Divides the first operand by the second.
 % (Modulus): Returns the remainder of an integer division.
Example:

C++
int a = 10, b = 3;
int sum = a + b; // sum is 13
int difference = a - b; // difference is 7
int product = a * b; // product is 30
int quotient = a / b; // quotient is 3 (integer division)
int remainder = a % b; // remainder is 1

2. Relational Operators

These operators compare two values and evaluate to a bool (true or false).

 == (Equal to): Checks if two operands are equal.


 != (Not equal to): Checks if two operands are not equal.
 > (Greater than): Checks if the first operand is greater than the second.
 < (Less than): Checks if the first operand is less than the second.
 >= (Greater than or equal to)
 <= (Less than or equal to)

Example:

C++
int x = 5, y = 8;
bool is_equal = (x == y); // is_equal is false
bool is_not_equal = (x != y); // is_not_equal is true
bool is_greater = (y > x); // is_greater is true

3. Logical Operators

These are used to combine or modify Boolean expressions.

 && (Logical AND): Returns true if both operands are true.


 || (Logical OR): Returns true if at least one operand is true.
 ! (Logical NOT): Reverses the logical state of an operand.

Example:

C++
bool is_raining = true;
bool has_umbrella = false;
bool can_go_out = !is_raining || has_umbrella; // can_go_out is false (!true is false,
false || false is false)

4. Assignment Operators

These operators assign a value to a variable.

 = (Simple assignment): Assigns the value of the right operand to the left.
 += (Add and assign): Adds the right operand to the left and assigns the result to the left. x += 5; is the
same as x = x + 5;.
 -= (Subtract and assign): x -= 3; is the same as x = x - 3;.
 *= (Multiply and assign)
 /= (Divide and assign)
 %= (Modulus and assign)

Example:

C++
int num = 10;
num += 5; // num is now 15
num *= 2; // num is now 30

5. Other Important Operators

 sizeof:Returns the size in bytes of a variable or data type.


 Ternary Operator (? :): A shorthand for an if-else statement. condition ? value_if_true :
value_if_false;
 & (Address-of): Returns the memory address of a variable.
 * (Dereference): Accesses the value at the memory address stored in a pointer.
 :: (Scope Resolution): Used to access global variables or members of a class.
 new and delete: Used for dynamic memory allocation and deallocation.
 << (Insertion) and >> (Extraction): Used with cout and cin for I/O.

You might also like