Slide Set - 08
Slide Set - 08
CP & CPSA
17ME & 17PG
Contents
• Anatomy of C++ basic program (LL 04)
• Outputting with cout statement (LL 02)
• getch() function (LL 02)
• main function (LL 02)
• return statement (LL 02)
• Header files (LL 02)
• Preprocessor directives (LL 02)
• Namespace (LL 02)
LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis
Anatomy of C++ basic program
int main()
{
cout<<“Mehran University";
getch();
return 0;
}
Anatomy of C++ basic program
Output on monitor screen
Anatomy of C++ basic program
Using Namespace
using namespace std; Standard Namespace
Statement
Return Type
Function Name
main Function int main ( )
Closing Parenthesis
Opening Parenthesis
Anatomy of C++ basic program
Console Output
Insertion / put-to operator
Opening Brace {
cout Statement cout << “Mehran University” ;
Terminator
Get Character
getch();
Function String Literal
• The getch function is used to get one character from the keyboard.
• Whenever the computer encounters getch function, it will wait for the user
to press any key from the keyboard.
• Once the user presses any key on the keyboard that will be get by the getch
function.
• In this program it is used to hold the screen so that we can view the output
of the program.
getch()function
Header file which are part of C as well as C++ have the extension .h
Header files which are only the part of C++ does not have extension .h
Header files
• Header file is the file with extension.h
• It contains the declarations and definitions of the functions that can be shared
between various different source programs.
• It is so called header file because it is always written at the top (head) of the
program.
• When the compiler reads the getch() function, it does not know about how this
function works.
• All the details (definition) of getch() function are stored in conio.h header
file.
• If you want to use getch() function in your program you must include conio.h
header file.
Preprocessor Directives
Pre Before
Processor Processing
The names like cout, cin, endl can be used in either of two ways.
int main()
{
cout<<“Mehran University”<<endl;
cout<<“MUET";
getch();
return 0;
}
Namespaces
• In fully qualified name, every name will be prepended with namespace and :: (double
colon sign) as std::cout, std::cin, std::endl.
• Here we do not have to write the using namespace statement.
#include<iostream>
#include<conio.h>
int main()
{
std::cout<<“Mehran University"<<std::endl;
std::cout<<“MUET";
getch();
return 0;
}