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

Lecture 1 Notes: Introduction 1 Compiled Languages and C++

C++ is a compiled, high-level programming language that provides concise and maintainable code through features like object orientation. The compilation process involves translating source code files into object files using a compiler, then linking the object files together along with library code using a linker to produce an executable program. This lecture introduces C++ and explains the basic compilation process, noting that C++ code is preprocessed, compiled, then linked ahead of time to produce fast executable programs. It concludes by presenting a simple "Hello World" program to demonstrate basic C++ code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lecture 1 Notes: Introduction 1 Compiled Languages and C++

C++ is a compiled, high-level programming language that provides concise and maintainable code through features like object orientation. The compilation process involves translating source code files into object files using a compiler, then linking the object files together along with library code using a linker to produce an executable program. This lecture introduces C++ and explains the basic compilation process, noting that C++ code is preprocessed, compiled, then linked ahead of time to produce fast executable programs. It concludes by presenting a simple "Hello World" program to demonstrate basic C++ code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to C++ January 4, 2011

Massachusetts Institute of Technology 6.096

Lecture 1 Notes: Introduction

1 Compiled Languages and C++

1.1 Why Use a Language Like C++?


At its core, a computer is just a processor with some memory, capable of running tiny
instructions like “store 5 in memory location 23459.” Why would we express a program as
a text file in a programming language, instead of writing processor instructions?
The advantages:
1. Conciseness: programming languages allow us to express common sequences of com­
mands more concisely. C++ provides some especially powerful shorthands.
2. Maintainability: modifying code is easier when it entails just a few text edits, instead
of rearranging hundreds of processor instructions. C++ is object oriented (more on
that in Lectures 7­8), which further improves maintainability.
3. Portability: different processors make different instructions available. Programs writ­
ten as text can be translated into instructions for many different processors; one of
C++’s strengths is that it can be used to write programs for nearly any processor.
C++is ahigh-level language: when you write a program in it, the shorthands are sufficiently
expressive that you don’t need to worry about the details of processor instructions. C++ does
give access to some lower­level functionality than other languages (e.g. memory addresses).

1.2 The Compilation Process

A program goes from text files (or source files) to processor instructions as follows:
Source File Compiler Object File
Linker OS
Executable Program in Memory
Compiler Libraries
Source File Object File

Object files are intermediate files that represent an incomplete copy of the program: each
source file only expresses a piece of the program, so when it is compiled into an object file,
the object file has some markers indicating which missing pieces it depends on. The linker
takes those object files and the compiled libraries of predefined code that they rely on, fills
in all the gaps, and spits out the final program, which can then be run by the operating
system (OS).
The compiler and linker are just regular programs. The step in the compilation process in
which the compiler reads the file is called parsing.
In C++, all these steps are performed ahead of time, before you start running a program.
In some languages, they are done during the execution process, which takes time. This is
one of the reasons C++ code runs far faster than code in many more recent languages.
C++ actually adds an extra step to the compilation process: the code is run through a
preprocessor, which applies some modifications to the source code, before being fed to the
compiler. Thus, the modified diagram is:
Source File P rep ro ces s o r P ro cess ed Co d e Compiler Object File
Linker OS
E x ecut abl e Pro g ram i n M em o ry
P rep ro ces s o r Compiler Libraries
Source File P ro cess ed Co d e Object File

1.3 General Notes on C++


C++ is immensely popular, particularly for applications that require speed and/or access
to some low­level features. It was created in 1979 by Bjarne Stroustrup, at first as a set
of extensions to the C programming language. C++ extends C; our first few lectures will
basically be on the C parts of the language.
Though you can write graphical programs in C++, it is much hairier and less portable than
text­based (console) programs. We will be sticking to console programs in this course.
Everything in C++ is case sensitive: someName is not the same as SomeName.

2 Hello World
In the tradition of programmers everywhere, we’ll use a “Hello, world!” program as an entry
point into the basic features of C++.

2.1 The code


1 // A Hello World program
2 #in clu de <iostream >
3

You might also like