C++ Basics Pdf

11 min read Jul 01, 2024
C++ Basics Pdf

C++ Basics: A Comprehensive Guide

C++ is a powerful and versatile programming language widely used for developing a wide range of applications, from operating systems and game engines to embedded systems and scientific simulations. This article will guide you through the fundamental concepts of C++ programming, providing a solid foundation for your journey into this robust language.

1. Introduction to C++

C++ is a compiled, statically typed, object-oriented programming language. It is considered a "middle-level" language, offering features of both high-level and low-level languages. This means that it provides a high level of abstraction for complex tasks while also allowing direct control over system resources.

2. Setting up the Environment

Before diving into the code, you need a proper environment to write and execute C++ programs. This typically involves:

  • Compiler: A compiler translates your C++ code into machine-readable instructions. Popular compilers include GCC (GNU Compiler Collection) and Clang.
  • Integrated Development Environment (IDE): An IDE provides a user-friendly interface for coding, debugging, and building your programs. Some popular IDEs for C++ include Visual Studio Code, Code::Blocks, and CLion.

3. Basic Syntax and Structure

C++ programs are structured into functions, which are blocks of code that perform specific tasks. Every program starts with the main() function:

#include 

int main() {
  // Your code goes here
  return 0;
}
  • #include <iostream>: This line includes the input/output stream library, allowing you to interact with the user.
  • int main(): This declares the main() function, the entry point of your program. It returns an integer value (typically 0 for success).
  • // Your code goes here: This is where you write your instructions.

4. Data Types and Variables

C++ supports a variety of data types for storing different kinds of values:

  • Integer (int): Used to store whole numbers (e.g., 10, -5, 0).
  • Floating-Point (float, double): Used to store numbers with decimal points (e.g., 3.14, 2.718).
  • Character (char): Used to store single characters (e.g., 'A', 'b', '5').
  • Boolean (bool): Used to store truth values (e.g., true, false).

Variables are used to store data within a program. You declare a variable with its type and a name:

int age = 25;
float height = 1.83;
char grade = 'A';
bool is_active = true;

5. Operators

C++ provides various operators for performing operations on data:

  • Arithmetic Operators: +, -, *, /, %
  • Assignment Operators: =, +=, -=, *=, /=, %=
  • Comparison Operators: ==, !=, >, <, >=, <=
  • Logical Operators: && (and), || (or), ! (not)

6. Control Flow Statements

Control flow statements determine the order in which code is executed:

  • if statement: Executes a block of code only if a condition is true.
  • else statement: Executes a block of code if the if condition is false.
  • else if statement: Allows you to check multiple conditions.
  • switch statement: Provides a concise way to execute different code blocks based on the value of an expression.
  • for loop: Executes a block of code repeatedly for a specified number of times.
  • while loop: Executes a block of code repeatedly as long as a condition is true.
  • do-while loop: Similar to a while loop, but it executes the loop body at least once.

7. Functions

Functions are reusable blocks of code that perform specific tasks. They help to organize your code and make it more manageable.

int sum(int num1, int num2) {
  return num1 + num2;
}

int main() {
  int result = sum(5, 10);
  std::cout << "Sum: " << result << std::endl;
  return 0;
}
  • Function Declaration: Defines the function's return type, name, and parameters.
  • Function Definition: Contains the code that the function will execute.
  • Function Call: Executes the function by using its name and passing any necessary arguments.

8. Arrays

Arrays are used to store collections of elements of the same data type:

int numbers[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {
  std::cout << numbers[i] << " ";
}
  • Declaration: Defines the array's type, name, and size.
  • Initialization: Assigns values to the elements of the array.
  • Accessing Elements: Individual elements are accessed using their index (starting from 0).

9. Strings

Strings are sequences of characters. In C++, strings can be handled using the std::string class:

std::string greeting = "Hello, world!";

std::cout << greeting << std::endl;

The std::string class provides methods for manipulating strings, such as concatenation (+), length (length()), and substring extraction (substr()).

10. Pointers

Pointers are variables that store memory addresses. They allow for direct manipulation of memory locations:

int num = 10;
int* ptr = # // Store the address of num in ptr

std::cout << "Value of num: " << num << std::endl;
std::cout << "Address of num: " << &num << std::endl;
std::cout << "Value of ptr: " << ptr << std::endl;
std::cout << "Value at address stored in ptr: " << *ptr << std::endl;

Pointers are essential for working with dynamic memory allocation, which is covered in more advanced C++ topics.

11. References

References provide an alternative way to access variables without copying their values. They act as aliases for existing variables:

int num = 10;
int& ref = num; // ref becomes an alias for num

ref = 20; // Modifying ref also changes num

std::cout << "num: " << num << std::endl;
std::cout << "ref: " << ref << std::endl;

References are useful for passing arguments to functions efficiently and for avoiding unnecessary copying.

12. Object-Oriented Programming (OOP)

C++ is an object-oriented programming language. This means it supports the following concepts:

  • Classes: Blueprints for creating objects. They define the properties (data members) and behaviors (member functions) of an object.
  • Objects: Instances of classes. They represent real-world entities and encapsulate data and methods.
  • Encapsulation: Hiding data and methods within a class, exposing only necessary elements.
  • Abstraction: Presenting simplified interfaces to complex functionalities.
  • Inheritance: Creating new classes that inherit properties and behaviors from existing classes.
  • Polymorphism: The ability of objects of different classes to respond to the same message in different ways.

Conclusion

This article covered the fundamental concepts of C++ programming. It introduced you to the language's syntax, data types, operators, control flow, functions, arrays, strings, pointers, references, and object-oriented programming principles. This foundation will enable you to begin writing your own C++ programs and explore the vast possibilities this language offers. Remember that C++ is a complex language with many advanced features, so continuous learning and practice are crucial for mastering it.

Latest Posts


Featured Posts