A collection of C++ programs covering basic concepts, problem-solving, algorithms, and object-oriented programming.
- Variables & Data Types
- Operators
- Conditional Statements
- Loops
- Functions
- Arrays
- Strings
- Pointers
- Structures
- Classes & Objects
- Inheritance
- Polymorphism
- File Handling
- Standard Template Library (STL)
- Sorting & Searching
- Pattern Programs
- Basic Algorithms
π Cpp-Programs
βββ Calculator.cpp
βββ Conditional_statemen.cpp
βββ If_statemen.cpp
βββ README.md
βββ Switch.cpp
βββ Third.cpp
βββ Triangle.cpp
βββ arithmetic_operations.cpp
βββ ternary_operator.cpp
βββ Temperature.cpp
βββ While_loop.cpp
βββ DO-While - Loop.cpp
βββ For_Loop.cpp
βββ Break_continue.cpp
βββ Squre_game.cpp
βββ Random Number.cpp
βββ Conditional_statemen.cpp
βββ Random_event.cpp
βββ Number_Guess.cpp
βββ Functions.cpp
βββ Function-2.cpp
βββ Function--3.cpp
βββ Grade_system.cpp
βββ Function 4.cpp
βββ Full Name.cpp
βββ void day()
βββ Global_variable.cpp
βββ Bank_System.cpp
βββ Bank-version-2.cpp
βββ Rock-paper-Game.cpp
βββ Arrays.cpp
βββ Arrays-2.cpp
βββ Arrays-3.cpp
βββ Dice.cpp
βββ Array-loop.cpp
βββ Array-loop-sum.cpp
βββ Search-Array.cpp
βββ Menu.cpp
βββ Search_string_array.cpp
βββ C-Bubble_sort.cpp
βββ Fill_Function.cpp
βββ
βββ
βββ ...
- C++17 or later
- Any C++ compiler (GCC, MinGW, MSVC, Clang)
Compile:
g++ filename.cpp -o outputRun:
output.exe./outputThis repository is maintained for learning, practice, and improving C++ programming skills. New programs and concepts will be added regularly.
β’ General-purpose programming language. β’ Created by Bjarne Stroustrup (1985), β’ Extension of the C language. β’ Supports Object-Oriented Programming (OOP). β’ Compiled language. β’ Fast and memory efficient.
#include
int main() { std::cout << "Hello World!"; return 0; }
Explanation:
#include -> Includes a library. -> Input & Output library. int -> Return type. main() -> Program entry point. {} -> Code block. return 0; -> Program ended successfully.
Single-line
// Comment
Multi-line
/* Comment Comment */
Integer
int age = 18;
Floating Point
float price = 10.5f; double pi = 3.1415926535;
Character
char grade = 'A';
Boolean
bool isOnline = true;
String
std::string name = "ZERO";
Void
void function();
char -> 1 Byte bool -> 1 Byte short -> 2 Bytes int -> 4 Bytes long -> 4 or 8 Bytes long long -> 8 Bytes float -> 4 Bytes double -> 8 Bytes
Check size:
sizeof(int)
Declaration
int age;
Initialization
int age = 18;
Multiple Variables
int a = 1, b = 2, c = 3;
const double PI = 3.14159;
Cannot be modified.
10 Integer 3.14 Double 3.14f Float 'A' Character "Hello" String true Boolean
int float double char bool void return const if else switch case default for while do break continue class struct public private protected virtual new delete namespace using typedef auto
std::cout
Using whole namespace
using namespace std;
Using specific object
using std::cout;
Type alias
using text = std::string;
std::cout
MyClass::function()
Namespace::variable
int age;
std::cin >> age;
std::cout << "Hello";
New Line
'\n'
or
std::endl
Arithmetic
/ %
Assignment
= += -= *= /= %=
Comparison
== !=
<
= <=
Logical
&& || !
Increment
++
Decrement
--
Recommended
static_cast(x)
Old
(double)x
Implicit
int β double
Explicit
static_cast(5.8)
if(condition) { }
if(condition) {
} else {
}
if() {
} else if() {
} else {
}
switch(choice) { case 1: break;
case 2: break;
default: }
For
for(int i=0;i<5;i++) { }
While
while(condition) { }
Do While
do {
} while(condition);
Stops a loop.
Skips current iteration.
Declaration
void greet();
Definition
void greet() { }
Calling
greet();
void add(int a, int b)
Arguments
add(5,3);
int add(int a,int b) { return a+b; }
int add(int a,int b)
double add(double a,double b)
Same name. Different parameters.
void greet(string name="User")
Local
Inside function.
Global
Outside all functions.
int nums[5];
Initialization
int nums[]={1,2,3};
Access
nums[0]
int matrix[3][3];
std::string name;
Length
name.length()
Size
name.size()
Empty
name.empty()
Append
name += "Hello";
Clear
name.clear();
toupper()
tolower()
isdigit()
isalpha()
int x=5;
int& ref=x;
int x=5;
int* ptr=&x;
Address
&x
Value
*ptr
Allocate
new
Delete
delete
enum Color { RED, GREEN, BLUE };
struct Student { string name; int age; };
class Car { public:
private:
};
Car bmw;
Car() {
}
Runs automatically when object is created.
~Car() {
}
Runs automatically when object is destroyed.
public
private
protected
class Dog : public Animal
Function Overloading
Function Overriding
Virtual Functions
Hiding internal data.
Showing only important information.
ofstream
ifstream
fstream
try
catch
throw
vector
map
set
queue
stack
pair
algorithm
sqrt()
pow()
abs()
round()
floor()
ceil()
length()
size()
substr()
find()
replace()
erase()
insert()
compare()
β’ Prefer std::string over char arrays. β’ Prefer '\n' over std::endl unless flushing is needed. β’ Use const whenever possible. β’ Prefer static_cast over C-style casts. β’ Give variables meaningful names. β’ Keep functions small. β’ Don't use global variables unless necessary. β’ Comment why, not what. β’ Initialize variables. β’ Format your code consistently.
If you find this repository helpful, consider giving it a star.