Lecture 4 C++ fundamentals
Lecture 4 C++ fundamentals
Fundamentals
Lecture 4
First Program in C++
• #include <iostream>
• using namespace std;
• int main()
•{
• cout << “Welcome to C++";
•}
Preprocessor Directive
Preprocessor Directive
// 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
#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
#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
#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>
#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
• Assignment # 1
• Q1) What is escape sequence, write all escape sequence characters
along with their usage in C/C++.