📘 Table of Contents
1. Introduction to C++
C++ is a powerful, high-performance programming language used for software, game engines, and systems programming. It extends C with object-oriented features.
Note: C++ is known for its speed and control — ideal for applications where performance is critical.
2. Setting Up Environment
- Install Code::Blocks or Visual Studio.
- Ensure
g++compiler is installed.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++!" << endl;
return 0;
}
3. Basic Syntax
C++ programs always begin with a main() function.
4. Variables and Data Types
int age = 20;
float price = 9.99;
string name = "Alice";
char grade = 'A';
5. Operators
int a = 5, b = 2;
cout << a + b; // 7
6. Conditionals & Loops
for (int i = 1; i <= 5; i++) {
cout << i << endl;
}
7. Functions
int add(int a, int b) {
return a + b;
}
8. Arrays and Strings
int nums[3] = {1, 2, 3};
string text = "Hello";
9. Object-Oriented Programming (OOP)
class Person {
public:
string name;
void greet() { cout << "Hello " << name; }
};
int main() {
Person p;
p.name = "Alice";
p.greet();
}
10. Best Practices
- Always initialize variables.
- Use
constwhere applicable. - Use descriptive variable names.
11. Mini Project — Simple Calculator
#include <iostream>
using namespace std;
int main() {
double a, b;
char op;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Choose operator (+,-,*,/): ";
cin >> op;
switch(op) {
case '+': cout << a+b; break;
case '-': cout << a-b; break;
case '*': cout << a*b; break;
case '/': cout << a/b; break;
default: cout << "Invalid!";
}
}
12. Conclusion
C++ gives you control, power, and performance. It’s the foundation of modern systems, games, and large-scale applications.
Comments
Post a Comment