C++ Fundamentals: Complete Guide
OOP, STL, inheritance, templates, and file I/O with practical examples.
Why C++ matters
C++ combines low-level performance with high-level abstractions. It is widely used in game engines, trading systems, robotics, graphics, and performance-sensitive backend services.
Modern C++ emphasizes safer patterns, RAII, standard libraries, and expressive generic programming.
First C++ program
#include <iostream>
using namespace std;
int main() {
cout << "Hello, Rishtaara!" << endl;
return 0;
}Class design basics
Classes model objects with state and behavior. Use constructors for initialization and methods for operations on object state.
#include <iostream>
#include <string>
using namespace std;
class Course {
private:
string title;
int lessons;
public:
Course(string t, int l) : title(t), lessons(l) {}
void printInfo() const {
cout << title << " has " << lessons << " lessons" << endl;
}
};Encapsulation best practices
- Keep fields private by default.
- Expose behavior with public methods.
- Mark methods const when they do not modify object state.
Most-used STL containers
- vector for dynamic arrays.
- map for key-value associations.
- set for unique ordered values.
- unordered_map for fast hash-based lookup.
Working with vector and map
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
vector<int> scores = {80, 92, 88};
scores.push_back(95);
map<string, int> marks;
marks["Asha"] = 91;
marks["Rohan"] = 85;
for (int score : scores) cout << score << " ";
cout << endl << marks["Asha"] << endl;
}Pointer and reference differences
Pointers hold addresses and can be reassigned. References are aliases to existing objects and must be initialized immediately.
#include <iostream>
using namespace std;
int main() {
int value = 10;
int* ptr = &value;
int& ref = value;
*ptr = 20;
ref = 30;
cout << value << endl; // 30
}Memory safety mindset
- Prefer smart pointers over raw new/delete in modern C++.
- Use const references to avoid unnecessary copies.
- Avoid dangling pointers by understanding object lifetimes.
Runtime polymorphism
Polymorphism lets derived classes provide specialized behavior through virtual methods. Base pointers can call overridden methods at runtime.
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() const { return 0; }
virtual ~Shape() = default;
};
class Rectangle : public Shape {
double w, h;
public:
Rectangle(double w, double h) : w(w), h(h) {}
double area() const override { return w * h; }
};Design guidelines
- Use virtual destructors in polymorphic base classes.
- Favor composition when inheritance hierarchy grows too complex.
- Override methods with clear contracts and const-correctness.
Function and class templates
Templates let you write reusable code that works with many types while retaining compile-time safety and performance.
#include <iostream>
using namespace std;
template <typename T>
T maxValue(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << maxValue(4, 9) << endl;
cout << maxValue(4.2, 3.1) << endl;
}Where templates shine
- Reusable utilities across data types.
- STL containers and algorithms are template-based.
- Enables high-performance abstractions with zero-cost intent.
Reading and writing text files
fstream enables persistent storage for logs, configs, and generated reports. Always check stream state for errors.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ofstream out("notes.txt");
out << "C++ File I/O basics" << endl;
out.close();
ifstream in("notes.txt");
string line;
getline(in, line);
cout << line << endl;
}I/O reliability tips
- Check if file streams are open before reading.
- Use binary mode for non-text data.
- Avoid silent failures by validating stream status.
Project goals
- Create Product class with id, name, and quantity.
- Use vector to manage product list.
- Search, update stock, and print summaries.
- Save/load inventory from a file.
Starter project skeleton
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Product {
int id;
string name;
int quantity;
};
int main() {
vector<Product> items = {{1, "Keyboard", 20}, {2, "Mouse", 50}};
for (const auto& item : items) {
cout << item.id << " " << item.name << " " << item.quantity << endl;
}
}Key Takeaways
- C++ balances performance with expressive abstractions.
- Classes, STL, and templates are core to modern C++ development.
- Pointers and references require careful lifetime awareness.
- Polymorphism helps design extensible object models.
- File I/O and projects prepare you for practical systems work.
Frequently Asked Questions
- Is C++ hard for beginners?
- It has a steeper learning curve than Python, but a structured path through classes, STL, and memory basics makes it manageable.
- Should I use raw pointers in modern C++?
- Use raw pointers only when ownership is clear and limited. Prefer smart pointers and references for safer, more maintainable code.
- Why are templates important?
- Templates power reusable, type-safe code and are the foundation of STL containers and algorithms used in real C++ projects.