chapter 2 c++

50
Starting Out with C++, 3 rd Edition 1 Chapter 02. Fundamental of C++ By Tariq Rashid Khan

Upload: tariq-khan

Post on 26-May-2017

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: chapter 2 C++

Starting Out with C++, 3rd Edition1

Chapter 02. Fundamental of C++

By Tariq Rashid Khan

Page 2: chapter 2 C++

Starting Out with C++, 3rd Edition2

Program 2-1

//A simple C++ program#include <iostream.h>

Main(){cout<< “Programming is great fun!”;

}

Program Output:Programming is great fun!

Page 3: chapter 2 C++

Starting Out with C++, 3rd Edition3

Table 2-1

Character Name Description // double slash Marks the beginning of a comment. # Pound sign Marks the beginning of a preprocessor

directive < > Opening and

closing brackets

Encloses a filename when used with the #include directive

( ) Opening and closing parenthesis

Used in naming a function, as in int main ()

{ } Opening and closing braces

Encloses a group of statements, such as the contents of a function.

" " Opening and closing quotation marks

Encloses a string of characters, such as a message that is to be printed on the screen

; Semicolon Marks the end of a complete programming statement

Page 4: chapter 2 C++

Starting Out with C++, 3rd Edition4

2.2 The cout Object

• Use the cout object to display information on the computer’s screen. The cout object is referred to as the standard

output object. Its job is to output information using the

standard output device

Page 5: chapter 2 C++

Starting Out with C++, 3rd Edition5

Program 2-2// An unruly printing program#include <iostream.h>

main(){

cout << “Transistor and diodes";cout << “Electronics Components";cout << “Transistor Types";cout << “BJT,Mosfet,FET";cout << “be happy";

}

Program OutputTransistor and diodes……………………be happy

Page 6: chapter 2 C++

Starting Out with C++, 3rd Edition6

New lines(Manipulator and escape of sequence)

• cout does not produce a newline at the end of a statement

• To produce a newline, use either the stream manipulator endl

• or the escape sequence \n• Setw(n)• Tokens

Page 7: chapter 2 C++

Starting Out with C++, 3rd Edition7

Program 2-3

// A well-adjusted printing program#include<iostream.h>#include<conio.h>

main(){

cout<<"Transistor and diodes"<<endl;cout<<"Electronics Components"<<endl;cout<<" Transistor Types"<<endl;cout<<"BJT,Mosfet,FET"<<endl;cout<<"Be happy"<<endl;

getch();}

Page 8: chapter 2 C++

Starting Out with C++, 3rd Edition8

Program Output

Transistor and diodesElectronics Components Transistor TypesBJT,Mosfet,FETBe happy

Page 9: chapter 2 C++

Starting Out with C++, 3rd Edition9

Program 2-4// A well-adjusted printing program#include<iostream.h>#include<conio.h>

main(){

cout<<"Transistor and diodes\n";cout<<"Electronics Components\n";cout<<" Transistor Types\n";cout<<"BJT,Mosfet,FET\n";cout<<"Be happy\n";

getch();}

Page 10: chapter 2 C++

Starting Out with C++, 3rd Edition10

Program Output

Transistor and diodesElectronics Components Transistor TypesBJT,Mosfet,FETBe happy

Page 11: chapter 2 C++

Starting Out with C++, 3rd Edition11

Program 2-5

#include<iostream.h>#include<iomanip.h>#include<conio.h>main(){clrscr();cout<<setw(15)<<"university"<<setw(15)<<"ranking";getch();}

Page 12: chapter 2 C++

Starting Out with C++, 3rd Edition12

Program Output

university ranking

Page 13: chapter 2 C++

Starting Out with C++, 3rd Edition13

Program 2-6

#include<iostream.h>#include<iomanip.h>#include<conio.h>main(){clrscr();cout<<setw(8)<<"20"<<setw(8)<<30<<endl;cout<<setw(8)<<"222"<<setw(8)<<333<<endl;cout<<setw(8)<<"444"<<setw(8)<<666<<endl;getch();}

Page 14: chapter 2 C++

Starting Out with C++, 3rd Edition14

Program Output

20 30 222 333 444 666

Page 15: chapter 2 C++

Starting Out with C++, 3rd Edition15

2.3 The #include Directive

• The #include directive causes the contents of another file to be inserted into the program

• Preprocessor directives are not C++ statements and do not require semicolons at the end

Page 16: chapter 2 C++

Starting Out with C++, 3rd Edition16

2.4 Variables and Constants

• Variables represent storage locations in the computer’s memory. Constants are data items whose values do not change while the program is running.

• Every variable must have a declaration.

Page 17: chapter 2 C++

Starting Out with C++, 3rd Edition17

Program 2-7

#include <iostream.h>#include<conio.h>main(){ int value; value = 555; cout<<"The value is "<< value <<endl; getch(); }

Page 18: chapter 2 C++

Starting Out with C++, 3rd Edition18

Assignment statements:

Value = 555; //This line is an assignment statement.

• The assignment statement explain the expression on the right of the equal sign then stores it into the variable named on the left of the equal sign

• The data type of the variable was in integer, so the data type of the expression on the right should explain to an integer as well.

Page 19: chapter 2 C++

Starting Out with C++, 3rd Edition19

2.5 Constants and Variables

• A variable is called a “variable” because its value may be changed. A constant, on the other hand, is a data item whose value does not change during the program’s execution.

Page 20: chapter 2 C++

Starting Out with C++, 3rd Edition20

Program 2-8#include <iostream.h>#include<conio.h>main(){ int x= 999,y= 2014; float z = 5.5; char name[15]= "University"; cout<<x<<endl; cout<<y<<endl; cout<<z<<endl; cout<<name<<endl; getch();}

Page 21: chapter 2 C++

Starting Out with C++, 3rd Edition21

Program Output

99920145.5University

Page 22: chapter 2 C++

Starting Out with C++, 3rd Edition22

Program 2-9

#include <iostream.h>

void main (void){ int apples; apples = 25; cout<< “Today we sold “ << apples << “ bushels\n”; cout << “of apples.\n”;}

Page 23: chapter 2 C++

Starting Out with C++, 3rd Edition23

Program Output

Today we sold 25 bushels of apples.

Where are the constants in program 2-10?

Page 24: chapter 2 C++

Starting Out with C++, 3rd Edition24

Constants from Program 2-10

Constant Type of Constant 25 Integer constant "Today we sold " String constant " bushels\n" String constant "of apples.\n" String constant

Page 25: chapter 2 C++

Starting Out with C++, 3rd Edition25

2.6 Integer Data Types

• There are many different types of data. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Integer variables only hold whole numbers.

Page 26: chapter 2 C++

Starting Out with C++, 3rd Edition26

Table 2-2

Table 2-5 Integer Data Types, Sizes and RangesData Type Size Rangeshort 2 Bytes -32,768 to +32.767unsigned short 2 Bytes 0 to +65,535int 4 Bytes -2,147,4833,648 to +2,147,4833,647unsigned int 4 Bytes 0 to 4,294,967,295long 4 Bytes -2,147,4833,648 to +2,147,4833,647unsigned long 4 Bytes 0 to 4,294,967,295

Page 27: chapter 2 C++

Starting Out with C++, 3rd Edition27

Program 2-11// This program has variables of several of the integer types.#include <iostream.h>#include<conio.h>

main(){

int checking;unsigned int miles;int years;

  checking = -20;miles = 500;years = 5;cout << "We have made a long journey of " << miles;cout << " miles.\n";cout << "Our checking account balance is " << checking;cout << "\nExactly " << years << " years ago I was ";cout << "stood on this computer lab as a student.\n";

getch();

}

Page 28: chapter 2 C++

Starting Out with C++, 3rd Edition28

Program Output

We have made a long journey of 500 miles.Our checking account balance is -20Exactly 5 years ago I was stood on this computer lab as a student.

Page 29: chapter 2 C++

Starting Out with C++, 3rd Edition29

Program 2-12// This program shows three variables declared on the same// line.#include <iostream.h>#include<conio.h>main(){

int floors,rooms,hall; 

floors = 3;rooms = 100;hall = 1;cout << “UET Abbottabad boys Hostel has " << floors << " floors\n";cout << "with " << rooms << " rooms and " << hall;cout << " hall.\n";

}

Page 30: chapter 2 C++

Starting Out with C++, 3rd Edition30

Program Output

UET Abbottabad boys Hostel has 3 floorswith 100 rooms and 1 hall..

Page 31: chapter 2 C++

Starting Out with C++, 3rd Edition31

2.7 Hexadecimal and Octal Constants

• Hexadecimal numbers are preceded by 0x Hexadecimal F4 would be expressed in C++ as

0xF4• Octal numbers are preceded by a 0

Octal 31 would be written as 031

Page 32: chapter 2 C++

Starting Out with C++, 3rd Edition32

2.8 The char Data Type

• Usually 1 byte long• Internally stored as an integer• ASCII character set shows integer

representation for each character• ‘A’ == 65, ‘B’ == 66, ‘C’ == 67, etc.• Single quotes denote a character, double

quotes denote a string

Page 33: chapter 2 C++

Starting Out with C++, 3rd Edition33

Program 2-13

// This program demonstrates the close relationship between// characters and integers.#include <iostream.h>

main(){

char letter; 

letter = 65;cout << letter << endl;letter = 66;cout << letter << endl;

}

Page 34: chapter 2 C++

Starting Out with C++, 3rd Edition34

Program Output

AB

Page 35: chapter 2 C++

Starting Out with C++, 3rd Edition35

Program 2-14

// This program uses character constants#include <iostream.h>

void main(void){

char letter; 

letter = 'A';cout << letter << endl;letter = 'B';cout << letter << endl;

}

Page 36: chapter 2 C++

Starting Out with C++, 3rd Edition36

Program Output

AB

Page 37: chapter 2 C++

Starting Out with C++, 3rd Edition37

Program 2-15// This program uses character constants#include <iostream.h>

void main(void){

char letter; 

letter = 'A';cout << letter << '\n';letter = 'B';cout << letter << '\n';

}

Page 38: chapter 2 C++

Starting Out with C++, 3rd Edition38

2.9 The bool Data Type

• Boolean variables are set to either true or false

Page 39: chapter 2 C++

Starting Out with C++, 3rd Edition39

Program 2-16#include <iostream.h>

void main (void){ bool boolValue; boolValue = true; cout << boolValue << endl; boolValue = false; cout << boolValue << endl;}

Page 40: chapter 2 C++

Starting Out with C++, 3rd Edition40

Program Output

10Internally, true is represented as the number 1 and false is

represented by the number 0.

Page 41: chapter 2 C++

Starting Out with C++, 3rd Edition41

Program 2-17

#include <iostream.h>

void main (void){

int month = 2, days = 28;cout << “Month “ << month << “ has “ << days << “ days.\n”;

}

Program output:Month 2 has 28 days.

Page 42: chapter 2 C++

Starting Out with C++, 3rd Edition42

The Cin Object input Stream• Used as input statement to get input from

the keyboard during execution of the program.

• >> extration operator or to get from operator.

Page 43: chapter 2 C++

Starting Out with C++, 3rd Edition43

// This program demonstrates the summing and multiplication between two numbers#include <iostream.h>#include<conio.h>

void main (){ int a1,a2,y,z; clrscr(); cout<<"Enter first number"; cin>>a1;cout<<"Enter second number";cin>>a2;y=a1+a2;z=a1*a2;cout<<"sum of numbers="<<y<<endl;cout<<"product of numbers="<<z<<endl;getch();}

Program 2-18

Page 44: chapter 2 C++

Starting Out with C++, 3rd Edition44

Program output: depend upon numbers! Wait

Page 45: chapter 2 C++

Starting Out with C++, 3rd Edition45

Increment and decrement Operators

Like C C++ provides increment(++) and decrement(--) operators

++a denotes pre increment operator.a++ denotes post increment operator.--b denotes pre decrement operator.b-- denotes pre decrement operator.

Page 46: chapter 2 C++

Starting Out with C++, 3rd Edition46

#include <iostream.h>#include<conio.h>

void main (){ clrscr(); int a1,a2,b,sum; a1=5; a2=10; b=2; sum=a1+a2+(++b); cout<<"sum="<<sum; cout<<"b="<<b;getch();}

Program 2-19

Page 47: chapter 2 C++

Starting Out with C++, 3rd Edition47

Program Output

sum=18b=3

Page 48: chapter 2 C++

Starting Out with C++, 3rd Edition48

// This program demonstrates the power of a number#include<math.h>#include<conio.h>#include<iostream.h>void main(){clrscr();long int x,n;long int y;cout<<"value of x : ";cin>>x;cout<<"value of n : ";cin>>n;y=pow(x,n);cout<<"answer is : "<<y;getch();}

Program 2-20

Page 49: chapter 2 C++

Starting Out with C++, 3rd Edition49

Program Output: depend upon numbers

Page 50: chapter 2 C++

Starting Out with C++, 3rd Edition50