C++ Notes
1. Introduction to C++:
- C++ is a general-purpose programming language.
- Supports both procedural and object-oriented programming.
- Extension of the C language.
2. Structure of a C++ Program:
#include<iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
3. Variables and Data Types:
- Common types: int, float, double, char, bool
int a = 5;
float b = 3.14;
char c = 'A';
bool isTrue = true;
4. Operators:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: &&, ||, !
5. Conditional Statements:
if (condition) {
// code
} else if (condition) {
// code
} else {
// code
6. Loops:
for (int i = 0; i < 5; i++) {
cout << i << endl;
while (condition) {
// code
do {
// code
} while (condition);
7. Functions:
int add(int a, int b) {
return a + b;
}
8. Arrays:
int arr[5] = {1, 2, 3, 4, 5};
cout << arr[0];
9. Strings:
string name = "John";
cout << name.length();
10. Object-Oriented Programming:
- Class and Object
class Person {
public:
string name;
int age;
void introduce() {
cout << "Name: " << name << ", Age: " << age;
};
11. Constructors and Destructors:
class MyClass {
public:
MyClass() { cout << "Constructor called!"; }
~MyClass() { cout << "Destructor called!"; }
};
12. Inheritance:
class Animal {
public:
void sound() { cout << "Animal sound"; }
};
class Dog : public Animal {
public:
void bark() { cout << "Woof!"; }
};
13. File Handling:
#include<fstream>
ofstream file("example.txt");
file << "Hello";
file.close();
14. STL (Standard Template Library):
- Useful containers: vector, list, map
#include<vector>
vector<int> v = {1, 2, 3};
v.push_back(4);
15. Useful Functions and Keywords:
- sizeof(), return, break, continue, new, delete