Introduction To C++

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Programming I

(CIS-PRO-111)

Introduction to C++
Presented By:
Michael R Chinguwo
Computer Science and Information Systems Department
Malawi University of Business and Applied Sciences

:265 (0) 993 195 721 :[email protected]


Introduction
 Programs are written using different languages
 C++ is an example a programming language.
 There are generally two ways of writing programs
i. Procedural programming
ii. Object-oriented programming
i. Procedural Programming
­ In procedural programming a program is a collection of procedures (functions).
­ It is procedure centered.
­ Procedures are collections of programming statements that perform a specific
task.
Introduction (Cont’d)
ii. Object-oriented programming
­ Object-oriented programming is centered on the object.
­ An object is a programming element that contain data and procedures that
operate on the data.
 C++ can be used to write both procedural and object-oriented
programs

“Our focus is procedural programming concepts using C++”.


Program Structure
 A C++ program has the following structure

#include<iostream>
using namespace std;
int main()
{
.
.
.
return 0;
}
Program Structure (Cont’d)
#include<iostream>
 # indicates that this line is a preprocessor directive
 Preprocessor reads the program before it is compiled and only
executes those lines beginning with # symbol
 Preprocessor is like a program that prepares/sets up your source code
for the compiler.
 #include is used to include the library or header file to your program.
 The header file to be included is enclosed in <>.
­ In our case iostream is a header file.
Program Structure (Cont’d)
 The header file contains codes that is added to your source code at the
point the #include appears.
 The result of this, is a an enhanced source code (header file code +
your source code).
 The header file such as iostream contains pre-written programming
codes that the program requires to work properly
 iostream is a standard library also called standard header file.
Program Structure (Cont’d)
 iostream (input/output stream) library (header file) contains actions
for standard input (getting data from a keyboard) and standard output
(displaying data or information to a monitor).
 Actions (objects) such as cout, cin and cerr are implemented in this
file.
Program Structure (Cont’d)
Using namespace std;
 Tells the compiler to use the std namespace
 A namespace is grouping of variables, classes, etc- (Concept of OOP)
 In C++, cout, cin, endl belong to the namespace called std
 So to use cout for example, we are required to indicate a namespace as
well, so that the compiler should know where to look for cout.
 The full name for cout can be something like cout in std namespace
(which is std::cout).
Program Structure (Cont’d)
 To avoid writing long names such as std::cout, the directive using
namespace std; is used to inform the compiler that whenever it sees
short name such as cout, cin, it should check its implementation in std
namespace
Program Structure (Cont’d)
int main() and {…}
 A C++ program is composed of functions.
 A function is a portion of program that does a particular task.
 The C++ program has at least one function for it to work (run).
 This function is called main() function.
 The main() function is the first function to be executed in multi-
function program
Program Structure (Cont’d)
 An opening brace ({), marks the beginning of the function body (the
function code)
 The closing brace (}), marks the end of function.
 The int indicates that the main() function is expected to return an
integer number i.e. 0
Program Structure (Cont’d)
return 0;
 This line of code returns a number 0 when a program executes
properly hence exits normally.
 If the program does not execute properly i.e. exited abnormally, a
different number either +ve or –ve is returned.
 This line, return 0; returns an integer value because the main() started
with int (int main()).
Program Structure (Cont’d)
Use of cout
 cout is used to display the message (data) on a standard output device,
monitor
 General syntax is:
cout<< “message here”;
Note
• << is called output redirection symbol
• message here is where you have to type what to be displayed on the monitor
Program Structure (Cont’d)
#include<iostream>
using namespace std;
int main()
{
cout<< “ Welcome to C++ programming”;
return 0;
}
Program Structure (Cont’d)
 In the program, we are able to use short name cout without specifying
its namespace because of the directive using namespace std;
 If that is removed, our program will result in an error unless std::
included at the beginning of the cout
Program Structure (Cont’d)
 With using namespace std; short name  Without using namespace std;
(cout) long name (std::cout)

#include<iostream>
#include<iostream>
using namespace std;
int main()
int main()
{
{
std::cout<< “Welcome
cout<< “Welcome to
C++ programming”; to C++ programming”;
return 0; return 0;
} }
Program Structure (Cont’d)
Comments
 A comment is text that the compiler ignores but that is useful to
programmers.
 It describes what a certain portion of code does or how it is
implemented.
 There are two types of comments:
i. Single-line
ii. Block comments
Program Structure (Cont’d)
1. Single-line or line comment
­ Comments just a line or portion of it.
­ Starts with double slashes (//)
­ Example: // this is a line comment.
2. Block comment
­ Comments a line or a group of lines.
­ Starts with a slash followed by an asterisk (/*)
­ Ends with an asterisk followed by a slash (*/)
­ Example: /* this is a block comment */
Program Structure (Cont’d)
#include<iostream>
using namespace std;
int main()
{
//this is line comment
cout<<”comment example - inline”<<endl;
/*
this is
a- block comment”
*/
cout<<“comment example – block”;
return 0;
}
END

You might also like