C++ is a general-purpose programming language developed by Bjarne Stroustrup. It is an extension of the C language with object-oriented features, making it suitable for system-level programming, game development, and applications requiring high performance.
Key Features
- Object-Oriented Programming (OOP)
- Standard Template Library (STL)
- Memory Management (pointers, dynamic allocation)
- Multi-paradigm (procedural, functional, object-oriented)
An IDE is a software application that provides comprehensive facilities for software development. Popular IDEs for C++ include:
- Visual Studio Code
- Code::Blocks
- Eclipse
- JetBrains CLion
Features
- Code Editor
- Debugger
- Compiler Integration
- IntelliSense (code suggestions)
A compiler translates human-readable C++ code into machine code. Popular C++ compilers:
- GCC (GNU Compiler Collection)
- Clang
- Microsoft Visual C++ Compiler
Visual Studio Code is a lightweight, powerful code editor with extensive extensions for C++ programming.
Steps to Download
- Visit the official website.
- Download the version appropriate for your operating system.
- Install the downloaded file.
Steps
- Download GCC using MinGW or a similar tool.
- For Windows, use MinGW-w64.
- Install MinGW and add its
bindirectory to the systemPATH. - Test installation by typing
g++ --versionin the terminal.
- Open the VS Code installer and follow the on-screen instructions.
- Install the C/C++ extension from the Extensions Marketplace.
- Configure the
tasks.jsonandlaunch.jsonfiles for building and debugging.
- Explorer: View and manage project files.
- Editor: Write and edit code.
- Terminal: Execute commands.
- Extensions: Add new features like IntelliSense and debugging tools.
Code
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}Steps
- Open a new file in VS Code.
- Save it with a
.cppextension. - Compile using
g++ filename.cpp -o filename. - Run the executable using
./filename.
#include <iostream>: Includes the library for input/output.using namespace std;: Avoids prefixing standard library functions withstd::.int main(): Entry point of the program.cout: Outputs text to the console.return 0;: Indicates successful program termination.
- Single-line comment:
// This is a comment - Multi-line comment:
/* This is
a multi-line
comment */Primitive Types
int,float,double,char,bool
Derived Types:
- Arrays, Functions, Pointers User-defined Types:
- Structures, Classes, Enums
A variable is a container for storing data.
Syntax
int number = 10;
float pi = 3.14;
char grade = 'A';Code
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is " << age << endl;
return 0;
}- Arithmetic:
+,-,*,/,% - Relational:
==,!=,>,<,>=,<= - Logical:
&&,||,!
Code
if (condition) {
// code block
} else {
// code block
}Code
switch (variable) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}- For Loop
for (int i = 0; i < 5; i++) {
cout << i << endl;
}- While Loop
while (condition) {
// code block
}- Do-While Loop
do {
// code block
} while (condition);Syntax
returnType functionName(parameters) {
// code block
return value;
}An array stores multiple values of the same type.
int arr[5] = {1, 2, 3, 4, 5};Converting one data type to another.
float num = 10.5;
int integer = (int)num;Strings are objects in C++.
#include <string>
string str = "Hello";A pointer stores the address of a variable.
int var = 10;
int* ptr = &var;C++ is object-oriented, using classes and objects.
Class
class Car {
public:
string brand;
void display() {
cout << brand;
}
};Object
Car car1;
car1.brand = "Toyota";
car1.display();Special method called when an object is created.
class Car {
public:
string brand;
Car(string b) {
brand = b;
}
};A way to derive one class from another.
class Parent {
public:
void display() {
cout << "Parent Class";
}
};
class Child : public Parent {
};