� C++ Midterm Revision Cheatsheet
1. Functions
Reusable block of code.
Syntax:
return_type function_name(parameters) {
// body
}
Example:
int add(int a, int b) { return a + b; }
2. Pointers
Stores the address of a variable.
Syntax:
int a = 10;
int* ptr = &a;
cout << *ptr; // prints 10
3. Arrays
Collection of elements of the same type.
Syntax:
int arr[3] = {1, 2, 3};
4. Concept of Memory Allocation
Static: Memory is allocated at compile time.
Dynamic: Memory allocated at runtime using new/delete.
5. New Operator
Allocates memory dynamically.
Syntax:
int* ptr = new int;
*ptr = 5;
delete ptr;
6. Classes
Blueprint for objects.
Syntax:
class ClassName {
// members
};
7. Objects
Instance of a class.
ClassName obj;
8. Access Specifiers
public: accessible anywhere.
private: accessible only inside the class.
protected: accessible in derived classes.
9. Member Functions
Functions defined inside a class to access or modify data members.
10. Constructor
Special function that runs when an object is created.
class A {
public:
A() { cout << "Constructor"; }
};
11. Parameterized Constructor
Constructor with parameters.
A(int x) { value = x; }
12. Constructor Overloading
Multiple constructors with different parameters.
A() {}
A(int x) {}
A(int x, int y) {}
13. Destructor
Cleans up memory.
~A() { cout << "Destructor"; }
14. Abstraction
Hiding internal details and showing only essential info.
Achieved using classes & access specifiers.
15. Encapsulation
Wrapping data and methods into a single unit (class).
Achieved using private data and public methods.
16. Static Variables
Shared among all objects.
Initialized once.
static int count;
17. Constant Data Member
Value cannot change once assigned.
const int max = 100;
18. Constant Member Function
Does not modify class members.
void show() const;
19. Constant Static Variable
Must be initialized outside the class.
static const int max;
const int ClassName::max = 100;
20. Friend Function
Can access private/protected members.
friend void display(ClassName obj);
21. Operator Overloading
Redefine operators.
ClassName operator+(ClassName obj);
22. Inheritance
One class inherits another.
class A {};
class B : public A {};
✅ Tips:
Always match constructor names with class names.
Use this-> to refer to the current object inside class.
Constructors can’t return values.
Destructors have ~ and no parameters.
✅ Pointer to Object
You can create a pointer that points to an object.
class A {
public:
void show() { cout << "Hello"; }
};
int main() {
A obj;
A* ptr = &obj;
ptr->show(); // Use -> for member access through pointer
}
✅ Dynamic Object Creation
Create object dynamically using new
A* ptr = new A();
ptr->show();
delete ptr; // Always free memory
✅ Array of Objects with Pointers
A* arr = new A[3]; // Create array of 3 objects
delete[] arr; // Use delete[] to free object arrays
✅ this Pointer
Refers to the current object inside class
class A {
int x;
public:
void set(int x) {
this->x = x; // disambiguates local and member variable
}
};
✅ Shallow Copy vs Deep Copy
Shallow Copy: Just copies address
Deep Copy: Duplicates data in new memory
class A {
int* ptr;
public:
A(int val) { ptr = new int(val); }
// Deep copy constructor
A(const A& other) {
ptr = new int(*other.ptr);
}
~A() { delete ptr; }
};
✅ Passing Object to Function
By value (creates a copy):
void show(A obj) { ... }
By reference (no copy):
void show(A& obj) { ... }
By pointer:
void show(A* obj) { obj->display(); }
✅ Returning Object from Function
A createObj() {
A temp;
return temp;
}
✅ Operator Overloading (Example: +)
class A {
int val;
public:
A(int x) { val = x; }
A operator+(A obj) {
return A(val + obj.val);
}
};
✅ Static Functions
Can only access static members.
class A {
public:
static int count;
static void showCount() {
cout << count;
}
};
✅ Constant Object
Can only call const functions.
class A {
public:
void show() const { cout << "safe"; }
};
const A obj;
obj.show(); // OK
✅ Inheritance Syntax + Access
class Base {
protected:
int x;
};
class Derived : public Base {
public:
void print() { cout << x; } // OK due to protected
};
✅ Function Overriding
Same function in base and derived classes.
Use virtual for polymorphism (coming in future chapters).
class Base {
public:
void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() { cout << "Derived"; }
};
✅ Constructor and Destructor in Inheritance
Base constructor runs before derived.
Destructor runs in reverse order.
class A {
public:
A() { cout << "A "; }
~A() { cout << "~A "; }
};
class B : public A {
public:
B() { cout << "B "; }
~B() { cout << "~B "; }
};