You are on page 1of 4

CLASS 7 : C++ NOTES (Compiled by: Computer Dept.

-Main Branch CBS)

INTRODUCTION TO COMPUTER LANGUAGES

Modern computers are incredibly fast, and getting faster all the time. Yet with this speed comes some
significant constraints. Computers only natively understand a very limited set of instructions, and must
be told exactly what to do. A program (also commonly called an application or software) is a set of
instructions that tells the computer what to do. The physical computer machinery that executes the
instructions is the hardware.

MACHINE LANGUAGE

A computer’s CPU is incapable of speaking C++. The very limited set of instructions that a CPU natively
understands is called machine code (or machine language or an instruction set). How these
instructions are organized is beyond the scope of this introduction, but it is interesting to note two things.
First, each instruction is composed of a number of binary digits, each of which can only be a 0 or a 1.
These binary numbers are often called bits (short for binary digit).

Second, each set of binary digits is translated by the CPU into an instruction that tells it to do a very
specific job, such as compare these two numbers, or put this number in that memory location. Different
types of CPUs will typically have different instruction sets and the programmers had to write programs
directly in machine language, which was a very difficult and time consuming thing to do.

ASSEMBLY LANGUAGE

Because machine language is so hard to program with, assembly language was invented. In an assembly
language, each instruction is identified by a short name (rather than a set of bits), and variables can be
identified by names rather than numbers. This makes them much easier to read and write. However, the
CPU cannot understand assembly language directly. Instead, it must be translated into machine language
by using an assembler. Assembly languages tend to be very fast, and assembly language is still used
today when speed is critical.

HIGH-LEVEL LANGUAGES

To address these concerns, high-level programming languages were developed. C, C++, Pascal, Java,
JavaScript, and Perl, are all high level languages. High level languages allow the programmer to write
programs without having to be as concerned about what kind of computer the program is being run on.
Programs written in high level languages must be translated into a form that the CPU can understand
before they can be executed. There are two primary ways this is done: compiling and interpreting.

A COMPILER is a program that reads code and produces an executable program that the CPU can
understand directly. Once our code has been turned into an executable, we do not need the compiler to
run the program.

An INTERPRETER is a program that directly executes our code without compiling it into machine code
first. Interpreters tend to be more flexible, but are less efficient when running programs because the
interpreting process needs to be done every time the program is run. This means the interpreter is needed
every time the program is run.

INTRODUCTION TO C++

The C language was developed in 1972 by Dennis Ritchie at Bell Telephone laboratories, primarily as a
systems programming language. That is, a language to write operating systems with. 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 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 adopted ANSI C (with a few minor modifications). This version of C became known as
C90.

In 1999, the ANSI committee released a new version of C called C99. It 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++ adds many new features to the C language, and is perhaps best thought of as a
superset of C, though this is not strictly true as C99 introduced a few features that do not exist in C++.
1
INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)

An Integrated Development Environment (IDE) contains all of the things we need to develop,
compile, link, and debug our programs. Once our IDE is installed, we are ready to write our first program.

Compiling our first program

Although our programs will be written inside .cpp files, the .cpp files themselves will be added to
a PROJECT. The project stores the names of all the code files we want to compile, and also saves various
IDE settings. Every time we reopen the project, it will restore the state of the IDE to where we left off.
When we choose to compile our program, the project tells the compiler and linker which files to compile
and link.

A QUICK NOTE ABOUT EXAMPLES CONTAINING CODE

Starting with this lesson, we will see many examples of C++ code presented. Most of these examples
will look something like this:

#include <iostream.h>
#include <conio.h>

int main()
{
cout << "Hello world!" << endl;
return 0;
}

STRUCTURE OF A PROGRAM

Computer program is a sequence of instructions that tell the computer what to do.

STATEMENTS

The most common type of instruction in a program is the statement. A statement in C++ is the smallest
independent unit in the language. In human language, it is analogous to a sentence. We write sentences
in order to convey an idea. In C++, we write statements in order to convey to the compiler that we want
to perform a task. Statements in C++ are often terminated by a semicolon.

EXPRESSIONS

The compiler is also capable of resolving expressions. An expression is a mathematical entity that
evaluates to a value. For example, in math, the expression 2+3 evaluates to the value 5. Expressions
can involve values (such as 2), variables (such as x), operators (such as +) and functions (which return
an output value based on some input value). They can be singular (such as 2, or x), or compound (such
as 2+3, 2+x, x+y, or (2+x)*(y-3)).

For example, the statement x = 2 + 3; is a valid assignment statement. The expression 2 + 3 evaluates
to the value of 5. This value of 5 is then assigned to x.

A LIBRARY is a collection of precompiled code (e.g. functions) that has been “packaged up” for reuse in
many different programs. Libraries provide a common way to extend what our programs can do. For
example, if we were writing a game, we’d probably want to include a sound library and a graphics library.

Taking a look at a sample program


Now that we have a brief understanding of what statements, functions, and libraries are, let’s look at a
simple “hello world” program:

2
#include <iostream.h>
#include <conio.h>

int main()
{
cout << "Hello world!" << endl;
return 0;
}

Line 1 is a special type of statement called a PREPROCESSOR DIRECTIVE. Preprocessor directives tell
the compiler to perform a special task. In this case, we are telling the compiler that we would like to add
the contents of the iostream header to our program. The iostream header allows us to access functionality
from the iostream library, which will allow us to write text to the screen.

Line 2 is blank, and is ignored by the compiler.

Line 3 declares the main() function, which as we learned above, is mandatory. Every program must have
a main() function.

Lines 4 and 7 tell the compiler which lines are part of the main function. Everything between the opening
curly brace on line 4 and the closing curly brace on line 7 is considered part of the main() function.

Line 5 is our first statement (we can tell it’s a statement because it ends with a semicolon), and it is an
output statement. cout is a special object that represents

the console/screen. The << symbol is an operator (much like + is an operator in mathematics) called
the OUTPUT OPERATOR. cout understands that anything sent to it via the output operator should be
printed on the screen. In this case, we’re sending it the text “Hello world!”.

Line 6 is a new type of statement, called a return statement. When an executable program finishes
running, the main() function sends a value back to the operating system that indicates whether it was
run successfully or not.

This particular return statement returns the value of 0 to the operating system, which means “everything
went okay!”. Non-zero numbers are typically used to indicate that something went wrong, and the
program had to abort. We will discuss return statements in more detail when we discuss functions.

All of the programs we write will follow this template, or a variation on it. We will discuss each of the lines
above in more detail in the upcoming sections.

SYNTAX AND SYNTAX ERRORS

In English, sentences are constructed according to specific grammatical rules. For example, normal
sentences end in a period. The rules that govern how sentences are constructed in a language is
called syntax.

C++ has a syntax too: rules about how our programs must be constructed in order to be considered
valid. When we compile our program, the compiler is responsible for making sure our program follows
the basic syntax of the C++ language. If we violate a rule, the compiler will complain when we try to
compile our program, and issue we a syntax error.

TYPES OF COMMENTS

A comment is a line (or multiple lines) of text that are inserted into the source code to explain what the
code is doing. In C++ there are two kinds of comments.

The // symbol begins a C++ single-line comment, which tells the compiler to ignore everything to the
end of the line.

The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the symbols
is ignored.

As noted in previous sections, the cout object (in the iostream library) can be used to output text to the
console. As a reminder, here’s our Hello world program:

3
Sample Program 1:

#include <iostream.h>
#include <conio.h>

int main()
{
string s;
s = "This is a test";
cout << s << endl;
system("PAUSE");
return 0;
}

Sample Program 2:

#include <iostream.h>
#include <conio.h>

int main()
{
int n=0;
cout<<"hi..CBS"<<endl;
cout<<"enter 1st number =";
cin>>n;
cout<<"the number ="<<n<<endl;
system("PAUSE");
return 0;
}

You might also like