0% found this document useful (0 votes)
8 views

C++ Notes

This document provides an overview of C++ programming, including its history, structure, and development process. It discusses the evolution from C to C++, the importance of compilers and interpreters, and the steps involved in writing, compiling, and testing C++ programs. Additionally, it highlights the significance of libraries and best practices in programming.

Uploaded by

drampley1727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C++ Notes

This document provides an overview of C++ programming, including its history, structure, and development process. It discusses the evolution from C to C++, the importance of compilers and interpreters, and the steps involved in writing, compiling, and testing C++ programs. Additionally, it highlights the significance of libraries and best practices in programming.

Uploaded by

drampley1727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C++ Notes

Chapter 1-Programs and programming languages;

Programs and programming languages.

The act (and art) of writing a program is called


programming. Programs usually require the user to to launch (or
run or execute) the program, although programs may also be
launched by other programs.

Programs are executed on the computer’s hardware, which


consists of the physical components that make up a computer.
Notable hardware found on a typical computing device includes:

1.A CPU

(central processing unit, often called the “brain” of the


computer), which actually executes the instructions. Memory,
where computer programs are loaded prior to execution.

2.Interactive devices

(e.g. a monitor, touch screen, keyboard, or mouse), which


allow a person to interact with a computer.

3.Storage devices

(e.g. a hard drive, SSD, or flash memory), which retain


information (including installed programs) even when the
computer is turned off.
Machine Language

A computer’s CPU is incapable of understanding C++.


Instead, CPUs are only capable of processing instructions
written in machine language (or machine code). The set of all
possible machine language instructions that a given CPU can
understand is called an instruction set.

Assembly Languages

An assembly language (often called assembly for short) is a


programming language that essentially functions as a more
human-readable machine language. Since CPUs do not understand
assembly language, assembly programs must be translated into
machine language before they can be executed. This translation
is done by a program called an assembler.

Compiler

Here is a simplified representation of the compiling


process:
Interpreter

An interpreter is a program that directly executes the


instructions in the source code without requiring them to be
compiled first. Interpreters tend to be more flexible than
compilers, but are less efficient when running programs.

High-level languages allow programmers to write programs


without knowing much about the platform it will be run on. This
not only makes programs easier to write, it also makes them
significantly more portable. If we’re careful, we can write a
single C++ that will compile on every platform that has a C++
compiler! A program that is designed to run on multiple
platforms is said to be cross-platform.

Rules, Best practices, and warnings



​ Rules are instructions that you must do, as required by the
language. Failure to abide by a rule will generally result in
your program not working.
​ Best practices are things that you should do, because that
way of doing things is either conventional (idiomatic) or
recommended. That is, either everybody does it that way (and if
you do otherwise, you’ll be doing something people don’t
expect), or it is generally superior to the alternatives.
​ Warnings are things that you should not do, because they
will generally lead to unexpected results.

Chapter 2-Before C++, there was C;

C
The C language was developed in 1972 by Dennis Ritchie at
Bell Telephone laboratories, primarily as a systems programming
language.

C ended up being so efficient and flexible that in 1973,


Ritchie and Ken Thompson rewrote most of the Unix operating
system using C.

In 1978, Brian Kernighan and Dennis Ritchie published a


book called “The C Programming Language”, this book, which was
commonly known as K&R.

In 1983, the American National Standards Institute (ANSI)


formed a committee to establish a formal standard for C. In 1989
(committees take forever to do anything), they finished, and
released the C89 standard, more commonly known as ANSI C. In
1990 the International Organization for Standardization (ISO)
adopted ANSI C.

In 1999, the ISO committee released a new version of C


informally named C99. C99 adopted many features which had
already made their way into compilers as extensions, or had been
implemented in C++.

C++
C++ (pronounced “see plus plus”) was developed by Bjarne
Stroustrup at Bell Labs as an extension to C, starting in 1979.
C++’s most notable innovation over C is that it supports
object-oriented programming.

​ C++ was standardized in 1998 by the ISO committee. This


means the ISO standards committee approved a document (called a
standards document) that provides a formal description of the
C++ language.

​ Five major updates to the C++ language (informally named


C++11, C++14, C++17, C++20, and C++23) have been made since
then, each adding additional functionality.
​ Because the official name of the approved standards is
complex (C++20’s formal name is ISO/IEC 14882:2020), standards
are conventionally referred to by informal names, which include
the last two digits of the year of publication (or expected
publication). For example, C++20 refers to the version of the
language published in 2020.

What is C++ good at?

​ C++ excels in situations where high performance and precise


control over memory and other resources is needed. Here are a
few types of applications that C++ would excel in:Video games,
Real-time systems (e.g. for transportation, manufacturing, etc…)
, High-performance financial applications (e.g. high frequency
trading), Graphical applications and simulations, Productivity /
office applications, Embedded software, Audio/video processing,
Artificial intelligence, neural networks. C++ also has a large
number of high-quality 3rd party libraries available, which can
shorten development times significantly.

Chapter 3-C++ development;


Step 1: Define the problem that you would like to solve

​ This is the “what” step, where you figure out what problem
you are intending to solve.

“I want to write a program that will allow me to enter many


numbers, then calculate the average.”

“I want to write a program that generates a 2d maze and lets the


user navigate through it. The user wins if they reach the end.”

“I want to write a program that reads in a file of stock prices


and predicts whether the stock will go up or down.”

Step 2: Determine how you are going to solve the problem

​ This is the “how” step, where you determine how you are
going to solve the problem you came up with in step 1.

They are straightforward (not overly complicated or confusing).

They are well documented (especially around any assumptions


being made or limitations).
They are built modularly, so parts can be reused or changed
later without impacting other parts of the program.

They can recover gracefully or give useful error messages when


something unexpected happens.

Step 3: Write the program

​ A program typed into a basic text editor would look


something like this:
1.#include <iostream>
2.
3.int main()
4.{
5. std::cout << "Here is some text.";
6. return 0;
7.}
However, we strongly urge you to use an editor that is
designed for programming (called a code editor). Don’t worry if
you don’t have one yet. We’ll cover how to install a code editor
shortly.
Line numbering. Line numbering is useful when the compiler
gives us an error, as a typical compiler error will state: some
error code/message, line 64. Without an editor that shows line
numbers, finding line 64 can be a real hassle.

Syntax highlighting and coloring. Syntax highlighting and


coloring changes the color of various parts of your program to
make it easier to identify the different components of your
program.

An unambiguous, fixed-width font (often called a “monospace


font”). Non-programming fonts often make it hard to distinguish
between the number 0 and the letter O, or between the number 1,
the letter l (lower case L), and the letter I (upper case i). A
good programming font will ensure these symbols are visually
differentiated in order to ensure one isn’t accidentally used in
place of the other. All code editors should have this enabled by
default, but a standard text editor might not. Using a
fixed-width font (where all symbols have the same width) makes
it easier to properly format and align your code.

Many simple C++ programs only have one source code file,
but complex C++ programs can have hundreds or even thousands of
source code files.

Each source code file in your program will need to be saved


to disk, which means each source code file needs a filename. C++
does not have any requirements for naming files. However, the
de-facto standard is to name the first/primary source file
created for a program main.cpp. The filename (main) makes it
easy to determine which is the primary source code file, and the
.cpp extension indicates that the file is a C++ source code
file.

You may occasionally see the first/primary source code file


named after the name of the program instead (e.g.
calculator.cpp, poker.cpp). You may also occasionally see other
extensions used (e.g. .cc or .cxx).

Step 4: Compiling your source code.


​ First, the compiler checks your C++ code to make sure it
follows the rules of the C++ language. If it does not, the
compiler will give you an error (and the corresponding line
number) to help pinpoint what needs fixing. The compilation
process will also be aborted until the error is fixed.

Second, the compiler translates your C++ code into machine


language instructions. These instructions are stored in an
intermediate file called an object file. The object file also
contains other data that is required or useful in subsequent
steps (including data needed by the linker in step 5, and for
debugging in step 7).

Object files are typically named name.o or name.obj, where
name is the same name as the .cpp file it was produced from.

Step 5: Linking object files and libraries and creating the


desired output file.

​ First, the linker reads in each of the object files


generated by the compiler and makes sure they are valid.

Second, the linker ensures all cross-file dependencies are


resolved properly. For example, if you define something in one
.cpp file, and then use it in a different .cpp file, the linker
connects the two together. If the linker is unable to connect a
reference to something with its definition, you’ll get a linker
error, and the linking process will abort.
Third, the linker typically links in one or more library
files, which are collections of precompiled code that have been
“packaged up” for reuse in other programs.

Finally, the linker outputs the desired output file.


Typically this will be an executable file that can be launched
(but it could be a library file if that’s how you’ve set up your
project).

The standard library.

​ C++ comes with an extensive library called the C++ Standard


Library (usually called “the standard library”) that provides a
set of useful capabilities for use in your programs. One of the
most commonly used parts of the C++ standard library is the
Input/Output library (often called “iostream”), which contains
functionality for printing text on a monitor and getting
keyboard input from a user.

3rd party libraries.

​ You can optionally link third party libraries, which are


libraries that are created and distributed by independent
entities (rather than as part of the C++ standard). For example,
let’s say you wanted to write a program that played sounds. The
C++ standard library contains no such functionality. While you
could write your own code to read in the sound files from disk,
check to ensure they were valid, or figure out how to route the
sound data to the operating system or hardware to play through
the speaker -- that would be a lot of work! Instead, you’d be
more likely to find some existing software project that has a
library that already implements all of these things for you.

Building.

​ Because there are multiple steps involved, the term


building is often used to refer to the full process of
converting source code files into an executable that can be run.
A specific executable produced as the result of building is
sometimes called a build.

Step 6: Testing and Debugging

​ Once you can run your program, then you can test it.
Testing is the process of assessing whether your software is
working as expected. Basic testing typically involves trying
different input combinations to ensure the software behaves
correctly in different cases.

Integrated development environments (IDEs)

​ Note that steps 3, 4, 5, and 7 all involve software


programs that must be installed (editor, compiler, linker,
debugger). While you can use separate programs for each of these
activities, a software package known as an integrated
development environment (IDE) bundles and integrates all of
these features together.

You might also like