Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Mbeya University of Science and

Technology
EEB 3107: Computer Programming
Academic year :2020/2021

Facilitator: Mr. Kyambille G


Grading System

Exam #1 ( Week 6/7)


Exam #2 (week 12/13)

Course Work (40%)

University Examination (60%)

EEB 3107_MUST 2
Introduction

C++ has borrowed many features from other programming


language particularly from C. Programmers argue that C++ is an
extension of C, providing more and powerful programming
techniques. It introduces Object Oriented concepts over C
programming language. Structured programming concepts found
in C are found in a more powerful form in C++. Such features
include: function overloading, single line comment and function
template. Generally, Programming in C++ improves productivity
of the programmer.

EEB 3107_MUST 3
Introduction

 C is a programming language developed in the 1970's


alongside the UNIX operating system.
 C provides a comprehensive set of features for handling a
wide variety of applications, such as systems development
and scientific computation.
 C++ is an “extension” of the C language, in that most C
programs are also C++ programs.
 C++, as opposed to C, supports “object-oriented
programming.”
Basics of a Typical C++ Environment

Phases of C++ Programs: Editor Disk


Program is created in
the editor and stored
on disk.
1. Edit Preprocessor Disk Preprocessor program
processes the code.
2. Preprocess Compiler creates
Compiler Disk object code and stores
3. Compile it on disk.
Linker links the object
4. Link Linker Disk code with the libraries,
creates an executable
file and stores it on disk
5. Load Primary
Memory
Loader

6. Execute Loader puts program


in memory.
Disk ..
..
..

Primary
Memory
CPU
  CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program
5 ..
executes.
Basics of a Typical C++ Environment

 Common Input/output functions


 cin

• Standard input stream


• Normally keyboard
 cout

• Standard output stream


• Normally computer screen
 cerr

• Standard error stream


• Display error messages

6
A Simple Program: Printing a Line of Text

 Before writing the programs


 Comments

• Document programs
• Improve program readability
• Ignored by compiler
• Single-line comment
– Use C’s comment /* .. */ OR Begin with // or
 Preprocessor directives
• Processed by preprocessor before compiling
• Begin with #

7
Moving from C to C++

In C Programming language, the standard library function


printf() statement sends the output to the standard output
device, the console. The function body consists of statements
for creating data storage called local variables and executable
statements. Note that local variables in main() are not visible
by any other function. Recall the traditional C programme called
Hello World, in example below.

EEB 3107_MUST 8
Example

/* Hello.c: printing Hello World message */


#include<stdio.h>
main( )
{
printf(“Hello World”);
}

EEB 3107_MUST 9
Running the program it outputs the statement:

Hello World

However, the program could be rewritten using C++ streams.


The C++ equivalent of the Hello World program is given in
example below in the program hello.cpp.

EEB 3107_MUST 10
Example

// hello.cpp: printing Hello World message

#include <iostream.h>

main()

cout << “Hello World”;

EEB 3107_MUST 11
Running the program it outputs the statement: 

Hello World

The header file iostream.h supports streams programming features


by including predefined stream objects.

The character “<<” is called the C++ stream insertion operator. It


sends the message “Hello World” to the predefined console object
cout, which in turn prints on the console. The Hello World program
in example 1.2 is again shown below for the purpose of discussion.
 

EEB 3107_MUST 12
1. // hello.cpp: printing Hello World message comment
 
2. #include <iostrem.h> preprocessor directive
3. main() function declarator.
4. { beginning of the program
5. cout << “Hello World”; body of the function main
6. } end of the program

Various components of the program hello.cpp, shown above, are


interpreted as follows:

EEB 3107_MUST 13
First Line: Comment Line
 

The statement which starts with symbols “//” (i.e, two slash
characters one after another without a space) is treated as a
comment. Hence, the compiler ignores the complete line
starting from the // character pair.

The file name extension .cpp, in the program hello.cpp, is an


acronym for C PlusPlus (C++). The compiler will recognize a
program as a C++ program only when it has an extension .cpp.

EEB 3107_MUST 14
Second Line: Preprocessor Directive

 The second line is a preprocessor directive. The


preprocessor directive #include<iostream.h> includes all the
statements of the header file iostream.h. It contains
instructions and predefined constants that will be used in
the program. It plays a role similar to that of the header
file stdio.h of C.

EEB 3107_MUST 15
Third Line: Function Declarator

The third line in the program is main()


Similar to a C program, the C++ program also consists of a set of
functions. Every C++ program must have one function with name main,
from where the execution of the program begins. The name main is a
special word (not a reserved word) but must not be invoked anywhere
by the user. The names of the functions (except main) are given by
the programmer. The function name is followed by a pair of
parentheses which may or may not contain arguments. In this case,
there are no arguments, but still the parentheses pair is mandatory.
Every function is supposed to return a value, but the function in the
example does not return any value. In other compilers, such function
names must be preceded by the reserved word void.
EEB 3107_MUST 16
Fourth Line: Function Begin
 
The function body in a C/C++ program is enclosed between two
flower brackets. The opening flower bracket ({) marks the
beginning of a function. All the statements in a function, which
are listed after this brace can either be executable or non-
executable statements.

EEB 3107_MUST 17
Fifth Line: Body of a Function

The function body contains a statement to display the message Hello


World.
The output statement cout is pronounced as C-out (meaning Console
Output).
It plays a role similar to that of the printf() in C.
cout << “Hello World”;
prints the message “Hello World” on the standard console output
device i.e. Video Display Unit (VDU) by default). It plays the role of
the statement.

printf(“Hello World”);

as in the hello. c program.

EEB 3107_MUST 18
Sixth Line: Function End

Just like it is in C, the end of a function body in a C++ program is


marked by the closing flower bracket (}).

When the compiler encounters this bracket, it knows it has


reached the end of the program and transfers control to a caller.

In this program, the last line actually marks the end of program
and control is transferred to the operating system on termination
of the program.

EEB 3107_MUST 19
Compilation Process

The C++ program hello.cpp can be entered into the system using
any available text editor. The program coded by the programmer
is called the source code. This source code is supplied to the
compiler for converting it into the machine code.

C++ programs make use of libraries. A library contains the object


code of standard functions. The object code of all functions used
in the program have to be combined with the program written by
the programmer. In addition, some start-up code is required to
produce an executable version of the program. This process of
combining all the required object codes and the start-up code is
EEB 3107_MUST 20
Streams Based I/O

C++ supports a rich set of functions for performing input and


output operations. The syntax of using these I/O functions is
totally consistent, irrespective of the device with which I/O
operations are performed. C++’s new features for handling I/O
operations are called streams. Streams are abstractions that
refer to data flow. Streams in C++ are classified into

Output Streams

Input Streams.

EEB 3107_MUST 21
Output Streams

The output streams allow to perform write operations on output


devices such as screen, disk, etc. Output on the standard stream
is performed using the cout object. The syntax for the standard
output stream operation is as follows:

cout << variable;

the word cout is followed by the symbol <<, called the insertion or
put-to operator, and then with the items
(variables/constants/expressions) that are to be output.
Variables can be of any basic data type.
 
The following are examples of stream output operations:

EEB 3107_MUST 22
Output Streams

cout << “Hello World”;


int age;
cout << age;
float weight;
cout << weight;
double area;
cout << area;
char letter;
cout << letter;
More than one item can be displayed using a single cout output
stream object. Such output operations in C++ are called cascaded
output operations. For example, output of the age of a person
along with some message can be performed by cout as follows:

EEB 3107_MUST 23
Output Streams

cout << “Age = “ << age;

The cout object will display all the items from left to right.
Hence, in the above case, it prints the massage string “Age =”
first, and then prints the value of the variable age. C++ does not
enforce any restrictions on the maximum number of items to
output. The complete syntax of the standard output streams
operation is as follows:

cout << variable << variable2 << .. << variableN;


 
The object cout must be associated with at least one argument.
Like printf( ), a constant value can also be sent as an argument to
the cout object. Following are some valid output statements:

EEB 3107_MUST 24
Example

// read.cpp: data input through cin object


#include <iostream.h>
main()
{
char name[25];
int age;
char address[25];

// read data
cout << “Enter Name: ”;
cin >> name;
cout << “Enter age: “;
cin >> age;
cout << “Enter Address: “;
cinEEB 3107_MUST
>> address; 25
Example

//Output data

cout << “The data entered are:” << endl;

cout << “Name is “ << name << endl;

cout << “Age is “ << age << endl;

cout << “Address is “ << address;

EEB 3107_MUST 26
Running this program the output will be:

Enter Name: Godphrey

Enter Ange: 24

Enter Address: MUST Box 131 MBEYA

The data entered are:

Name is Godphrey

Age is 24

Address is MUST Box 131 MBEYA

EEB 3107_MUST 27
Thank you for Listening! Questions

EEB 3107_MUST 28

You might also like