Programming C++  «Prev  Next»
Lesson 1

C++ Programming

Introduction to C++ is the first course in the C++ for C Programmers series. This course teaches you the basic differences between C++ and C and gets you started writing C++ programs. After completing this course, you will be able to write C++ programs that:
  1. Use function prototypes and static_cast to ensure type safety
  2. Use the single-line comment style
  3. Use reference declarations to creates aliases for variables
  4. Pass arguments to functions using call-by-reference
  5. Implement dynamically allocated multidimensional arrays

#include <iostream>
#include <cstring>

using namespace std;

void reverse(char *str, int count = 0);
int main()
  char *s1 = "This is a test.";
  char *s2 = "I like C++.";

  reverse(s1); // reverse entire string
  reverse(s2, 7);  // reverse 1st 7 chars

  cout << s1 << '\n';
  cout << s2 << '\n';

  return 0;
// ----- reverse 
void reverse(char *str, int count){
  int i, j;
  char temp; 
  if(!count) count = strlen(str)-1;
  for(i = 0, j=count; i < j; i++,  j--) {
    temp = str[ i ];
    str[ i ] = str[j];
    str[j] = temp;

Big C++

Role of function Prototypes in C++

Here's a breakdown of the key roles function prototypes serve in C++:
  1. Early Error Checking (Type Safety):
    • Defines the interface: A function prototype declares the function's name, return type, and the number and types of its parameters. Imagine it like a contract the function needs to adhere to.
    • Compiler Verification: The compiler uses this information to cross-check function calls throughout your code. If a function is used with the wrong number of arguments, incorrect argument types, or an attempted assignment to a mismatched return type, the compiler can flag an error early.
  2. Enabling Flexible Code Organization
    • Top-down vs. Bottom-up: Traditional C required you to define functions before you could use them, leading to a bottom-up coding style. Function prototypes allow you to declare a function before its full definition.
    • Improved Readability: You can present the prototypes for a group of functions near the top of your source file, providing a clear overview of what the functions do without having to immediately see their full implementations.
  3. Facilitating Separate Compilation:
    • Modular Code: Large projects often split code into multiple source files. Function prototypes are frequently used in header files (.h) to declare the functions that are defined in separate implementation files (.cpp).
    • Linker's Guide: When the compiler encounters a call to a function whose implementation is in another file, the prototype tells it what to expect. This allows the linker to connect everything even when the full function isn't yet available during compilation.

// Function prototype
int calculateArea(int length, int width); 

int main() {
    int result = calculateArea(5, 10); 
    // ...

// Function definition (can be placed later)
int calculateArea(int length, int width) {
    return length * width;

Programming C++
Programming C++

Historical Role of C++

In many ways, C and C++ run the world. You would never know it based on the discussion of other programming languages, such as Java and Ruby, but the vast majority of high-performance desktop applications and operating systems are written in C++. In addition, most embedded applications are written in C because of this programming language's ability to operate closer to the hardware level. We are not talking about smartphone apps or web applications, because these domains make use of special languages, such as Java and Kotlin for Android and Swift for iOS. Smartphone applications only use C/C++ for inner loops that have a crying need for speed, and for libraries shared across operating systems.
C and C++ have dominated systems programming for such a long time, that it is difficult to imagine them being replaced. Yet many experts are saying it is time for them to go, and for programmers to embrace better alternatives. Microsoft Azure CTO Mark Russinovich suggested that C and C++ developers should move to Rust instead.

SEMrush Software