c++ lecture 2

17

Upload: sajidpk92

Post on 14-Jun-2015

189 views

Category:

Education


0 download

TRANSCRIPT

Page 1: c++ Lecture 2
Page 2: c++ Lecture 2

Chapter # 2C++ Programming Basics

Lecture # 2Instructor: Engr. Afshan Asim

Page 3: c++ Lecture 2

Objectives

endl & setw Manipulator

const Qualifier

#define Directive

Input with “cin”

Type Conversion

Operators

Arithmetic Operators.

Assignment & Increment/Decrement Operators

Expression & precedence

Page 4: c++ Lecture 2

endl Manipulator

//endl Manipulator#include<iostream>using namespace std;void main(){int var1; //declare var1int var2=20; //20 is integer constantvar1=var2+10;cout<<“value of var1=”<<var1<<endl;cout<<“value of var2=“<<var2;system(“pause”);}/* end of program*/

Page 5: c++ Lecture 2

setw Manipulator

//setw Manipulator#include<iostream>#include<iomanip>using namespace std;void main(){int a=1234567;int b=234;int c=12345; cout<<“a=“<<setw(10)<<a;cout<<“\nb=“<<setw(10)<<b;cout<<“\nc=“<<setw(10)<<c;system(“pause”);}

Page 6: c++ Lecture 2

const Qualifier

//const Qualifier#include<iostream>using namespace std;void main(){int num1=15;const int num2=23;const float num3=3.14F;const int num4; //ERRORnum1=20;num2=50; //ERRORsystem(“pause”);}

Page 7: c++ Lecture 2

#define Directive

//#define Directive

#include<iostream>

#define PI 3.14

void main()

{

int num1;

num1=3*PI;

cout<<“Result=“<<num1;

system(“pause”);}

Page 8: c++ Lecture 2

Input with cin

//Method to take user input

#include<iostream>

using namespace std;

void main()

{

int num1, num2;

cout<<“Enter 1st number :”;

cin>>num1;

cout<<“Enter 2nd number :”;

cin>>num2;

or

cout<<“Enter two numbers :”;

cin>>num1>>num2;

cout<<“\n You entered:”<<num1<<“&”<<num2;

system(“pause”);

}

Page 9: c++ Lecture 2

Type Conversion

• Automatic Conversion

Data Type Order

long double Highest

double

float

long

int

short

char Lowest

Page 10: c++ Lecture 2

Automatic Conversion

//automatic conversion

#include<iostream>

using namespace std;

void main()

{

int a=20;

float b=2.34F;

int result;

result=a*b; //automatic conversion

cout<<“Result=“<<result;

system(“pause”);}

Page 11: c++ Lecture 2

Arithmetic Operators

• Four arithmetic operators work on all data types.– Addition: (+) cout<<6+8;– Subtraction(-) float f=3.4-2.0;– Multiplication(*) long m=34*20;– Division(/) cout<<8/5;

• Remainder/Modulus Operator: (%)Works only with (int, char, long, short)cout<<6%8 6cout<<9%8 1

Page 12: c++ Lecture 2

Arithmetic Assignment Operators

• Arithmetic assignment operators corresponding to all arithmetic operators are

+=, -+, *=, /=, %=item+=5 same as item=item + 5item-=6 same as item=item – 6item*=4 same as item=item * 4item/=9 same as item=item / 9item%=7 same as item=item % 7

Page 13: c++ Lecture 2

Increment Operator

• Increment operator ++ adds 1 to its argument– Prefix ++count– Postfix count++

int count=10;cout<<“count=“<<count<<endl; count=10cout<<“count=“<<++count<<endl; count=11cout<<“count=“<<count<<endl; count=11cout<<“count=“<<count++<<endl; count=11cout<<“count=“<<count<<endl; count=12

Page 14: c++ Lecture 2

Decrement Operator

• Decrement operator -- subtracts 1 from its argument– Prefix --count– Postfix count--

int count=10;cout<<“count=“<<count<<endl; count=10cout<<“count=“<<--count<<endl; count=9cout<<“count=“<<count<<endl; count=9cout<<“count=“<<count--<<endl; count=9cout<<“count=“<<count<<endl; count=8

Page 15: c++ Lecture 2

Expression & Precedence

• Any combination of variables, constants and operators– a+b– a/7– (a+b)*9

• Parenthesis has higher precedence.• * and / have same precedence.• + and – have same precedence.

20-3*7 20-21 -1(20-3)*7 17*7 119

• Operators with same precedence are evaluated from left to right(15-5)*5/10 10*5/10 50/10 5 (left to

right)

Page 16: c++ Lecture 2

Exercise

Assuming there are 7.481 gallons in a cubic feet, write a program that asks the user to enter number of gallons, and then displays the equivalent in cubic feet.

Page 17: c++ Lecture 2