Comprehensive C++ Programming and Data Structures Reference

Classified in Computers

Written on in English with a size of 8.72 KB

C++ Fundamentals

Let's learn about basic C++

C++ Data Types - GeeksforGeeks

Source: GeeksforGeeks

Loops in C++

For Loop

#include <iostream>
using namespace std;
int main() {
    for(int i = 1; i <= 5; i++) {
        cout << "Good morning \n";
    }
    return 0;
}

Loops in C++ 1

While Loop

#include <iostream>
using namespace std;
int main() {
    int i = 0; // Initialization
    while(i < 5) { // Test expression
        cout << "Good morning\n";
        i++; // Update expression
    }
    return 0;
}

Loops in C++ 1

Do-While Loop

#include <iostream>
using namespace std;
int main() {
    int i = 2; // Initialization
    do {
        cout << "Good morning\n";
        i++; // Update expression
    } while(i < 1); // Test expression
    return 0;
}

Output 2

C++ Interview Questions

1. Data Types in C++

The 4 data types in C++ are:

  • Primitive: char, short, int, float, long, double, bool, etc.
  • Derived: array, pointer, etc.
  • Enumeration: enum
  • User-defined: structure, class, etc.

2. Difference Between C and C++

CC++
Procedure-oriented.Object-oriented.
No data hiding.Encapsulation for data hiding.
Subset of C++.Superset of C.
No overloading.Supports function/operator overloading.
No namespaces.Uses namespaces.
Functions cannot be in structs.Functions can be in structs.
malloc()/free().new/delete operators.

3. Class and Object

A class is a user-defined data type containing data members and member functions. An object is an instance of a class.

class A {
private:
    int data;
public:
    void fun() {}
};

4. Struct vs. Class

StructureClass
Members are public by default.Members are private by default.
Default inheritance is public.Default inheritance is private.

5. Operator Overloading

Allows redefining operators (+, -, *, etc.) for user-defined types.

6. Polymorphism

Means "many forms." Types: Compile-time (overloading) and Runtime (virtual functions).

Polymorphism_in_C__.png?1614777415

7. Constructors

Member functions executed automatically upon object creation. Same name as the class, no return type.

8. Virtual Functions

Member functions in a base class redefined in a derived class, enabling runtime polymorphism.

9. Compile-time vs. Runtime Polymorphism

Compile-timeRuntime
Resolved by compiler.Resolved at runtime.
Faster execution.Slower execution.
Overloading.Virtual functions.

10. Friend Class and Function

Can access private and protected members of a class without being a member function.

11. Access Specifiers

  • Public: Accessible everywhere.
  • Protected: Accessible in class and derived classes.
  • Private: Accessible only within the class.

12. Inline Functions

Compiler replaces the function call with the function body to reduce overhead.

13. References

An alias for an existing variable.

14. Abstraction

Hiding complex implementation details and showing only essential features.

15. Destructor Overloading

Not possible, as destructors take no arguments.

16. Call by Value vs. Reference

Value passes a copy; Reference passes the address.

17. Abstract Class

A class with at least one pure virtual function; cannot be instantiated.

18. Destructors

Called automatically when an object is destroyed. Same name as class, preceded by a tilde (~).

19. Static Members

Shared across all instances of a class; only one copy exists.

20. Inheritance

Creating new classes from existing ones to enable code reusability.

Inheritance_in_C__.png?1614780459

Experienced Interview Questions

21. Copy Constructor

Initializes an object using another object of the same class.

22. Shallow vs. Deep Copy

Shallow copies references; Deep copies the actual data.

23. Virtual vs. Pure Virtual Functions

Pure virtual functions (= 0) have no implementation in the base class.

24. Constructor/Destructor Order

Constructors: Base to Derived. Destructors: Derived to Base.

25. Virtual Functions in Constructors

Possible, but they do not exhibit polymorphic behavior within the constructor.

26. Void Pointers

Generic pointers that can hold addresses of any data type.

27. This Pointer

A pointer to the current object instance.

28. Memory Management

Use new for allocation and delete for deallocation.

Data Structures and Algorithms

I started learning data structures and I am in arrays topic so I can solve  easy level and less medium level problems on arrays. Is it okay if I move  on to ComplexityChartDataStructureSelection

1.2 Vector

Use for simple storage and quick index lookups.

1.3 Deque

Double-ended queue; efficient front/back operations.

1.4 List

Efficient insertion/deletion in the middle.

1.5 Map

Key-value pairs; std::map (ordered) vs std::unordered_map (hash table).

1.6 Set

Stores unique elements; ordered.

1.7 Stack

LIFO (Last-In, First-Out).

1.8 Queue

FIFO (First-In, First-Out).

1.9 Priority Queue

Elements ordered by priority.

1.10 Heap

Min/Max heap implementation using priority queues.

Trees and Searching

2.1 Binary Tree

At most two children per node.

2.2 Balanced Trees

AVL or Red-Black trees to maintain O(log n) operations.

2.3 Binary Search

O(log n) search on sorted data.

2.4 Depth-First Search (DFS)

Recursively explore branches.

DepthFirstSearch

2.5 Breadth-First Search (BFS)

Explore level by level.

DepthFirstSearch

Algorithms

4.1 Insertion Sort

Simple, efficient for small datasets.

4.2 Selection Sort

In-place sorting.

SelectionSort

4.3 Bubble Sort

Inefficient for large datasets.

4.4 Merge Sort

Divide and conquer; O(n log n).

4.5 Quicksort

Fast, divide and conquer; O(n log n) average.

General Interview Tips

Prepare for behavioral questions like "Tell me about yourself," "Why this company?," and "How do you handle pressure?" Always have questions ready for the interviewer.

deque_crop.png

Related entries: