#1 - Basic Fundamentals - DSA Series With C++ | Akash Vishwakarma
1. Syntax and Control Structures
Basic Syntax
Every C++ program starts with a main function:
int main() {
// Your code here
return 0;
}
Variables and Data Types
- Integer types: int, short, long, long long
- Floating-point types: float, double, long double
- Character types: char
- Boolean type: bool
- Void type
Control Structures
If-else Statements
if (condition) {
// code block
} else if (another_condition) {
// code block
} else {
// code block
}
Loops
C++ supports three main types of loops:
// For loop
for (int i = 0; i < n; i++) {
// code block
}
// While loop
while (condition) {
// code block
}
// Do-while loop
do {
// code block
} while (condition);
2. Functions and Recursion
Functions
Functions are blocks of reusable code that perform specific tasks:
return_type function_name(parameters) {
// function body
return value;
}
Function Overloading
C++ allows multiple functions with the same name but different parameters:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
Recursion
Recursion is when a function calls itself. Here's a classic example of calculating factorial:
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
3. Pointers and Dynamic Memory Allocation
Pointers
Pointers store memory addresses of variables:
int x = 10; int* ptr = &x; // & operator gets address of x cout << *ptr; // * operator dereferences pointer
Dynamic Memory Allocation
Use new and delete operators for dynamic memory management:
// Single element int* p = new int; *p = 10; delete p; // Array int* arr = new int[5]; // use array delete[] arr;
4. Object-Oriented Programming (OOP)
Classes and Objects
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
void displayInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
Inheritance
class Student : public Person {
private:
string studentId;
public:
Student(string n, int a, string id)
: Person(n, a), studentId(id) {}
};
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common base class:
class Shape {
public:
virtual double area() = 0; // Pure virtual function
};
class Circle : public Shape {
private:
double radius;
public:
double area() override {
return 3.14159 * radius * radius;
}
};
5. Exception Handling and File I/O
Exception Handling
try {
// code that might throw an exception
if (someErrorCondition) {
throw runtime_error("Error message");
}
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
File I/O
#include
// Writing to a file
ofstream outFile("example.txt");
outFile << "Hello, World!" << endl;
outFile.close();
// Reading from a file
ifstream inFile("example.txt");
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
Common Interview Questions
Q1: What is the difference between references and pointers in C++?
Answer: References are aliases for existing variables and must be initialized when declared. They cannot be null and cannot be reassigned. Pointers can be null, can be reassigned, and need to be dereferenced using the * operator.
Q2: Explain the concept of virtual functions.
Answer: Virtual functions enable runtime polymorphism in C++. They allow a base class pointer to call the correct derived class function based on the actual object type at runtime. They are declared using the 'virtual' keyword in the base class and can be overridden in derived classes.
Q3: What is the difference between stack and heap memory?
Answer: Stack memory is automatically managed and stores local variables and function calls. It's faster but limited in size. Heap memory is manually managed using new/delete operators, is larger but slower, and can lead to memory leaks if not properly managed.
Q4: Explain the concept of function overloading.
Answer: Function overloading allows multiple functions with the same name but different parameters (number, types, or both) to coexist. The compiler determines which function to call based on the arguments provided at compile time.
Q5: What are the access specifiers in C++ and their significance?
Answer: C++ has three access specifiers: public (accessible from anywhere), private (accessible only within the class), and protected (accessible within the class and its derived classes). They help in implementing encapsulation and data hiding.