Tech

More C++ Notes…

More C++ Notes…

  • Step 1–Define the problem to solve.
  • Step 2–Define a solution.
  • Step 3–Write a program that implements the solution.
  • Step 4–Compile the program.
  • Step 5–Link object files.
  • Step 6–Test program.
  • Step 7–Debug.

C++ files should end in ‘.cpp‘ extension to indicate a C++ source file. Ex: name.cpp

Use a C++ compiler to compile a C++ (.cpp) program. The C++ compiler sequentially goes through each source code (.cpp) file in your program & does 2 important tasks:

  • First, it checks the code to make sure it follows the rules of the C++ language. If it does not, the compiler will give you an error to help pinpoint what needs fixing. The compilation process is aborted until the error is fixed.
  • Second, it translates the C++ source code into a machine language file called an object file. Object files are typically named ‘name.o‘ or ‘name.obj‘, where name is the same name as the .cpp file it was produced from.
    • Ex: Source file–calculator.cpp –> Object file–calculator.o

IDE–Integrated Development Environment

  • a piece of software containing all of the things you need to develop, compile, link, and debug your programs.

A project is a container that holds all of your source code files, images, data files, etc, that are needed to produce an executable (or library, website, etc.) that you can run or use.

*Best Practice–Create a new project for each new program you write.

C++ (like most languages) runs line by line, from top to bottom.

Structure of a C++ program:

include libraries –> #include < iostream >

int main() { }

std::out–is the character output stream.

<< –operator followed by value to be displayed

Ex: #include < iostream >

int main ()

{

std::out << “Hello, world!”;

return 0;

}

This function produces: Hello, world!

Compile Command:

  • Using GNU, the compilation command is “g++” followed by the file name.
  • Ex: g++ helloworld.cpp –> results in a.out
  • or
  • Ex: g++ -o helloworld helloworld.cpp –> results in helloworld

Execute Command:

  • the execution command is “./” followed by the file name.
  • Ex: Here, the name of the executable file is a.out:
    • ./a.out
    • or type ./filename

In C++, single line comments are created using two consecutive forward slashes.

Ex: //This is a single line comment.

Multi-line Comments use /* to begin and */ to end.

Ex: /* This is a

multi-line comment */

4 Phases of C++ development:

  1. Code–writing the program
  2. Save–saving the program.
  3. Compile–compiling via the terminal
  4. Execute–executing via the terminal
  5. And repeat (debug the errors if needed).

C++ is a “compiled language”. That means that to get a C++ program to run, you must first translate it from a human-readable form to something a machine can “understand”. That translation is done by a program called a compiler.

A compiler can translate the C++ programs we write into machine code.

To compile a file, we need to type “g++” followed by the file name in the terminal:

g++ hello.cpp

The compiler will then translate the C++ program hello.cpp and create a machine code file called “a.out”.

To execute the new machine code file, all you need to do is type “./” and the machine code file name in the terminal:

ex: ./a.out

The executable file will then be loaded to computer memory and the computer’s CPU executes the program one instruction at a time.

Compile & Execute (Naming Executables)

Compile: Sometimes when we compile, we want to give the output executable file a specific name.

To do so, the compile command is slightly different.

Ex: g++ hello.cpp -o hello

or g++ -o hello hello.cpp

Execute: To execute the new machine code file, all you need to do is type “./” and the machine code file name in the terminal:

Ex: ./ hello

In C++ variables must be declared before they can be used!

Every variable has a type, which represents the kind of info you can store in it.

TypeUsageExamples
intinteger numbers0, 420, -7
doublefloating point numbers3.14, -200.0
charcharacters‘a’, ‘@’
stringsequence of characters“Hello World!”
booltruth valuestrue, false

Ex:

int grade;

grade = 90;

or Ex:

int grade = 90

Conditionals & Logic

‘If’ statements are used to text if an expression, or condition, is true & execute code based on it.

Ex: if (condition) { some code statement; }

‘else’ Clause–added to an ‘if’ statement to provide code that will only be executed if the condition is false.

Ex: if (condition) {

do something;

}

else {

do something else;

}

‘else if’ statements always come after the ‘if’ statement and before the ‘else’ statement. The ‘else if’ statement also takes a condition. There can be multiple ‘else if’ statements!

Ex:

if (condition) {

some code;

}

else if (condition) {

some code;

}

else {

some code;

}

Since programs with multiple outcomes are so common, C++ provides a special statement for it…the ‘switch‘ statement.

A ‘switch‘ statement provides an alternative syntax that is is easier to read & write. However, they are used less frequently than “if, else if, else” out in the wild!

Ex:

switch (grade) {

case 10:

std::cout<<“Sophmore\n”;

break;

case 11:

std::cout<<“Junior\n”;

break:

case 12:

std::cout<<“Senior\n”;

break;

default:

std::cout<<“Invalid \n”;

break;

}