C++ Introduction
beginnerPart of C++ Basics
Theory
C++ is a powerful, high-performance programming language developed by Bjarne Stroustrup in 1985 as an extension of the C language. It combines the efficiency and low-level control of C with high-level abstractions like classes, templates, and the Standard Template Library (STL).
C vs C++
| Feature | C | C++ | |---------|---|-----| | Paradigm | Procedural | Multi-paradigm (procedural, OOP, generic) | | Classes | No | Yes | | Inheritance | No | Yes | | Polymorphism | No | Yes | | Templates | No | Yes | | Standard Library | Limited | Rich (STL) | | String handling | Arrays of char | std::string class | | I/O | printf/scanf | std::cin/std::cout |
The key takeaway: C++ is a superset of C — almost all valid C programs are valid C++ programs. But C++ adds object-oriented programming, templates, exceptions, and a rich standard library.
Setting Up a Compiler (G++)
On Linux, install G++ via the package manager:
On macOS, install Xcode Command Line Tools:
xcode-select --installOn Windows, install MinGW or use WSL.
Basic C++ Program
Every C++ program has at least one function — main() — which is the entry point:
Compilation Process
Source Code (.cpp)
↓ Preprocessor (#include, #define)
Expanded Source
↓ Compiler (g++)
Assembly Code (.s)
↓ Assembler
Object Code (.o)
↓ Linker (links libraries)
Executable
To compile and run:
The iostream Library and using namespace
#include <iostream> provides input/output functionality:
#include <iostream>
// Option 1: Use std:: prefix
std::cout << "Hello" << std::endl;
// Option 2: Using directive
using namespace std;
cout << "Hello" << endl;
// Option 3: Using specific names (recommended)
using std::cout;
using std::endl;
cout << "Hello" << endl;cout and cin
cout (console output) uses the insertion operator <<:
int age = 25;
cout << "I am " << age << " years old." << endl;cin (console input) uses the extraction operator >>:
string name;
int age;
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;Comments
// Single-line comment
/*
Multi-line comment
spanning multiple lines
*/
/**
* Javadoc-style comment
* Used for documentation
*/Basic Syntax Rules
- Statements end with a semicolon
; - Code blocks use curly braces
{ } - C++ is case-sensitive (
myVar≠MyVar) - Identifiers can contain letters, digits, and underscores
- Reserved keywords cannot be used as identifiers (
int,class,if, etc.)
Function Overloading (Preview)
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; }
int add(int a, int b, int c) { return a + b + c; }Always compile with -Wall -Wextra to catch potential issues early. Treat compiler warnings as errors during development.
Practical Examples
Use \n instead of std::endl when you don't need to flush the output buffer. \n is faster because it doesn't force a flush.
Exercises
Personal Information Program
Write a C++ program that asks the user for their name, age, and favorite number. Print a formatted message using cout. Use appropriate data types.
Expected Output:
Hello, Alice! You are 25 years old. Your favorite number is 42.Temperature Converter
Write a program that converts Celsius to Fahrenheit and vice versa. Ask the user which conversion they want, read the temperature, and display the result. Formula: F = C * 9/5 + 32, C = (F - 32) * 5/9.
Expected Output:
Enter C or F, then the temperature. Example: F 100 → 100°C = 212°FCompile and Fix Errors
Fix the syntax errors in this C++ program so it compiles and runs correctly.
Expected Output:
Sum: 30Mini Quiz
Mini Quiz
Mini Project
Mini Project: Simple Grade Calculator
Write a program that asks the user to enter grades for multiple subjects, calculates the average, assigns a letter grade (A, B, C, D, F), and provides feedback. Use functions to organize the code.
Requirements:
Bonus Challenge
Add input validation — ensure grades are between 0 and 100. Allow the user to add extra credit points.