C++
Usage of C++
• By the help of C++ programming language, we can
develop different types of secured and robust
applications:
• Window application
• Client-Server application
• Device drivers
• Embedded firmware etc
C++ Features
The #include Directive
• Preprocessor directive
• Inserts the contents of another file into the program
iostream is C++ library of input/output functions
This includes cout and cin #include <iostream>
using namespace std;
• Do not use ; at end of #include statement int main()
{
declaration(s)
statement(s)
return 0;
}
A Simple, Yet Complete, C++ Program
Program producing output only
// Hello world program Comment
#include <iostream> Preprocessor
using namespace std; directives
int main() Function
{ named
main()
cout << "Hello world!" << endl;
indicates
return 0;
start of the
} program
Ends execution
of main() which ends the
program
The cout Output Statement
• Used to display information on computer screen
• It is declared in the header file iostream
• Syntax
cout << expression;
• Uses << operator to send information to computer screen
cout << "Hello, there!";
• Can be used to send more than one item
cout << "Hello, " << "there!";
The cin Statement
• Used to read input from keyboard
• It is declared in the header file iostream
• Syntax
cin >> variable;
• Uses >> operator to receive data from the keyboard and
assign the value to a variable
• Often used with cout to display a user prompt first
• May use cin to store data in one or more variables
C++ program
#include <iostream>
#include<conio>
using namespace std;
void main() {
clrscr();
cout << "Welcome to C++ Programming.";
getch();
}
Standard output stream (cout)
#include <iostream>
using namespace std;
int main( ) {
char ary[] = "Welcome to C++ tutorial";
cout << "Value of ary is: " << ary << endl;
}
Example Program – Use of cout to prompt user to enter some data
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "What is your name? ";
cin >> name;
cout << "Hello there, " << name;
return 0;
}
• #include<iostream.h> includes the standard input
output library functions. It provides cin and cout methods
for reading from input and writing to output respectively.
• #include <conio.h> includes the console input
output library functions. The getch() function is defined in
conio.h file.
• void main() The main() function is the entry point of
every program in C++ language. The void keyword
specifies that it returns no value.
• cout << "Welcome to C++ Programming." is used to
print the data "Welcome to C++ Programming." on the
console.
• getch() The getch() function asks for a single character.
Until you press any key, it blocks the screen.
Standard input stream (cin)
#include <iostream>
using namespace std;
int main( ) {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age << endl;
}
Standard end line (endl)
The endl is a predefined object of ostream class. It is used
to insert a new line characters and flushes the stream.
#include <iostream>
using namespace std;
int main( ) {
cout << "C++ Tutorial";
cout << “ point"<<endl;
cout << "End of line"<<endl;
}
C++ Program
#include <iostream>
using namespace std;
int main() {
cout << "Hello C++ Programming";
return 0;
}
File: [Link]
Basic Data Types
the basic data types
C++ Keywords
• You cannot use it as a variable name,
C++ Keywords
• You cannot use it as a variable name,