techmore.in

C++ Basic Syntax

C++ syntax is designed to be efficient and expressive, allowing developers to write clear and effective code. Here’s a guide to the basic syntax elements in C++.


1. Structure of a C++ Program

A basic C++ program structure includes:

  • Preprocessor Directives: Instructions to the compiler.
  • Main Function: Entry point of the program.
  • Statements: Instructions executed by the program.

Example:

cpp
#include // Preprocessor directive int main() { // Main function std::cout << "Hello, World!" << std::endl; // Statement return 0; // Return statement }
  • #include : Includes the input-output stream library.
  • int main(): Defines the main function where execution starts.
  • std::cout: Used to output text to the console.
  • return 0;: Exits the program and returns a success code.

2. Basic Data Types

C++ provides several built-in data types:

  • Integer Types: int, short, long, long long
  • Floating-Point Types: float, double, long double
  • Character Type: char
  • Boolean Type: bool

Example:

cpp
int age = 25; // Integer float height = 5.9f; // Floating-point char initial = 'A'; // Character bool isStudent = true; // Boolean

3. Variables and Constants

  • Variables: Used to store data that can be modified.
  • Constants: Values that cannot be changed after initialization. Use const or constexpr keyword.

Example:

cpp
int number = 10; // Variable const int MAX = 100; // Constant

4. Control Structures

  • If-Else Statement: Conditional branching.

    cpp
    if (age > 18) { std::cout << "Adult" << std::endl; } else { std::cout << "Not an Adult" << std::endl; }
  • For Loop: Iterates a block of code a specific number of times.

    cpp
    for (int i = 0; i < 5; ++i) { std::cout << i << std::endl; }
  • While Loop: Repeats a block of code while a condition is true.

    cpp
    int count = 0; while (count < 5) { std::cout << count << std::endl; ++count; }
  • Do-While Loop: Similar to the while loop but guarantees at least one execution.

    cpp
    int count = 0; do { std::cout << count << std::endl; ++count; } while (count < 5);

5. Functions

Functions are blocks of code that perform a specific task and can be reused.

  • Function Declaration: Tells the compiler about the function's name, return type, and parameters.

    cpp
    int add(int a, int b); // Function declaration
  • Function Definition: Contains the actual implementation of the function.

    cpp
    int add(int a, int b) { return a + b; }
  • Function Call: Executes the function.

    cpp
    int result = add(5, 3); // Function call

6. Classes and Objects

C++ supports object-oriented programming, allowing you to define classes and create objects.

  • Class Definition: Blueprint for creating objects.

    cpp
    class Person { private: std::string name; int age; public: // Constructor Person(std::string n, int a) : name(n), age(a) {} // Member function void greet() { std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl; } };
  • Creating and Using Objects:

    cpp
    int main() { Person person("John", 30); // Create an object person.greet(); // Call member function return 0; }

7. Input and Output

  • Output: Using std::cout.

    cpp
    std::cout << "Hello, World!" << std::endl;
  • Input: Using std::cin.

    cpp
    int number; std::cout << "Enter a number: "; std::cin >> number;

8. Comments

  • Single-Line Comment: Starts with //.

    cpp
    // This is a single-line comment
  • Multi-Line Comment: Enclosed in /* ... */.

    cpp
    /* This is a multi-line comment spanning multiple lines. */

Summary

C++ basic syntax includes:

  • Program Structure: Includes preprocessor directives, the main function, and statements.
  • Data Types: Various types to handle different kinds of data.
  • Variables and Constants: Used to store and manage data.
  • Control Structures: Includes if-else, loops (for, while, do-while) for decision making and iteration.
  • Functions: For modular and reusable code.
  • Classes and Objects: For object-oriented programming.
  • Input and Output: Handling user input and program output.
  • Comments: For code documentation.

Understanding these basics will help you get started with C++ programming and build more complex applications.