Introduction to C++
C++ is a powerful, high-level programming language developed by Bjarne Stroustrup in 1983 as an extension of the C language. It introduced object-oriented programming (OOP) concepts like classes and objects, making it suitable for large-scale software development. C++ combines the efficiency of C with advanced programming features, making it one of the most widely used languages in software, gaming, and systems development.
History and Evolution of C++
- 1979: Bjarne Stroustrup starts developing “C with Classes.”
- 1983: Officially named C++, introducing object-oriented features.
- 1985: First commercial release of C++ compiler.
- 1990: ANSI/ISO standardization begins.
- 1998: C++98 standard released.
- 2011: C++11 introduces modern features like auto, nullptr, and lambda expressions.
- 2014 & 17: C++14 and C++17 add further improvements.
- 2020: C++20 introduces concepts, ranges, and modules for modern programming.
Features of C++
- Supports procedural, object-oriented, and generic programming.
- High performance and efficiency similar to C.
- Supports classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
- Rich Standard Template Library (STL) for data structures and algorithms.
- Portable and flexible for system and application programming.
Structure of a C++ Program
#include <iostream> // Standard input-output library
using namespace std;
int main() {
cout << "Hello, C++" << endl; // Output statement
return 0; // Exit status
}
#include <iostream>→ Includes the input-output stream library.using namespace std;→ Avoids typingstd::before standard functions.cout→ Prints output on the console.endl→ Moves cursor to the next line.
Basic Syntax and Commands
- Printing output:
cout << "Welcome to C++ programming";
- Reading input:
int age;
cin >> age;
- Comments:
// Single-line comment
/* Multi-line comment */
Data Types & Variables
C++ has similar data types as C with some additions:
- Basic types: int, float, double, char, bool
- Derived types: arrays, pointers, references
- User-defined types: classes, structs, enums
Example:
int x = 10;
float price = 99.5;
char grade = 'A';
bool isActive = true;
Operators in C++
- Arithmetic:
+ - * / % - Relational:
== != < > <= >= - Logical:
&& || ! - Assignment:
= += -= *= /= - Increment/Decrement:
++ -- - Additional C++ operators:
::(scope resolution),->(pointer to member),new&delete
Example:
int a = 5, b = 3;
cout << "Sum: " << a + b << endl;
Control Statements (if, switch, loops)
- If-Else:
if(age >= 18) {
cout << "Adult";
} else {
cout << "Minor";
}
- Switch:
switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
default: cout << "Invalid";
}
- Loops:
for(int i=1; i<=5; i++)
cout << i << " ";
int j=1;
while(j<=5) {
cout << j << " ";
j++;
}
int k=1;
do {
cout << k << " ";
k++;
} while(k<=5);
Functions in C++
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(10, 20);
cout << "Sum = " << result;
return 0;
}
Object-Oriented Programming in C++
- Class and Object:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1;
s1.name = "Alice";
s1.age = 20;
s1.display();
return 0;
}
- Inheritance, Polymorphism, Encapsulation, and Abstraction are key OOP features.
Arrays, Strings, and Pointers
- Array:
int arr[5] = {1,2,3,4,5};
- String (C++ STL):
#include <string>
string name = "C++ Language";
cout << name;
- Pointer:
int x = 10;
int* ptr = &x;
cout << *ptr; // 10
File Handling in C++
#include <fstream>
using namespace std;
int main() {
ofstream outfile("example.txt");
outfile << "Hello File Handling in C++";
outfile.close();
return 0;
}
ifstream→ Read filesofstream→ Write filesfstream→ Read & write
Memory Management
C++ provides dynamic memory allocation:
int* ptr = new int; // Allocate memory
*ptr = 25;
cout << *ptr;
delete ptr; // Free memory
new→ Allocates memorydelete→ Frees memory
Advantages & Limitations
- Advantages:
- High performance, object-oriented, STL support.
- Portable across platforms.
- Supports both procedural and OOP paradigms.
- Limitations:
- Complex syntax for beginners.
- Manual memory management.
- Larger programs may be harder to debug.
Real-Life Applications
- Software and game development
- Operating systems and compilers
- Real-time simulations
- GUI-based applications
- Banking and enterprise software
Sample Programs
- Factorial Using Function:
#include <iostream>
using namespace std;
int factorial(int n) {
if(n == 0) return 1;
return n * factorial(n-1);
}
int main() {
int num;
cin >> num;
cout << "Factorial = " << factorial(num);
return 0;
}
- Class Example with Inheritance:
#include <iostream>
using namespace std;
class Person {
public:
string name;
};
class Student : public Person {
public:
int roll;
void display() { cout << name << " - " << roll; }
};
int main() {
Student s;
s.name = "Bob";
s.roll = 101;
s.display();
return 0;
}
Conclusion
C++ is a versatile and powerful language suitable for both beginner programmers and advanced developers. Its combination of procedural and object-oriented programming, along with high performance, makes it ideal for software development, gaming, and systems programming. Mastering C++ builds a strong foundation for learning modern programming languages and creating efficient, scalable applications.
















