Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

C++ Codes

Uploaded by

ROBINAH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C++ Codes

Uploaded by

ROBINAH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Introduction to C++

 What is C++?
C++ is a general-purpose programming language created by Bjarne
Stroustrup as an extension of the C programming language. It
supports both procedural and object-oriented programming, making it
a versatile language.

 Basic Syntax:

A C++ program is composed of functions and statements. The main


function is where every C++ program starts execution.

Example: Hello World Program

cpp
Copy code
#include <iostream>

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

#include <iostream>: This includes the standard input/output stream


library.
int main(): The main function where the program begins execution.
std::cout: Used to output text to the console.
return 0;: Returns 0, indicating that the program ended successfully.

2. Variables and Data Types

 Data Types:
Common data types include int (integer), float (floating-point
number), double (double-precision floating-point), char (character),
and bool (Boolean).
Example:
cpp
Copy code
int age = 25;
float height = 5.9;
char grade = 'A';
bool is Student = true;
 Variable Declaration:

o Variables are declared with a type followed by a name and an optional


initialization.
cpp
Copy code
int number; // Declaration
number = 10; // Assignment

3. Operators

 Arithmetic Operators: +, -, *, /, %
 Relational Operators: ==, !=, >, <, >=, <=
 Logical Operators: &&, ||, !
Example:
cpp
Copy code
int a = 10, b = 20;
int sum = a + b; // Addition
bool is Equal = (a == b); // Comparison

4. Control Structures

 Conditional Statements:
o if, else if, else for branching logic.
Example:
cpp
Copy code
int number = 10;
if (number > 0) {
std::cout << "Positive number" << std::endl;
} else {
std::cout << "Non-positive number" << std::endl;
}
 Loops:
for, while, and do-while loops for repeating actions.
Example:
cpp
Copy code
for (int i = 0; i < 5; i++) {
std::cout << "Count: " << i << std::endl;
}
5. Functions

 Defining Functions:
o A function is a reusable block of code. Functions can take parameters
and return values.
Example:
cpp
Copy code
int add(int a, int b) {
return a + b;
}

int main() {
int sum = add(5, 3);
std::cout << "Sum: " << sum << std::endl;
return 0;
}
 Function Overloading:
o You can define multiple functions with the same name but different
parameters.

6. Arrays and Strings

 Arrays:
o An array is a collection of elements of the same type.
Example:
cpp
Copy code
int numbers[5] = {1, 2, 3, 4, 5};
std::cout << "First number: " << numbers[0] << std::endl;
 Strings:
o Strings in C++ are arrays of characters terminated with a null
character (\0), or they can be managed using the std::string class.
Example:
cpp
Copy code
std::string name = "Alice";
std::cout << "Name: " << name << std::endl;

7. Object-Oriented Programming (OOP)

 Classes and Objects:


A class is a blueprint for creating objects, which are instances of the
class.
Example:
cpp
Copy code
class Dog {
public:
std::string name;
void bark() {
std::cout << name << " says Woof!" << std::endl;
}
};

int main() {
Dog myDog;
myDog.name = "Rex";
myDog.bark();
return 0;
}

 Inheritance, Polymorphism, and Encapsulation:


o Key concepts in OOP that allow you to create a hierarchy of classes,
reuse code, and protect data.

8. Pointers and Memory Management

 Pointers:
A pointer is a variable that holds the memory address of another
variable.
Example:
cpp
Copy code
int value = 10;
int *ptr = &value;
std::cout << "Value: " << *ptr << std::endl;
 Dynamic Memory Allocation:
o Using new and delete to allocate and free memory dynamically.

9. Standard Template Library (STL)

 Containers:
o Vectors, Lists, Maps, etc., that help manage collections of data.
 Iterators:
o Objects that allow traversing through elements in containers.
 Algorithms:
o Functions like sort, find, reverse, etc., for common operations on
containers.
9. Advanced Topics

 File Handling:
o Reading from and writing to files using streams.
 Exception Handling:
o Using try, catch, and throw to manage errors gracefully.
 Templates:
o Creating generic functions and classes.
 Multithreading:
o Running multiple threads in parallel.
Resources for Learning C++
 Books:
o "Programming: Principles and Practice Using C++" by Bjarne
Stroustrup.
o "The C++ Programming Language" by Bjarne Stroustrup.
 Online Tutorials:
o Codecademy
o GeeksforGeeks

 Practice:
o LeetCode, HackerRank, and Codewars for coding challenges.

You might also like