What is LLD how is it different from HLD?
LLD, or Low-Level Design, focuses on detailed planning for individual components of a system, including algorithms and data structures. HLD, or High-Level Design, provides a broader overview of the system's architecture and major components, without going into fine-grained details.
For LLD we focus on writing maintainable, extensible and modular code, following SOLID principles
What is OOPS?
OOPS, or Object-Oriented Programming, is a programming paradigm that uses objects to model real-world entities and their interactions in software. It revolves around principles like encapsulation, inheritance, and polymorphism to create modular and reusable code.
Encapsulation:
Encapsulation is the concept of bundling data (attributes or properties) and methods (functions) that operate on that data into a single unit called an object.
It hides the internal state of an object from the outside, allowing controlled access through defined interfaces.
Inheritance:
Inheritance allows a new class (subclass or derived class) to inherit properties and behaviours (attributes and methods) from an existing class (base class or superclass).
It promotes code reuse and establishes a hierarchical relationship between classes.
There are types of inheritance and modes of inheritance in C++
Types of Inheritance in C++
Single Inheritance (B is a A):
- In single inheritance, a derived class (B) inherits from a single base class (A).
Multilevel Inheritance (B is a A and C is a B):
- In multilevel inheritance, a class (B) inherits from another class (A), and then a third class (C) inherits from the intermediate class (B).
Multiple Inheritance (B is a A and C):
- In multiple inheritance, a class (B) inherits from two or more base classes, which can be either classes (A and C) or a class and an interface.
Hierarchical Inheritance:
- In hierarchical inheritance, multiple derived classes (B, C, etc.) inherit from a single base class (A).
Hybrid Inheritance:
- Hybrid inheritance is a combination of two or more types of inheritance, such as single, multiple, or hierarchical inheritances, within a single program or class hierarchy..
Modes of inheritance in C++ (this affects the access status of derived members)
- Polymorphism:
Polymorphism means "many forms", which means a single function or operator will behave differently in different contexts
There are two types of Polymorphism
Compile Time Polymorphism - early/static binding
Function overloading
Operator overloading
Run Time Polymorphism- dynamic/late binding
Function overriding using virtual functions
In the above example both dog and cat are derived in the same way, the member functions are called in the same way but results are different because of run time polymorphism
- Marking a function virtual simply signals to the compiler that the function is to be bound art run time - seeing the context of the pointer rather than just the name
- Abstraction:
- The idea is to expose only necessary things ,for example, if you have a utility that uses several other smaller internal functions to generate output- don't expose those smaller internal functions to the user.
How to structure classes together? (Relationships and UML)
Symbol | Meaning | Usage |
# (Hash Symbol) | Protected members | Prefix before member name to indicate protected access. |
+ (Plus Symbol) | Public members | Prefix before member name to indicate public access. |
- (Minus Symbol) | Private members | Prefix before member name to indicate private access. |
_ (Underline) | Static members | Underline before member name to indicate static nature. |
<<abstract>> | Abstract classes or methods | Prefix before class or method name to indicate abstraction. |
<<interface>> | Interfaces | Prefix before interface name to indicate it's an interface. |
<<enumeration>> | Enumerations | Prefix before enumeration name to indicate it's an enumeration. |
Additional Notes:
Public Access : Members accessible everywhere.
Private Access: Members only within the class.
Protected Access: Members accessible in derived classes.
A pure virtual function HAS to be implemented
A virtual function may or may not be overriden
KISS: It emphasizes simplicity in code. For example, using a basic loop to iterate through a list instead of an overly complex algorithm for a straightforward task.
YAGNI: It discourages adding unnecessary features or code that's not currently required. For instance, refraining from implementing advanced customization in software until it's requested by users.
DRY: It promotes code reuse by avoiding duplication. For example, creating a reusable function or class to handle common tasks like database connections, rather than copying the same code across the application.