0% found this document useful (0 votes)
3 views8 pages

Basic C++ Programming

This document is a beginner-friendly guide to Basic C++ Programming, covering fundamental concepts such as variables, user input, arithmetic operators, control statements, loops, arrays, functions, and basic object-oriented programming. It includes example code snippets and explanations for each topic, along with a mini project for a simple calculator. Additionally, it recommends advanced topics for further study in programming and cybersecurity.

Uploaded by

i22-0605-382
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Basic C++ Programming

This document is a beginner-friendly guide to Basic C++ Programming, covering fundamental concepts such as variables, user input, arithmetic operators, control statements, loops, arrays, functions, and basic object-oriented programming. It includes example code snippets and explanations for each topic, along with a mini project for a simple calculator. Additionally, it recommends advanced topics for further study in programming and cybersecurity.

Uploaded by

i22-0605-382
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Here's a beginner-friendly guide to Basic C++ Programming.

Basic C++ Programming


What is C++?
C++ is a powerful programming language used for:

 Software development
 Game development
 Operating systems
 Embedded systems
 Robotics
 Competitive programming

1. Your First C++ Program


#include <iostream>

using namespace std;

int main() {
cout << "Hello, World!";
return 0;
}

Output
Hello, World!

Explanation

Code Meaning
#include <iostream> Allows input and output
using namespace std; Lets us use standard library names
int main() Main function where execution starts
cout Prints output
return 0; Ends the program successfully

2. Variables
Variables store data.

#include <iostream>
using namespace std;

int main() {
int age = 21;
float grade = 95.5;
char section = 'A';
string name = "Chris";

cout << name << endl;


cout << age << endl;

return 0;
}

Common Data Types

Type Example
int 10
float 3.14
double 3.141592
char 'A'
bool true
string "Hello"

3. User Input
#include <iostream>
using namespace std;

int main() {
string name;

cout << "Enter your name: ";


cin >> name;

cout << "Hello " << name;

return 0;
}

Output
Enter your name: Chris
Hello Chris
4. Arithmetic Operators
#include <iostream>
using namespace std;

int main() {
int a = 10;
int b = 5;

cout << a + b << endl;


cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl;

return 0;
}

Operators

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

5. If Statement
#include <iostream>
using namespace std;

int main() {
int age;

cout << "Enter age: ";


cin >> age;

if (age >= 18) {


cout << "Adult";
}

return 0;
}

6. If-Else
if (age >= 18) {
cout << "Adult";
}
else {
cout << "Minor";
}

7. Switch Statement
int day;

cout << "Enter day number: ";


cin >> day;

switch(day) {
case 1:
cout << "Monday";
break;

case 2:
cout << "Tuesday";
break;

default:
cout << "Invalid";
}

8. Loops
For Loop
for(int i = 1; i <= 5; i++) {
cout << i << endl;
}

Output:

1
2
3
4
5

While Loop
int i = 1;
while(i <= 5) {
cout << i << endl;
i++;
}

Do-While Loop
int i = 1;

do {
cout << i << endl;
i++;
} while(i <= 5);

9. Arrays
#include <iostream>
using namespace std;

int main() {

int numbers[5] = {10,20,30,40,50};

for(int i=0; i<5; i++) {


cout << numbers[i] << endl;
}

return 0;
}

10. Functions
#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

int main() {

int result = add(5, 3);

cout << result;

return 0;
}
Output:

11. Strings
#include <iostream>
#include <string>

using namespace std;

int main() {

string name = "Christopher";

cout << [Link]();

return 0;
}

12. Basic OOP (Classes)


#include <iostream>
using namespace std;

class Student {
public:
string name;
int age;

void display() {
cout << name << endl;
cout << age << endl;
}
};

int main() {

Student s1;

[Link] = "Chris";
[Link] = 21;

[Link]();

return 0;
}
Mini Project: Simple Calculator
#include <iostream>
using namespace std;

int main() {

double num1, num2;


char op;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter operator (+,-,*,/): ";


cin >> op;

cout << "Enter second number: ";


cin >> num2;

switch(op) {
case '+':
cout << num1 + num2;
break;

case '-':
cout << num1 - num2;
break;

case '*':
cout << num1 * num2;
break;

case '/':
cout << num1 / num2;
break;

default:
cout << "Invalid operator";
}

return 0;
}

Recommended Next Topics


1. Pointers
2. References
3. Vectors (vector)
4. File Handling
5. Structures (struct)
6. Object-Oriented Programming (OOP)
7. Templates
8. STL (Standard Template Library)
9. Data Structures and Algorithms
10. Competitive Programming

Since you're interested in programming and cybersecurity, after learning these basics, focus on:

 C++ OOP
 Memory management
 Pointers and references
 Data structures
 File handling
 Networking basics

These topics are especially useful for CTFs, malware analysis, reverse engineering, and systems
programming.

You might also like