You are on page 1of 17

C++

INTRODUCTION
Charles saah
What is C++
• General purpose of a C++ programming language
• Used to create computer programs
• Used to develop many applications
• Client side scripting language
• Derived from C and largely based on it
• It is also objected oriented OOP
What is a C++ program
• It is a collection of # include <iostream>
commands or using namespace std;
statements
int main()
{
• On the right hand side is
cout<< “Hello World”<<endl;
a simple code that has
“Hello World” as output return 0;
}
What is a C++ compiler
• A compiler is a program that converts our code into machine language
• The compiler ignores blank lines
• Blank line improves code readability
• White spaces, tabs, new lines are all ignore although they help
improve readability
Explanation
• C++ contains many headers
• Each contain information needed for the program to work properly
• #include <iostream>
• The # sign is a compiler’s preprocessor
• So # include tells the preprocessor to include <iostream> header
• The <iostream> defines the standard that inputs and outputs data
Explanation cont.
• In our code
• using namespace std
• Tells the compiler to use the standard (std) namespace
• The standard namespace include features of C++ standard library
• ------------ namespace ----
• Who will complete the above for us
Program execution
• Program execution begins with the main • # include <iostream>
function int main()
• using namespace std;
• Every program must therefore have ONE
main function • int main()
• The open and closed curly braces { } •{
• Indicates the beginning and end of the • cout<< “Hello World”<<endl;
function • return 0;
• Sometimes called body or block
•}
Question
• What is the starting point of your C++ program ?
• Possible suggestion
a. First line
b. iostream
c. main function
d. using namespace std
Explanation cont.
• The next line
• cout<<“Hello World”; will display “Hello World” to the screen.
• In C++ streams are used to perform input and output operations
• cout is used in combination with the insertion operator <<
• The operator inserts the data that comes after it into the stream that
comes before it
• A semicolon ; is used to terminate a statement.
• This indicates the end of one logical statement
Quick quiz
• Each instruction must end with
a. Colon :
b. Semicolon ;
c. Comma ,
d. Full stop .
e. endl
What is a block
• This is a set of logically connected statements surrounded by opening
and closing curly braces
• Example
•{
• cout<< “C++ is fun”;
•}
• You can have multiple statement in a block
Explanation cont.
•{
• cout<< “C++ is fun”;
• Cout<<“Liverpool are Champions after 30 odd years of agony”;
•}
• When we execute the code the output of the second cout will be
added to the first
• C++ is fun Liverpool are Champions after 30 odd years of agony
Block cont.
• To correct this we add endl. There should be the insertion operator <<
before the endl
•{
• cout<< “C++ is fun” <<endl;
• cout<<“Liverpool are Champions after 30 odd years of agony”;
•}
• endl means that the output of the next cout(one below the first)
• Should go to the line beneath it
• Output
• C++ is fun
• Liverpool are Champions after 30 odd years of agony
Quick quiz
• You want to output the statement C++ is fun completely fill in the blank
• Fill in the blank
• ------- -- “C++ is fun completely fill in the blank”;
Explanation cont
• We saw the code on the right hand side on • # include <iostream>
slides 2 and 7 • using namespace std;
• return 0 means that the code has run or
executed successfully • int main()
• If we ignore or don’t include it the system •{
automatically returns 0 • cout<< “Hello
• You can return any integer eg. 1, 2, -7 World”<<endl;
• return 0;
•}
Quick quiz
• Rearrange the following codes in the right order
a. {
b. }
c. int main()
d. Cout<<“Kolomashi trending ”;
Software – integrated environment
• We will use code block
• However you can download any IDE of your choice, just make sure it works
• There is Dev C++ which is also very good
• With this knowledge of C++ write a program entitled myself
• Your program should display an essay about yourself
• Each statement must be on a separate line
• 5 lines minimum and 6 lines maximum
• Good luck
• cs103ait@gmail.com

You might also like