C++
Fundamentals
Lecture 4
First Program in C++
• #include <iostream>
• using namespace std;
• int main()
•{
• cout << “Welcome to C++";
•}
Preprocessor Directive
Preprocessor Directive
• # is called Preprocessor Directive.
• The # line tells the compiler to use a file
<iostream.h> or <stdio.h> or whatever written in
<angle brackets>.
• Files having .h extension in C/C++ are called header
files. They are also sometimes called include files.
• The iostream.h file is included in the program as it
contains the information about the “cout” identifier
and the << operator.
• Normally all the header files of C/C++ are present in
the INCLUDE directory
Main Function
Main Function
• A C/C++ program may consist of many functions,
classes and other program elements, but on
startup, control always goes to main() function.
• The first statement executed by the C/C++
compiler will be the one that is the first statement
in function
void main(void)
or
void(main)
or
main()
Statements
Using Comments in the Program
// It is a C++ Program
#include <iostream.h>
void main(void) //main function
{
/* These lines
are the
part of comments and
will not execute
*/
cout<<"We are studying C++";
}
Using Comments in the Program
// It is a C++ Program
#include <iostream.h>
void main(void) //main function
{
/* These lines
are the
part of comments and
will not execute
*/
cout<<"We are studying C++";
}
Using Comments in the Program
There are two ways of specifying comments in C++.
Using
• i) //
• ii) /* and */
Task 1
• Lets Write a Program to Display two integer numbers .
• Lets Write a Program that introduce two variables, value of
first one is 5 and the next one is 3. Display these two
integer numbers.
• Write a Program that declares two integer variables and
display its sum.
• Write a Program that declares two integer variables and
add them and store its result in 3rd variable.
TASK 1. What will be the output of the
following code?
#include<iosteream.h>
void main(main)
{
int a;
int b;
int c;
a = 10;
b = 5;
c= a + b;
cout<<“Sum is "<<c <<endl;
}
TASK 2. What will be the output of the
following code?
#include<iosteream.h>
void main(void)
{
int a;
int b;
int c;
a = 10.1;
b = 5;
cout<<“Sum is "<<c <<endl;
c= a + b;
}
Displaying two integer numbers
#include <iostream.h>
Output
void main(void) A is 10
B is 15
{
int a=10;
int b=15;
cout<<"A is "<<a <<endl;
cout<<"B is "<<b <<endl; For next
line (endl)
}
Task 2
• Lets write a program to add two integers and display its
result in third integer number.
• SUM TWO NUMBERS
Defining and using Integer Variables
#include <iostream.h>
Output
void main(void) Sum is 15
{
int a;
int b;
int c;
a = 10;
b = 5;
c= a + b;
cout<<“Sum is "<<c <<endl;
}
Task 3
• Lets write a program to subtract , divide , multiply two integers
and display its result in third integer number.
Method 1
#include <iostream>
using namespace std;
int main()
{
int num1 = 240;
int num2 = 40;
int num3;
num3=num1+num2;
cout<<“Result is "<<num3<<endl;
num3=num1-num2;
cout<<“Result is "<< num3 <<endl;
num3=num1*num2;
cout<<“Result is "<< num3 <<endl;
num3=num1/num2;
cout<<“Result is"<< num3 <<endl;
}
Method 2
#include <iostream>
using namespace std;
int main()
cout<<“Add is"<<add<<endl;
{
cout<<“Sub is "<< sub <<endl;
int a=10;
cout<<“Div is "<< mul <<endl;
int b=5;
cout<<“Mul is"<<div<<endl;
int add;
}
add=a+b;
int sub;
sub=a-b;
int div;
div=a/b;
int mul;
mul=a*b;
Defining and using Integer Variables
#include <iostream.h>
Output
void main(void) A is 10
B is 15
{
int a;
int b;
a = 10;
b = a + 5; For next
line (endl)
cout<<"A is "<<a <<endl;
cout<<"B is "<<b <<endl;
}
Defining and using Integer Variables
#include <iostream.h>
#include <conio.h> Output
A is 10 and B is 15
void main(void) Press any key to finish
{ int a,b;
a = 10;
b = a + 5;
Clears the screen
clrscr();
cout<<"A is "<<a <<" and B is "<<b<<endl;
cout<<"Press any key to finish";
Get char from screen
getch();
}
•Lets work with float
data type
Defining and using Integer Variables
#include <iostream.h> Output
#include <conio.h> A
964
void main(void) 5.543
{ clrscr();
char first=65;
int second=964;
float third=5.543;
cout<<first<<endl<<second<<endl<<third;
getch();
}
Escape Sequence
• An escape sequence is a sequence of characters that
does not represent itself when used inside a character or
string literal, but is translated into another character or
a sequence of characters that may be difficult or
impossible to represent directly.
Using Escape Sequences in the Program
#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
cout<<"Hello\nHow\nAre\nYou\n";
cout<<“Hello\n\tHow\n\t\tAre\n\t\t\tYou\n”;
getch();
}
Using Escape Sequences in the Program
Output
endl VS \n
cout << endl : Inserts a new line and flushes the
stream
cout << "\n" : Only inserts a new line.
cout << “\n” seems performance wise
better than cout << endl; unless flushing of
stream is required.
Using Escape Sequences in the
Program
• Assignment # 1
• Q1) What is escape sequence, write all escape sequence characters
along with their usage in C/C++.