Introduction To C++
Introduction To C++
Introduction to C++:
C++ is an extension of the C language, developed by Bjarne Stroustrup in 1983.
It is a general-purpose, object-oriented programming (OOP) language.
C++ supports both procedural and object-oriented programming paradigms, making it
more flexible than C.
It is widely used in system/software development, game development, and competitive
programming.
2. Basic Syntax:
Every C++ program starts with the main() function, like in C.
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Header Files: #include <iostream> for input-output operations.
Namespace: using namespace std; is used to avoid writing std:: repeatedly.
Semicolons ; are used to terminate statements.
3. Data Types:
Primitive Types: int, float, double, char, bool, etc.
cpp
Copy code
int age = 22;
char grade = 'A';
bool isValid = true;
Derived Types: Arrays, pointers, references, classes, structures, and unions.
Void: Special type representing "no type," often used for functions.
4. Input/Output:
cin and cout: Used for input and output in C++.
cpp
Copy code
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age;
5. Operators:
Arithmetic Operators: +, -, *, /, %.
Relational Operators: ==, !=, <, >, <=, >=.
Logical Operators: && (AND), || (OR), ! (NOT).
Increment/Decrement Operators: ++, --.
6. Control Structures:
If-Else Statements:
cpp
Copy code
if (age > 18) {
cout << "Adult";
} else {
cout << "Minor";
}
Switch Statements:
cpp
Copy code
switch (age) {
case 18:
cout << "You are 18!";
break;
default:
cout << "Not 18!";
}
Loops:
For Loop:
cpp
Copy code
for (int i = 0; i < 5; i++) {
cout << i << endl;
}
While Loop:
cpp
Copy code
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
}
Do-While Loop:
cpp
Copy code
int i = 0;
do {
cout << i << endl;
i++;
} while (i < 5);
7. Functions:
Functions in C++ are declared using the return type, function name, and parameters.
cpp
Copy code
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3);
cout << "Sum: " << sum;
return 0;
}
C++ supports function overloading, allowing functions to have the same name but
different parameters.
8. Object-Oriented Programming (OOP):
Classes and Objects: A class is a blueprint for creating objects.
cpp
Copy code
class Person {
public:
string name;
int age;
void introduce() {
cout << "Name: " << name << ", Age: " << age;
}
};
int main() {
Person p1;
p1.name = "Sumit";
p1.age = 22;
p1.introduce();
return 0;
}
Encapsulation: Grouping data (variables) and methods (functions) in a class.
Inheritance: Deriving a class from another class.
cpp
Copy code
class Student : public Person {
public:
string school;
};
Polymorphism: Ability to take many forms, like function overloading or overriding.
Abstraction: Hiding complex details and showing only essential information.
9. Constructors and Destructors:
Constructor: A special function automatically called when an object is created.
cpp
Copy code
class Person {
public:
Person() {
cout << "Constructor Called!";
}
};
Destructor: Automatically called when an object is destroyed.
cpp
Copy code
~Person() {
cout << "Destructor Called!";
}
10. Pointers and References:
Pointers: Store the memory address of a variable.
cpp
Copy code
int x = 10;
int *ptr = &x;
cout << *ptr; // Dereferencing
References: Another name for an existing variable.
cpp
Copy code
int a = 10;
int &ref = a;
ref++;
cout << a; // Output: 11
11. Memory Management:
Dynamic Memory Allocation: Using new and delete.
cpp
Copy code
int *ptr = new int;
*ptr = 5;
delete ptr;
Smart Pointers: Introduced in C++11, they manage memory automatically (e.g.,
shared_ptr, unique_ptr).