Skip to content

itsmahbub/cpp-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Introduction to C++

C++ is a high-level, general-purpose programming language developed by Bjarne Stroustrup in the early 1980s. It is an extension of the C programming language with added object-oriented programming features. C++ is widely used for developing system software, game development, and many other applications.

Setting Up Your Development Environment

Before you start coding in C++, you need a development environment. You can use IDEs like Visual Studio, Code::Blocks, or CLion, or simple text editors like Visual Studio Code or Sublime Text with a C++ compiler like GCC or Clang. Set up your environment according to your chosen tools.

Basic C++ Syntax

1. Comments:

  • C++ supports both single-line and multi-line comments.
  • Single-line comments start with //, and everything after // on that line is considered a comment.
  • Multi-line comments are enclosed between /* and */.
// This is a single-line comment

/* 
   This is a
   multi-line comment
*/

2. Preprocessor Directives:

  • Preprocessor directives start with #.
  • They are not part of the C++ code and are processed before compilation.
  • Common preprocessor directives include #include and #define.
#include <iostream> // Include a header file

#define PI 3.14159   // Define a constant

3. Main Function:

  • Every C++ program must have a main function where the program execution begins.
  • The main function returns an integer value (usually 0 for success).
int main() {
    // Your code here
    return 0; // Exit the program with status 0 (success)
}

4. Semicolons:

  • Statements in C++ are terminated by semicolons ;.
int x = 5; // Semicolon terminates the statement

5. Blocks and Scope:

  • Code blocks are enclosed by curly braces {}.
  • Variables declared within a block are scoped to that block.
int main() {
    int x = 5; // Variable x is scoped to the main function
    {
        int y = 10; // Variable y is scoped to this block
    }
    // y is not accessible here
}

6. Data Types:

  • C++ supports various data types, including int, float, double, char, and more.
  • You can use the sizeof operator to determine the size of a data type in bytes.
int age = 25;       // Integer
float price = 19.99; // Single-precision floating-point
double salary = 55000.75; // Double-precision floating-point
char grade = 'A';   // Character
cout << sizeof(salary) << endl; // 8

7. Variables:

  • Variables must be declared with a data type before use.
  • You can also initialize variables during declaration.
int number;         // Declaration
double pi = 3.141;  // Declaration and initialization

8. Constants:

  • You can define constants using the const keyword.
const double PI = 3.14159; // Constant value for PI

9. Input and Output:

  • C++ provides input and output streams via cin and cout.
#include <iostream>
using namespace std;

int main() {
    int x;
    cout << "Enter a number: ";
    cin >> x;
    cout << "You entered: " << x;
    return 0;
}

10. Operators:

  • C++ supports various operators for arithmetic, comparison, and logical operations.
int a = 10, b = 5;
int sum = a + b;      // Addition
int difference = a - b; // Subtraction
bool isGreater = a > b; // Comparison
bool logicalAnd = true && false; // Logical AND

These are the fundamental elements of C++ syntax that you'll need to get started with C++ programming. As you progress, you'll dive into more advanced concepts and features of the language.

Data Types and Variables

Data types and variables are fundamental concepts in C++ programming. Data types define the type of data a variable can hold, and variables store and manipulate that data. Here are some common data types and how to work with them in C++:

1. Integer Types:

  • int: Represents signed integers (e.g., -3, 0, 42). Typically 4 bytes.Range: -2^31 to 2^31 - 1
  • short: Represents short integers. Typically 2 bytes.Range: -2^15 to 2^15 - 1
  • long: Represents long integers. Typically 4 bytes.Range: -2^31 to 2^31 - 1
  • long long: Represents very long integers. Typically 8 bytes.Range: -2^63 to 2^63 - 1

Keep in mind that these are minimum requirements, and the actual sizes and ranges can vary between different compilers and platforms. To ensure portability and to work with specific integer sizes, you can use the header which provides fixed-size integer types like int32_t, int64_t, etc., with a known size and range.

Example:

int age = 30;
short population = 20000;
long totalDistance = 1500000000;

2. Floating-Point Types:

  • float: Represents single-precision floating-point numbers (e.g., 3.14, 0.001). Typically 4 bytes. Approximate range: -3.4E38 to 3.4E38. Approximately 6-9 significant decimal digits of precision.
  • double: Represents double-precision floating-point numbers (used most often). Typically 8 bytes. Approximate range: -1.7E308 to 1.7E308. Approximately 15-18 significant decimal digits of precision.
  • long double: Represents extended precision floating-point numbers. Minimum range and precision are typically greater than that of double.

These are minimum requirements, and the actual sizes, ranges, and precisions can vary between different compilers and platforms.

Example:

float price = 19.99;
double pi = 3.14159265359;

3. Character Types:

  • char: Represents a single character (e.g., 'A', '1', '$').

Example:

char grade = 'A';

4. Boolean Type:

  • bool: Represents a boolean value, either true or false.

Example:

bool isRaining = true;
bool hasPassedExam = false;

5. Constants:

  • You can use the const keyword to define constants, which are values that cannot be changed after initialization.

Example:

const int MAX_VALUE = 100;
const double PI = 3.14159;
PI = 3.14; // Error: cannot change the value of a constant

6. Type Modifiers:

  • You can use type modifiers to expand the range of data types. For example, unsigned for non-negative integers.

Example:

unsigned int positiveNumber = 42;

7. Sizeof Operator:

  • You can use the sizeof operator to determine the size of a data type in bytes.

Example:

int sizeOfInt = sizeof(int); // Returns the size of an integer in bytes

8. Type Casting:

  • You can convert one data type into another using type casting. For example, from int to double.

Example:

int a = 5;
double x = a; // Implicit casting: int to double, also known as "coercion"
double y = (double)a; // Explicit C-style casting: int to double
double z = static_cast<double>(x); // Explicit static_cast: int to double

10. User-Defined Data Types:

  • You can create your own data types using struct and class (for complex data structures) or enum (for enumerations).

Example:

struct Point {
    int x;
    int y;
};
class Line {
    public:
        Point start;
        Point end;
        double get_length(){
            double length;
            length = calculate_line_length(start, end);
            return length;
        }
};

enum DayOfWeek {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
};

Understanding data types and how to use variables of different types is essential for writing C++ programs. It allows you to represent and manipulate data effectively in your code.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages