Skip to content

Language Basics

In C++, the fundamental building blocks of any program are functions and classes. Functions allow you to define reusable blocks of code that perform specific tasks, while classes provide a way to organize and structure data and behavior. Understanding these two concepts is key to mastering C++ and creating efficient, modular programs.

At first, when you start writing C++ programs, you’ll primarily work with a special function called main, which serves as the entry point of every program. All your code will be written inside this function.

As your programs grow larger, you’ll want to organize your code by separating it into different functions. This allows for better structure, reusability, and readability. Later on, you’ll also learn how to place these functions in separate files to keep your projects organized and manageable.

driver.cc
// Copyright 2024 CSCE240
int main () {}

In the second half of the course, you’ll start creating your own classes to model real-world concepts and organize your code more effectively. Classes are a powerful feature in C++ that let you define custom data types with both properties (variables) and behaviors (functions).

From the very beginning, you’ll work with two special objects of the iostream class: cin for input and cout for output. These objects will help you interact with the user and display results as you begin writing more complex programs.

driver.cc
// Copyright 2024 CSCE240
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
int input;
cin >> input;
cout << input << endl;
return 0;
}

The creation process of a C++ program involves 6 steps:

  1. Edit
  2. Preprocess
  3. Compile
  4. Link
  5. Load
  6. Execute

The first step in creating a C++ program is writing the code in a text editor. This is where the programmer writes and organizes their instructions for the program. Popular text editors for C++ include Visual Studio Code, CLion, and Sublime Text, though you can use any editor you’re comfortable with. For this course, you will be saving files with the following extensions: .cc, .h, and makefile.

Before the source code is compiled, the preprocessor handles all preprocessor directives, which are instructions that begin with a # symbol. These directives are executed to prepare the code for compilation. Common tasks performed during this step include adding header files (e.g., #include <iostream>), or like (e.g., #include "program.h"), and performing conditional compilation. The preprocessor ensures that your code is properly set up before moving to the compilation stage.

driver.cc
// Copyright 2024 CSCE240
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "こんにちは世界" << endl;
return 0;
}

In this step, the compiler translates your source code into object code, a lower-level representation that the computer can understand. In this course, you’ll use the g++ compiler to perform this task. To compile a file into object code, you can use the -c flag with the following command:

Terminal window
g++ -c driver.cc

This will generate an object file (e.g., driver.o) that is ready for the next step in the process.

  • DirectoryCSCE240
    • DirectoryProject01
      • driver.cc
      • driver.o
    • DirectoryProject02
      • TODO.md

The linker takes the object code files produced during the compile step and combines them into a single executable file. This step ensures that all the necessary code, including libraries and other dependencies, is properly connected. In this course, you’ll use the g++ compiler as the linker with the following command:

Terminal window
g++ driver.o program.o

The terminal command creates an a.out executable file by default since no name for the executable was defined. If you would like to name your executable file you can use the -o flag followed by the desired name.

Terminal window
g++ driver.o program.o -o driver

This terminal command creates a driver executable file.

During this step, the loader brings the executable file into memory, preparing it for execution. This involves allocating the necessary resources and setting up the environment so the program can run smoothly. The operating system handles this process automatically, ensuring the executable is ready to execute its instructions.

In the final step, the program is run directly from memory. The CPU executes the instructions in the executable file, producing the desired output or performing the intended tasks. To run the executable file, use the following command:

Terminal window
./a.out

Errors in C++ can occur in different forms, and understanding them is key to debugging your programs. Two common types of errors you’ll encounter are syntax errors and logic errors.

Syntax errors occur when the code violates the rules of the C++ language. These errors prevent the program from compiling and often result from issues like missing semicolons, incorrect spelling of keywords, or unmatched parenthesis. For example:

int x = 10 // Missing semicolon

Logic errors occur when the code compiles and runs but doesn’t produce the expected result. These errors are caused by incorrect logic or assumptions in the program, such as using the wrong operator or formula. For example:

if (x = 2) { // Should be x == 2
cout << "x is 2" << endl;
} else {
cout << "x is not 2" << endl;
}

Since the assignment operator is used instead of a comparison operator, x will ALWAYS equal 2.