POP: A paradigm focused on procedures or functions operating on data (e.g., void display() { cout << "Hello"; }).
OOP: A paradigm based on objects that contain data and methods, focusing on classes and objects (e.g., class Car { public: int speed; void accelerate() { speed += 10; } };).
POP vs OOP: POP focuses on functions, whereas OOP focuses on objects encapsulating data and behavior.
Basic OOP Concepts: Encapsulation, Abstraction, Inheritance, Polymorphism.
Class: A blueprint for creating objects that define attributes and methods (e.g., class Car { public: int speed; void accelerate() { speed += 10; } };).
Object: An instance of a class, with data and methods defined in the class (e.g., Car myCar; myCar.accelerate();).
Data Members and Member Functions: Data members store the state, and member functions manipulate/access the data (e.g., int speed; void accelerate() { speed++; }).
Abstraction: Hiding complex implementation and exposing essential features (e.g., void startEngine();).
Encapsulation: Bundling data and methods together and restricting access to some components (e.g., private members).
Tokens: Smallest units in C++, including keywords, identifiers, constants, and variables.
Keyword: A reserved word with a special meaning (e.g., int, return).
Identifier: A name for variables, functions, etc. (e.g., int number;).
Constant: A variable whose value cannot be changed (e.g., const int MAX = 100;).
Conditional Statements allow decision-making in programs based on specific conditions, and their types include:
if statement: Executes a block of code if the condition is true.
Syntax: if (condition) { // code to be executed; }
if-else statement: Executes one block of code if the condition is true and another block if false.
Syntax: if (condition) { // code if true; } else { // code if false; }
else if statement: Used to check multiple conditions sequentially.
Syntax: if (condition1) { // code; } else if (condition2) { // code; } else { // code; }
switch statement: Selects one of many code blocks to be executed based on the value of a variable.
Syntax: switch (variable) { case value1: // code; break; case value2: // code; break; default: // code; }
Data Types in C++ define the type of data a variable can store, and they are classified into the following types:
Primitive Data Types: Basic built-in types like int (integer), float (floating-point number), char (character), double (double-precision floating-point), and bool (boolean).
Derived Data Types: Types derived from basic data types, including arrays, pointers, functions, and references.
User-defined Data Types: Data types defined by the user using struct, union, enum, and class to create custom data structures.
Operators in C++ are symbols used to perform operations on variables and values, and they are classified into the following types:
Arithmetic Operators: Perform basic arithmetic operations (+, -, *, /, %).
Relational Operators: Compare values and return a boolean (==, !=, <, >, <=, >=).
Logical Operators: Perform logical operations on boolean values (&&, ||, !).
Assignment Operators: Assign values to variables (=, +=, -=, *=, /=, %=).
Bitwise Operators: Perform bit-level operations (&, |, ^, ~, <<, >>).
Increment/Decrement Operators: Increase or decrease a variable’s value by one (++, --).
Conditional (Ternary) Operator: Evaluates an expression based on a condition (condition ? expr1 : expr2).
Pointer Operators: Work with pointers (*, &).
Type-casting Operators: Convert a variable from one type to another ((type), static_cast, dynamic_cast, const_cast, reinterpret_cast).
Comma Operator: Separates multiple expressions to be evaluated in sequence (expr1, expr2).
Looping Statements allow repeated execution of a block of code while a condition is true, and their types include:
for loop: Repeats a block of code for a specified number of iterations.
Syntax: for (initialization; condition; increment) { // code to be executed; }
while loop: Repeats a block of code as long as the condition is true.
Syntax: while (condition) { // code to be executed; }
do-while loop: Executes a block of code once, then repeats as long as the condition is true.
Syntax: do { // code to be executed; } while (condition);
Jumping Statements: Transfer control to another part of the program (e.g., break;).
While vs Do-While: while checks condition before execution; do-while executes at least once before checking the condition.
Specifying a Class: A class in C++ is a blueprint for objects, defining their properties and behaviors (data members and member functions).
Defining Member Functions: Member functions are functions defined inside a class to operate on the class's data members.
C++ Program with Class: A C++ program can include classes to encapsulate data and functions, making code modular and reusable.
Private Member Functions: Functions that are accessible only within the class they are defined in and cannot be accessed directly from outside the class.
Arrays within Class: Arrays can be used as data members in a class to store multiple values of the same type.
Static Data Members: Static data members are shared by all instances of a class and have a single copy for the entire class.
Static Member Functions: Static member functions can access static data members of the class but cannot access non-static members.
Arrays of Objects: An array can store multiple instances of a class, allowing the creation of multiple objects in a single data structure.
Functions in C++: Functions in C++ allow code reuse and organization, where each function performs a specific task.
Main Function: The entry point of every C++ program, typically int main().
Function Prototyping: The declaration of a function before its definition to inform the compiler about its return type and parameters.
Call by Reference: A method of passing arguments to a function by referencing the actual variable, allowing modification of the original variable.
Scope Resolution Operator: Used to define the scope of a function or variable, typically :: for accessing global variables or functions.
Inline Functions: Functions that are expanded in line at the place of the call to reduce overhead of function calls.
Default Arguments: Arguments that are passed to a function if no argument is provided during the function call.
Function Overloading: The ability to define multiple functions with the same name but different parameters.
Friend Function: A function that is not a member of a class but has access to its private and protected members.
Constructors and Destructors: Special member functions used to initialize(allocate) and clean up(deallocate) space for the objects, respectively.
Constructors: Functions called when an object is created, used to initialize the object's properties.
Parameterized Constructors: Constructors that take parameters to initialize objects with specific values.
Multiple Constructors in a Class: The ability to have more than one constructor with different parameter lists for flexibility in object creation.
Copy Constructors: A constructor that creates a new object as a copy of an existing object.
Destructors: Functions that are called when an object is destroyed, used to release resources allocated by the object.
Inheritance: A mechanism in C++ that allows a class to inherit properties and behaviors (methods) from another class.
Introduction to Inheritance: A way to extend the functionality of existing classes.
Single Inheritance: A class inherits from one base class.
Syntax:
class BaseClass {
public:
// Base class members
};
class DerivedClass : public BaseClass {
public:
// Derived class members
};
Multi-Level Inheritance: A class inherits from a derived class, forming a chain of inheritance.
Syntax:
class Base {
public:
// Base class members
};
class Intermediate : public Base {
public:
// Intermediate class members
};
class Derived : public Intermediate {
public:
// Derived class members
};
Multiple Inheritance: A class inherits from more than one base class.
Syntax:
class Base1 {
public:
// Base1 class members
};
class Base2 {
public:
// Base2 class members
};
class Derived : public Base1, public Base2 {
public:
// Derived class members
};
Hierarchical Inheritance: Multiple classes inherit from a single base class.
Syntax:
class Base {
public:
// Base class members
};
class Derived1 : public Base {
public:
// Derived1 class members
};
class Derived2 : public Base {
public:
// Derived2 class members
};
Hybrid Inheritance: A combination of two or more types of inheritance.
Syntax:
class Base {
public:
// Base class members
};
class Derived1 : public Base {
public:
// Derived1 class members
};
class Derived2 {
public:
// Derived2 class members
};
class FinalDerived : public Derived1, public Derived2 {
public:
// FinalDerived class members
};
Operator Overloading: The process of defining custom behavior for operators for user-defined types.
Overloading Unary Operators: Overloading operators that operate on a single operand (e.g., ++, --).
Overloading Binary Operators: Overloading operators that operate on two operands (e.g., +, -, *).
Pointers: Variables that store the memory address of another variable.
Declaring and Initializing Pointers: Declaring pointers and initializing them with memory addresses (e.g., int* ptr = &var;).
'this' Pointer: A special pointer in C++ that points to the current object of the class.
Polymorphism: The ability to treat objects of different classes as objects of a common base class, enabling flexibility.
Compile-Time Polymorphism: Achieved through function or operator overloading, where the method is determined at compile time.
Syntax: void display(int x); void display(double x);
Run-Time Polymorphism: Achieved using virtual functions, where the method is determined at runtime.
Syntax: virtual void display() { cout << "Base"; }
Difference Between Run-Time and Compile-Time Polymorphism: Compile-time is resolved during compilation (overloading), while runtime is resolved at execution (virtual functions).
Virtual Functions: A base class function that can be overridden in a derived class to ensure dynamic method dispatch.
Syntax: virtual void display() { cout << "Base"; }
Templates in C++: A way to create generic classes or functions that can work with any data type.
Function Template: template <typename T> void func(T x) { // code }
Class Template: template <typename T> class Box { T value; }
Exception Handling: A mechanism to handle errors using try, throw, and catch blocks.
Syntax:
try { // code }
catch (exception e) { // handle }
Types of Errors:
Compile-Time Error: Occurs during compilation due to syntax issues. E.g. syntax errors and logical errors
Run-Time Error: Occurs during execution, like invalid memory access. it is also called as exception. E.g. number divided by zero etc.