0% found this document useful (0 votes)
21 views7 pages

Oop CPP Notes

Uploaded by

alyackerman6
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)
21 views7 pages

Oop CPP Notes

Uploaded by

alyackerman6
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
You are on page 1/ 7

Detailed Notes on Object-Oriented Programming (OOP) and C++

1. Procedural-Oriented Programming (POP) vs. Object-Oriented Programming (OOP)

Procedural-Oriented Programming (POP):

Definition: A programming paradigm based on calling procedures or functions in sequence, with a

linear step-by-step approach to problem-solving.

Key Characteristics:

- Top-Down Approach: Large programs are divided into smaller sub-programs.

- Functions: Procedures manipulate data in a linear fashion.

- Data Flow: Data passes openly between functions, causing a lack of data security.

- Global Data Sharing: Functions can modify global data, leading to potential inconsistencies.

Disadvantages:

- Data Insecurity: Data is exposed globally without security controls.

- Difficult Real-World Modeling: Unlike OOP, POP doesn't naturally represent real-world entities.

- Operation Over Data: Emphasizes actions over securing data.

Object-Oriented Programming (OOP):

Definition: OOP focuses on 'objects'-real-world entities combining both data and functions.

Key Characteristics:

- Objects: Instances of classes representing real-world entities.

- Data Hiding: Data within objects is protected, only accessible through specific methods.

- Bottom-Up Approach: Focus on objects, not functions, to build a system.

- Focus on Data: Emphasizes securing data and operating on it via methods.

Advantages:

- Data Security: Data is secured within objects, hidden from outside functions.
- Modularity: Objects form independent, modular units, simplifying maintenance.

- Real-World Modeling: OOP closely mimics real-world interactions.

- Reusability: Code is reused via inheritance and polymorphism.

2. Key Concepts and Features of OOP

Object:

Definition: An instance of a class representing a real-world entity.

Example: If "Car" is a class, 'myCar' can be an object of this class.

Class:

Definition: A blueprint that defines objects, containing attributes (data) and behaviors (functions).

Example:

class Car {

private:

int speed;

public:

void setSpeed(int s) { speed = s; }

Encapsulation:

Definition: Wrapping data (attributes) and methods (functions) within a class for controlled access.

Data Hiding: Prevents unauthorized access by hiding internal implementation.

Advantages:

- Simplifies maintenance by isolating internal changes.

Example:
class Employee {

private:

int salary;

public:

void setSalary(int s) { salary = s; }

int getSalary() { return salary; }

Abstraction:

Definition: Hiding complex details and exposing only necessary functionality.

Achieved by abstract classes or interfaces.

Advantages: Focuses on what the object does, not how.

Example: A car's interface might include 'start()', 'stop()' without exposing the mechanics.

Inheritance:

Definition: New classes (child) inherit properties and methods from existing classes (parent).

Types of Inheritance:

1. Single Inheritance

2. Multiple Inheritance

3. Multilevel Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

Advantages:

- Code Reusability: Reuse existing code in new classes.

- Maintainability: Modifications in the parent class reflect in child classes.

Example:
class Animal {

public:

void eat() { cout << "Eating"; }

class Dog : public Animal {

public:

void bark() { cout << "Barking"; }

Polymorphism:

Definition: One function or method behaving differently based on context.

Types:

1. Compile-Time (Static) Polymorphism: Function overloading or operator overloading.

2. Run-Time (Dynamic) Polymorphism: Function overriding via virtual functions.

Example:

class Animal {

public:

virtual void sound() { cout << "Some sound"; }

class Dog : public Animal {

public:

void sound() override { cout << "Barking"; }

3. Constructors and Destructors in C++


Constructor:

Definition: Special member function to initialize an object when it is created.

Types:

1. Default Constructor: No parameters.

2. Parameterized Constructor: Takes arguments to initialize objects.

3. Copy Constructor: Initializes a new object using an existing object.

Characteristics:

- No return type.

- Automatically called when an object is created.

Example:

class Box {

private:

int length;

public:

Box() { length = 0; }

Box(int l) { length = l; }

Box(const Box &b) { length = b.length; }

Destructor:

Definition: Special member function called when an object goes out of scope or is destroyed,

cleaning up resources.

Example:

class Box {

public:

~Box() { cout << "Destructor called"; }


}

4. Static Members in C++

Static Data Members:

Definition: Shared variables among all objects of the class. Only one copy exists.

Initialization: Initialized outside the class.

Example:

class Counter {

public:

static int count;

int Counter::count = 0;

Static Member Functions:

Definition: Functions that can only access static data members and are called using the class name.

Example:

class Counter {

public:

static void increment() { count++; }

5. C++ Functions: Inline, Overloaded, Default Arguments

Inline Functions:

Definition: Functions expanded in-place where called, reducing function call overhead.
Example:

inline int add(int a, int b) {

return a + b;

Function Overloading:

Definition: Multiple functions with the same name but different parameter types or numbers.

Example:

int sum(int a, int b) { return a + b; }

float sum(float a, float b) { return a + b; }

Default Arguments:

Definition: Functions taking default values if no argument is passed.

Example:

int sum(int a, int b = 0) { return a + b; }

You might also like