Tech

Computer Science (CompSci) 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++ (C plus plus) files should end in a “.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 and 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 translate the C++ source code into a machine language file called an object file. Object files are typically named “named.o” or “name.obj”, where “name” is the same name as the “.cpp” file it was produced from. (ex: If source file is calculator.cpp, then the corresponding object file would be 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 is to create a new project for each new program you write.

C++ (like most languages) runs line by line, from top to bottom. We call this “sequentially”.

Structure of a C++ program: include libraries —> # include < iostream >

int main() { ***insert what the program does here between brackets*** } (The “int main()” is the main() function, the first “{“ bracket is the beginning of a function, and “}” is the end of the function.)

std::cout (is the character output stream)

<< operator followed by value to be displayed

ex:

#include < iostream >

int main() { std::cout << “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 would be a 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
    • 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 them be loaded into 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.

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

ex: (type) (name) int grade; grade = 90;

or ex: (type) (name) int grade = 90; (type) (int) (value)

Conditionals & Logic

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

ex: if (condition) { some code statement here; }

‘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 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;

}