vaayaa Inclusive Education
Micro-Diploma

C++ Programming

5-Year Access Self-Paced Learning Certificate Included

Unlock Full Course

Get instant access to all sections and earn your verified certificate.

$9.99

Table of Contents

FREE PREVIEW — Section 1 of Module 1

Getting Started with C++: Syntax and Structure

Unit
  • Program entry point: Every C++ program begins execution in the main() function. The function returns an integer (0 for success) and can accept command-line arguments via argc and argv.
  • Comments and whitespace: Use // for single-line comments and /* */ for multi-line blocks. Whitespace (spaces, tabs, newlines) is ignored by the compiler but improves readability.
  • Include directives: The #include <iostream> preprocessor directive brings in the standard input/output library. Headers like <string> and <vector> provide additional functionality.
  • Standard namespace: The statement using namespace std; allows direct access to standard library names like cout and cin without prefixing with std::. Avoid this in header files to prevent namespace pollution.
  • Output with cout: The cout object, combined with the insertion operator <<, prints text to the console. Example: cout << 'Hello, World!' << endl;.
  • Input with cin: Use cin with the extraction operator >> to read user input. For example: int age; cin >> age; stores the integer directly.
  • Newline vs endl: endl inserts a newline and flushes the output buffer, while '\n' only inserts a newline. Prefer '\n' for performance unless you need immediate flush.
  • Basic structure: A minimal C++ program consists of includes, namespace declaration, and a main() function body. This skeleton forms the foundation for every executable.
  • Variables and data types: C++ offers several built-in data types including int for integers, double for floating-point numbers, char for single characters, and bool for Boolean values. Variables must be declared with a type before use and can be initialized at declaration. For example, int age = 25; double price = 19.99;.
  • Function definitions: Functions encapsulate reusable blocks of code. A function definition includes a return type, a name, a parameter list, and a body enclosed in braces. The main function is the entry point, but you can define other functions. Example: int square(int x) { return x * x; }.
  • Conditional statements: If, else if, and else allow your program to make decisions based on conditions. Conditions are expressions that evaluate to true or false. Use logical operators like &&, ||, and ! to combine conditions. Example: if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else grade = 'C';.
  • Loop constructs: For loops, while loops, and do while loops repeat execution of a block of code. For loops are ideal when you know the number of iterations, while loops continue as long as a condition holds true. Example: for (int i = 0; i
Code Example
C++
#include <iostream>
using namespace std;

int main() {
cout << 'Enter your name: ';
string name;
cin >> name;
cout << 'Hello, ' << name << '!' << endl;
return 0;
}

This program reads a name from standard input and prints a greeting. Note the use of cin and cout with the string type. The endl ensures the output appears immediately.

Quick Revision
  • C++ programs start in main() and return an integer.
  • Use #include to import libraries and using namespace std; for convenience.
  • cout with << prints output; cin with >> reads input.
  • Prefer '\n' over endl for better performance in most cases.

Like what you see? Unlock the full course to continue.

Limited Offer
$9.99
Limited Offer $9.99