0% found this document useful (0 votes)
49 views14 pages

4 - A C++Program

The document discusses the basics of a C++ program, including: 1) A C++ program contains functions, one of which must be the main function. Functions accomplish tasks and some are predefined. 2) The main function is the starting point of a C++ program. Libraries provide common functions and symbols through header files included using #include directives. 3) Input/output streams like cout and cin are used to display output and get input. cout displays to the console and cin extracts input from the keyboard.

Uploaded by

rababahh28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views14 pages

4 - A C++Program

The document discusses the basics of a C++ program, including: 1) A C++ program contains functions, one of which must be the main function. Functions accomplish tasks and some are predefined. 2) The main function is the starting point of a C++ program. Libraries provide common functions and symbols through header files included using #include directives. 3) Input/output streams like cout and cin are used to display output and get input. cout displays to the console and cin extracts input from the keyboard.

Uploaded by

rababahh28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

4- A C++ program

1
The Basics of a C++ Program
 A C++ program is a collection of functions, one of which is the function
main.
 A function is a set of statements whose objective is to accomplish
something.
 Some functions, called predefined or standard functions, are already
written and provided as part of the system.
 But to accomplish most tasks, programmers must learn to write their own
functions.
 if a C++ program has only one function, it must be the function main.
The function in C++ are 2 types:
1) pre-defined function in C++ compiler.
2)user-defined function by programmer.

2
Output console

#include <iostream> Hello world


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

Input
console
 Programming language: A set of rules, symbols, and special words.
 Many of the functions and symbols needed to run a C++ program are provided as a
collection of libraries.
 Every library has a name and is referred to by a header file.
 Preprocessor directives are processed by a program called a preprocessor .
 All preprocessor commands begin with #.

3
Preprocessor Directives (cont’d.)

• Syntax to include a header file:

• For example:
#include <iostream>
– Causes the preprocessor to include the header file
iostream in the program
• Preprocessor commands are processed before the
program goes through the compiler

4
The tracing for any C++ program starts always from main function (starting
point of a C++ program).
cout: >>
it is an object used to print on monitor
console out .
>> : angular brackets is called the insertion operator.
cout is used together with the insertion operator.
endl causes the insertion point to move to the beginning of the next line.

 cin<< : console in for input from keyboard.


cin is used together with the extraction operator >>

 cout and cin are objects in the iostream library.


5
Structure of all c++ programs
# include >iostream<
int main( )
{ std :: cout >>"hello world";
std :: cout >>"welcome to c++";
std :: cout >>"Iam a programmer";
return 0 ;
}
std : is a name space found on iostream library.

every time you use cout , you have to write:


std :: cout >>’’………’’ ;
to make it easier ,you can write the statement :
using name space std ;
At the beginning after # include >iostream< and remove the std :: before cout .
6
# include >iostream<
using namespace std;
int main( )
{cout >>"hello world" ;
cout >>"welcome to c++" ;
cout >>"Iam a programmer";
return 0 ;
}
**each instruction mostly must end with ;
The structure of all c++ programs is :
# include >iostream<
using namespace std;
int main( )
{ ----------;
-----------;
return 0 ;}

7
A C++ Program
Example:
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
cout << "The sum of 2 and 3 = " << 5 << endl;
cout << "7 + 8 = " << 7 + 8 << endl;
return 0;
}

Sample Run:
My first C++ program.
The sum of 2 and 3 = 5
7 + 8 = 15

8
Output (cont’d.)

• A manipulator is used to format the output


– Example: endl causes insertion point to move
to beginning of next line

9
Output (cont’d.)

• The new line character is '\n'


– May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
Output:
Hello there.My name is James.
cout << "Hello there.\n";
cout << "My name is James.";
Output :
Hello there.
My name is James.

10
Output (cont’d.)

11
How to get data from keyboard:
program performs three basic operations: it gets data, it
manipulates the data, and it outputs the results.
The syntax of an input statement using cin and the
extraction operator >> is:
for one variable: cin >> variable ;

for multiple variables: cin >> variable >>


variable...;

12
The Basics of a C++ Program
 Comments are for the user; they typically explain the purpose of the
programs, Single-line comments begin with // and can be placed anywhere
in the line. Everything encountered in that line after // is ignored by the
compiler
 /* You can include comments that can occupy several lines */

 you can add comment by writing // or /*block*/ .

// : line comment , you can write a comment at the same line.


/*block comment*/ : block comment to write a comment on a line
or more .

13
EX.: #include <iostream>
using namespace std;
int main()
{ // cout<<"hello"<<endl;
return 0;
}

What is the output ? No output , the cout statement is a comment , and no


problem if it is written in a wrong way .

14

You might also like