Vous êtes sur la page 1sur 50

Chapter 02.

Fundamental of C++ By Tariq Rashid Khan

Starting Out with C++, 3rd Edition

Program 2-1
//A simple C++ program #include <iostream.h>
Main() { cout<< Programming is great fun!; }

Program Output: Programming is great fun!


2

Starting Out with C++, 3rd Edition

Table 2-1
Character // # <> Name double slash Pound sign Opening and closing brackets Opening and closing parenthesis Opening and closing braces Opening and closing quotation marks Semicolon Description Marks the beginning of a comment. Marks the beginning of a preprocessor directive Encloses a filename when used with the #include directive Used in naming a function, as in int main () Encloses a group of statements, such as the contents of a function. Encloses a string of characters, such as a message that is to be printed on the screen

()

{} ""

Marks the end of a complete programming statement


3

Starting Out with C++, 3rd Edition

2.2 The cout Object

Use the cout object to display information on the computers screen.

The cout object is referred to as the standard output object. Its job is to output information using the standard output device

Starting Out with C++, 3rd Edition

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

main() { cout cout cout cout cout }

<< << << << <<

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

Program Output
Transistor and diodesbe happy

Starting Out with C++, 3rd Edition

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
6

Starting Out with C++, 3rd Edition

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(); }

Starting Out with C++, 3rd Edition

Program Output
Transistor and diodes Electronics Components Transistor Types BJT,Mosfet,FET Be happy

Starting Out with C++, 3rd Edition

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(); }

Starting Out with C++, 3rd Edition

Program Output
Transistor and diodes Electronics Components Transistor Types BJT,Mosfet,FET Be happy

10

Starting Out with C++, 3rd Edition

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

11

Starting Out with C++, 3rd Edition

Program Output
university ranking

12

Starting Out with C++, 3rd Edition

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(); }

13

Starting Out with C++, 3rd Edition

Program Output
20 222 444 30 333 666

14

Starting Out with C++, 3rd Edition

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

15

Starting Out with C++, 3rd Edition

2.4 Variables and Constants


Variables represent storage locations in the computers memory. Constants are data items whose values do not change while the program is running. Every variable must have a declaration.

16

Starting Out with C++, 3rd Edition

Program 2-7

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

Starting Out with C++, 3rd Edition

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.
18

Starting Out with C++, 3rd Edition

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 programs execution.

19

Starting Out with C++, 3rd Edition

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(); }
20

Starting Out with C++, 3rd Edition

Program Output

999 2014 5.5 University

21

Starting Out with C++, 3rd Edition

Program 2-9
#include <iostream.h>
void main (void) { int apples; apples = 25; cout<< Today we sold << apples << bushels\n; cout << of apples.\n; }

22

Starting Out with C++, 3rd Edition

Program Output

Today we sold 25 bushels of apples. Where are the constants in program 2-10?

23

Starting Out with C++, 3rd Edition

Constants from Program 2-10


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

24

Starting Out with C++, 3rd Edition

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.

25

Starting Out with C++, 3rd Edition

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

26

Starting Out with C++, 3rd Edition

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(); }
27

Starting Out with C++, 3rd Edition

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

28

Starting Out with C++, 3rd Edition

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";
}

29

Starting Out with C++, 3rd Edition

Program Output
UET Abbottabad boys Hostel has 3 floors with 100 rooms and 1 hall. .

30

Starting Out with C++, 3rd Edition

2.7 Hexadecimal and Octal Constants


Hexadecimal numbers are preceded by 0x

Hexadecimal F4 would be expressed in C++ as 0xF4 Octal 31 would be written as 031

Octal numbers are preceded by a 0

31

Starting Out with C++, 3rd Edition

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
32

Starting Out with C++, 3rd Edition

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; }

33

Starting Out with C++, 3rd Edition

Program Output
A B

34

Starting Out with C++, 3rd Edition

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; }
35

Starting Out with C++, 3rd Edition

Program Output
A B

36

Starting Out with C++, 3rd Edition

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'; }

37

Starting Out with C++, 3rd Edition

2.9 The bool Data Type


Boolean variables are set to either true or false

38

Starting Out with C++, 3rd Edition

Program 2-16
#include <iostream.h> void main (void) { bool boolValue; boolValue = true; cout << boolValue << endl; boolValue = false; cout << boolValue << endl; }

39

Starting Out with C++, 3rd Edition

Program Output 1 0 Internally, true is represented as the number 1 and false is represented by the number 0.

40

Starting Out with C++, 3rd Edition

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.

41

Starting Out with C++, 3rd Edition

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.

42

Starting Out with C++, 3rd Edition

Program 2-18
// 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();

}
43

Starting Out with C++, 3rd Edition

Program output: depend upon numbers! Wait

44

Starting Out with C++, 3rd Edition

Increment and decrement Operators


Like C C++ provides increment(++) and decrement(--) operators ++a a++ --b b-denotes pre increment operator. denotes post increment operator. denotes pre decrement operator. denotes pre decrement operator.

45

Starting Out with C++, 3rd Edition

Program 2-19
#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(); }

46

Starting Out with C++, 3rd Edition

Program Output sum=18b=3

47

Starting Out with C++, 3rd Edition

Program 2-20
// 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(); }

48

Starting Out with C++, 3rd Edition

Program Output: depend upon numbers

49

Starting Out with C++, 3rd Edition

50

Starting Out with C++, 3rd Edition

Vous aimerez peut-être aussi