oop_cpp_god
oop_cpp_god
OBJECT ORIENTED
Class:
A blueprint for creating
objects that encapsulates data
(attributes) and functions
(methods) to operate on the
data. The class does not
consume memory at runtime.
Object:
An object is a concrete instance of a class. While a class defines the
structure and behavior
(data and methods) that
objects created from it
1
Object Oriented Programming Cheat Sheet
Constructor:
A constructor is a special member function in a class used to initialize
objects of that class. It is called automatically when an object is
created and is responsible for setting up the initial state of the
object.
Default constructor:
Parameterized constructor:
Copy constructor:
2
Object Oriented Programming Cheat Sheet
Access modifiers:
Access modifiers in Object-Oriented Programming (OOP) are keywords
used to specify the accessibility or visibility of class members
(attributes and methods) from
outside the class.
• Protected: Used to
allow derived classes to
access and modify
certain members while
still keeping the hidden
3
Object Oriented Programming Cheat Sheet
1. Encapsulation
2. Abstraction
3. Inheritance
4. polymorphism
Encapsulation:
Involves bundling data and methods within a class, and controlling
access to the data through access modifiers. It promotes data hiding,
enhances security, and
improves maintainability and
flexibility in code. It involves
restricting direct access to
some of the object's
components, which is intended
to prevent unintended
interference and misuse of
the object's internal state. The data and the operations that
manipulate the data are contained within the same structure, creating
a cohesive unit.
4
Object Oriented Programming Cheat Sheet
Abstraction:
Helps manage complexity by hiding the detailed implementation of
objects and exposing only the essential features that are relevant for
interaction.
5
Object Oriented Programming Cheat Sheet
Key points:
6
Object Oriented Programming Cheat Sheet
Abstract Classes and Methods: Defines abstract classes with pure virtual
functions to establish a common interface for derived classes.
Inheritance:
Inheritance in object-oriented programming (OOP) is a mechanism that
allows one class (the derived class) to acquire properties and behaviors
(methods) from another class (the base class). It promotes code reuse
and establishes a natural hierarchy between classes.
Key points:
Code reusability: allows you to reuse code from an existing class, reducing
redundancy.
Types of inheritance:
7
Object Oriented Programming Cheat Sheet
Access specifiers:
• Public inheritance: Public members of the base class remain public in the
derived class.
• Private inheritance: Public and protected members of the base class become
private in the derived class.
• Protected inheritance: Public members of the base class become protected in
the derived class.
Method overriding:
8
Object Oriented Programming Cheat Sheet
Polymorphism:
It allows objects of different classes to be treated as objects of a common base class,
particularly through the use of methods with the same name but different
implementations.
Types of polymorphism:
Compile-Time Polymorphism:
Function Overloading
Operator Overloading
Operator overloading allows you to define custom behavior for operators (like +, -, *, etc.)
for user-defined classes.
9
Object Oriented Programming Cheat Sheet
Run-time polymorphism:
Run-time polymorphism is achieved through inheritance and virtual functions. This allows
the program to decide which method to call at run-time based on the object’s type.
Virtual Functions
A virtual function is a member function in a base class that you can override in a derived
class. The virtual keyword is used to indicate that the function can be overridden.
10
Object Oriented Programming Cheat Sheet
Dynamic Binding: Allows functions to be called based on the actual object type,
enhancing flexibility.
Abstract Classes: Define interfaces with pure virtual functions, forcing derived classes
to implement specific behavior.
Code Reusability: Promotes writing generic code that works across different derived
classes.
11
Object Oriented Programming Cheat Sheet
Exception:
Exceptions provide a way to handle errors and other exceptional conditions that occur
during the execution of a program. They help you manage unexpected situations without
using error codes and improve the robustness and readability of your code.
Basics of exception:
Throwing exception:
Use the throw keyword to signal that an error or exceptional condition has occurred.
12
Object Oriented Programming Cheat Sheet
Use the try block to write code that might throw an exception and catch blocks to handle
specific types of exceptions.
13
Object Oriented Programming Cheat Sheet
Best practices:
Use specific exceptions: catch specific exceptions before more general ones.
Avoid using exceptions for control flow: use exceptions for error handling only,
not as a mechanism for controlling program flow.
Example:
14
Object Oriented Programming Cheat Sheet
Templates:
Templates are a powerful feature that allows you to write generic and reusable code. They
enable you to define functions and classes that can operate with any data type without
knowing the exact type in advance. Templates help you achieve type independence and
flexibility in your code.
Types of templates:
Function template:
Function templates allow you to create functions that work with any data type. You define
a template with a placeholder for the type and then use it for various types.
Class templates:
Class templates allow you to create
classes that operate with any data type.
You define a template with a
placeholder for the type and then use it
to create instances with different
types.
15
Object Oriented Programming Cheat Sheet
Template specialization:
Full specialization:
Full specialization provides a complete and specific implementation of a template for a
particular type or set of types.
16
Object Oriented Programming Cheat Sheet
Partial specialization:
Partial specialization allows you to define a template that is specialized for a subset
of template parameters while leaving the rest generic. This is useful when you want
to specialize the template based on some but not all template parameters.
Structures:
A structure (often abbreviated as struct) is a user-defined data type that groups
together variables (members) of different types under a single name. Structures provide a
17
Object Oriented Programming Cheat Sheet
way to encapsulate related data items, making it easier to manage and organize complex
data.
Syntax:
Key features:
Arrays of structures:
18
Object Oriented Programming Cheat Sheet
19
Object Oriented Programming Cheat Sheet
Enum:
An enum (short for "enumeration") in C++ is a user-defined data type comprising a set of
named integral constants. It is utilized to define variables that can take on a specific,
predefined set of values, thereby enhancing the readability and maintainability of your
code.
Scoped Enums: enum class provides better type safety and avoids name conflicts.
20
Object Oriented Programming Cheat Sheet
Union:
A union in C++ is a user-defined data type that enables you to store different data types in
the same memory location. Unlike a structure, which allocates separate memory for each
member, a union uses a single memory block that is shared among all its members.
Consequently, a union can hold only one of its non-static data members at any given time.
Memory Efficiency: Unions are more memory-efficient than structures because they
allocate space only for the largest member, sharing a single memory block among all
members.
Size of Union: The size of a union is determined by the size of its largest member. This
ensures that the union can accommodate the largest data type it holds.
Overwriting Data: When you assign a value to one member of a union, it overwrites the
data of other members. Accessing the value of an overwritten member may result in
undefined behavior.
21
Object Oriented Programming Cheat Sheet
Use Cases: Unions are valuable for working with different data types efficiently in
memory-constrained environments, such as low-level programming or handling varied data
types in protocols.
22
Object Oriented Programming Cheat Sheet
• It is a static method
• The base class is inherited by some other subclass
23