You are on page 1of 13

Introduction to

C++

Learners Support Publications

www.lsp4you.com

Objectives of this session

What is C++
A Simple Program
Comments in C++
Output Operator
Input Operator
Cascading of I/O Operator
Structure of a C++ Program

Learners Support Publications

www.lsp4you.com

C++

C++ is an object-oriented programming


language.
Developed by Bjarne Stroustrup at
AT&T Bell Laboratories, USA in the
early 1980s.
Simule 67 + C language C++
1997 November ANSI/ISO standards
committee standardised C++.
For developing editors, compilers,
databases, communication systems, etc.

Learners Support Publications

www.lsp4you.com

A Simple Program

C++ works by giving (separate) instructions


to the computer.
These instructions can be written as functions.
The primary function used in C++ is called
main. This means that every C++ program
should have the main() function. Because a
function is an assignment, in order to perform
its job, a function has a body. The body of a
function starts with an opening curly bracket
"{" and closes with a closing curly bracket "}".

Learners Support Publications

www.lsp4you.com

A Simple Program
// my first program in
C++
#include <iostream.h>
int main ( )
{
cout << "Hello World!";
return 0;
}

Learners Support Publications

www.lsp4you.com

Hello World!

continue

Comments in C++

The most basic documentation you have to


perform is to put comments as much as you
can. Comments help you and other people
who read your code to figure out what you
were doing.
Comments are not read by the compiler
while executing your program.
C++ supports two ways to insert comments:
o
o

// line comment
/* block comment */

Learners Support Publications

www.lsp4you.com

Comments in C++

continue

// The hello.cpp program


// Include the iostream library
#include <iostream.h>
int main( )
{
/* Here is a simple sentence that I want to display
when the program starts. It doesn't do much. I am
planning to do more stuff in the future. */
cout << "Hi, this is my program!";
return 0;
}
// The end of my program
Learners Support Publications

www.lsp4you.com

Output Operator
cout << "Hi, this is my program!";
cout is a predefined object that represents
the standard output stream in C++, here
the standard output stream is screen.
scr
een

<<

cout
Object

Learners Support Publications

Insertion Operator

www.lsp4you.com

C++ variable

scr
een

Output Operator
<<

cout
Object

continue

C++ variable

Insertion Operator

The operator << is called insertion or put to


operator.
It inserts ( or sends ) the contents of the variable on
its right to the object on its left.

cout
cout

<< "Hi, this is my program!";


<< number ;

Learners Support Publications

www.lsp4you.com

Input Operator
cin >> number1;
cin is a predefined object that corresponds
to the standard input stream in C++, here
the standard input stream is keyboard.
Object

Extraction Operator

>>

cin

Keyboard

Learners Support Publications

www.lsp4you.com

C++ variable
50.55

Input Operator
Object

Extraction Operator

>>

cin

continue

C++ variable
50.55

Keyboard

The operator >> is known as extraction or


get from operator.
It extracts ( or takes ) the value from the
keyboard and assigns it to the variable on its
right.

Learners Support Publications

www.lsp4you.com

Cascading of I/O
Operators

Multiple use of << or >> in one


statement is called cascading.
eg:cout << Sum = << sum <<
\n;
cin >> number1 >> number2;

Learners Support Publications

www.lsp4you.com

Structure of a C++
Program
Include files
Class declaration
Member functions declaration
Main function program

Better to organize a program file into three


separate files.
o
Class declarations in a header file.
o
Definition of member function in a separate file.
o
The main program that uses the class is placed in
a third file which includes the previous two files.
Learners Support Publications

www.lsp4you.com

You might also like