lecture 03 c++ programming basics

16
Lecture 03 C++ Programming Basics Comparison between C++ & C: i) input/output statement ii) data type iii) arithmetic operators iv) header files

Upload: seda

Post on 04-Jan-2016

24 views

Category:

Documents


2 download

DESCRIPTION

Lecture 03 C++ Programming Basics. Comparison between C++ & C: i) input/output statement ii) data type iii) arithmetic operators iv) header files. C++ vs C. C++ is a superset of C. C++ was originally called “C with classes”. Basic C++ Program Construct. Basic C++ Program Construct - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 03 C++ Programming Basics

Lecture 03

C++ Programming Basics

Comparison between C++ & C:i) input/output statementii) data typeiii) arithmetic operatorsiv) header files

Page 2: Lecture 03 C++ Programming Basics

C++ vs C

C++ is a superset of C. C++ was originally called “C with classes”.

Page 3: Lecture 03 C++ Programming Basics

Basic C++ Program Construct

Page 4: Lecture 03 C++ Programming Basics

Basic C++ Program ConstructA Sample Program

// first.cpp #include <iostream> //preprocessorusing namespace std; //directive

int main() { int ftemp; // temperature in Fahrenheit cout << "Enter temperature in Fahr.: "; cin >> ftemp; int ctemp = (ftemp - 32) * 5 / 9; cout << "Equivalent in Celsius is: " << ctemp << endl; return 0; }

Page 5: Lecture 03 C++ Programming Basics

Comments

C Style:

/* This program demonstrate

function overloading */

C++ Style:

// This program demonstrate

// function overloading

Page 6: Lecture 03 C++ Programming Basics

Preprocessor Directive#include

C Style:

#include<stdio.h>

C++ Style:

#include<iostream.h>

OR THE FOLLOWING ANSI STANDARD

#include<iostream>

using namespace std;

Page 7: Lecture 03 C++ Programming Basics

Input and Output

C Input and Ouput:

#include<stdio.h>

scanf(“%i”, &num);

printf(“The number is %i\n“, num);

C++ Input and Ouput:

#include<iostream.h>

cin >> num;

cout << “The number is “, num << endl;

Page 8: Lecture 03 C++ Programming Basics

Stream I/OInsertion and Extraction

Page 9: Lecture 03 C++ Programming Basics

Constants

C-Style Symbolic Constant:

#define PI 3.14159

We can’t specify the data type!

C++ const Qualifier:

const float PI = 3.14159;

Recommended !

What is a Variable ?

What is an Identifier?

Rules for declaring an identifier?

Int Sum1 ;Float 1Salaray

Page 10: Lecture 03 C++ Programming Basics

Boolean Type

• Integer, character, and floating-point types are available both in C and C++.

• Boolean type is available in C++, but not in C.

• Variables of type bool can have only two possible values: true and false (Boolean Values).

• Even though a single bit is enough to store variables of type bool, compilers often store them as integers for ease of processing.

Page 11: Lecture 03 C++ Programming Basics

Boolean Type

void main()

{

bool flag = true;

if (flag)

cout<<“Hello Halizah”<<endl;

else

cout<<“Hello World!”<<endl;

}

Void Main( )

{ int num; bool flag = true;

Cin>> num;

if (num > 0 )

flag = true;

else

flag = false;

if (flag == true ) cout<<“positive number”;

else

cout “negative number”;

}

Page 12: Lecture 03 C++ Programming Basics

Type Conversion : Order of Data Types

Data Type Order

long double Highestdoublefloatlongintshortchar Lowest

Page 13: Lecture 03 C++ Programming Basics

Automatic Type Conversion

• When two operands of different types are encountered in the same expression, the lower-type variable is automatically converted to the higher type variable.

int denominator;

float numerator, quotient;

...

quotient = numerator/denominator;

• denominator is converted from int to float before the division.

Page 14: Lecture 03 C++ Programming Basics

The setw Manipulator

#include <iostream.h> #include <iomanip.h> // for setw

int main() { long pop=2425785; cout << setw(8) << "LOCATION" << setw(12)

<< "POPULATION" << endl << setw(8) << "Portcity" << setw(12) << pop

<< endl return 0; }

Output of the program:LOCATION POPULATION Portcity 2425785

Page 15: Lecture 03 C++ Programming Basics

The <cmath> Standard Library Functions

// sqrt.cpp // demonstrate sqrt() library function #include <iostream> // for cout, etc. #include <cmath> // for sqrt() using namespace std;

int main() { double number, answer; cout << "Enter a number: "; cin >> number; answer = sqrt(number); cout << "Square root is " << answer << endl; return 0; }

Page 16: Lecture 03 C++ Programming Basics

Header and Library Files