object oriented programming in c++

22
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE 03 1

Upload: marcella-quintin

Post on 03-Jan-2016

33 views

Category:

Documents


5 download

DESCRIPTION

OBJECT ORIENTED PROGRAMMING IN C++. LECTURE 03. What is a Variable?. Variables may be the most fundamental aspect of any computer language. A variable can be define as: A variable represents one or more storage location where program data is held. OR - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: OBJECT ORIENTED PROGRAMMING IN C++

OBJECT ORIENTED PROGRAMMING IN C++

LECTURE 03

1

Page 2: OBJECT ORIENTED PROGRAMMING IN C++

WHAT IS A VARIABLE?

Variables may be the most fundamental aspect of any computer language.

A variable can be define as:

A variable represents one or more storage location where program data is held.

ORA variable is a place to store a piece of

information.OR

A variable is a space in the computer’s memory for a certain kind of data and given a name for easy reference.

2

Page 3: OBJECT ORIENTED PROGRAMMING IN C++

VARIABLE NAME OR IDENTIFIER Each variable needs an identifier that distinguishes it

from the others. A valid identifier is a sequence of one or more letters,

digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ).

Another rule is that Keyword cannot be used as an identifier name and upper case and lower case are distinct.

Length: Variables names can contain any combination of characters as opposed to the restriction of 31 letters in C++. 3

Page 4: OBJECT ORIENTED PROGRAMMING IN C++

FUNDAMENTAL DATA TYPES

Data type means what type of data or information that are stored in a variable.

When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them.

By defining data type, computer are easily identify:- How many bytes are occupy in memory by variable. What is the max and min value/range that we are able to store

in variable. and, what operations can apply on value and how complier can

solve them.

In next slide, there is a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one:

4

Page 5: OBJECT ORIENTED PROGRAMMING IN C++

NAME DESCRIPTION SIZE RANGE

Char Character 1 byte signed: -128 to 127unsigned: 0 to 255

short int(short)

Short integers 2 bytes signed: -32768 to 32767unsigned: 0 to 65535

Int Integer 4 bytes signed: -2147483648 to2147483647unsigned: 0 to 4294967295

long int (long) Long integer 4 bytes signed: -2147483648 to2147483647unsigned: 0 to 4294967295

float Floating point number 4bytes 3.4 x 10-38 to 3.4 x 1038

double Double precision floating point number

8 bytes 1.7 x10-308 to 1.7 x 10308

Long double Long double precision floating point number

8bytes 3.4 x 10-4932 to 1.1 x 104932

Bool Boolean values 1 byte It can take one of two values: trueor false.

5

Page 6: OBJECT ORIENTED PROGRAMMING IN C++

TYPE BOOL

Type bool gets its name from George Boole, a19th century English mathematician who invented the concept of using logical operators with true-or-false values. Thus such true/false values are often called Boolean values.

Variable of type bool can have only two possible values: true and false.

Type bool variable is most commonly used to hold the result of comparisons.

6

Page 7: OBJECT ORIENTED PROGRAMMING IN C++

DECLARATION OF VARIABLES

In order to use a variable in C++, we must first declare it specifying which data type we want it to be.

In c++, we are free to declare any variable at any place in program.

The syntax for declararing a variable in c++ is same as c language:

datatype variable-name;

Examples:

int age;

float P1,P2;

to declare unsigned variables, we must add keyword unsigned before datatype e.g:

unsigned int radius;

unsigned long l;7

Page 8: OBJECT ORIENTED PROGRAMMING IN C++

INITIALIZATION OF VARIABLES

When declaring a regular local variable, we may want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable. There are two ways to do this in C++:

The initialization, is done by appending an equal sign followed by the value to which the variable will be initialized:

type identifier = initial_value ;

For example, if we want to declare an int variable called a initialized with a value of 0 at the moment in which it is declared, we could write:

 int a = 0;

8

Page 9: OBJECT ORIENTED PROGRAMMING IN C++

#include <iostream> void main () { int a=5; // initial value = 5 int b=2; // initial value = 2 int result; // initial value undetermined a = a + 3; result = a - b; cout << result; getch(); } 9

Page 10: OBJECT ORIENTED PROGRAMMING IN C++

SCOPE OF VARIABLES

A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

10

Page 11: OBJECT ORIENTED PROGRAMMING IN C++

LECTURER 04

C++ Operators11

Page 12: OBJECT ORIENTED PROGRAMMING IN C++

CONSTANTS

Lecture 05

12

Page 13: OBJECT ORIENTED PROGRAMMING IN C++

CONSTANTS

Constants are expressions with a fixed value.

Literals Literals are used to express particular values within

the source code of a program. For example, when we wrote:

a = 5; the 5 in this piece of code was a literal constant.

Literal constants can be divided in

1. Integer Numerals,

2. Floating-Point Numerals,

3. Characters, Strings and

4. Boolean Values. 13

Page 14: OBJECT ORIENTED PROGRAMMING IN C++

INTEGER NUMERALS

1776 707 -273 They are numerical constants that identify integer decimal

values. In addition to decimal numbers, C++ allows the use as

literal constants of octal numbers (base 8) and hexadecimal numbers (base 16).

If we want to express an octal number we have to precede it with a 0 (zero character). And in order to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the following literal constants are all equivalent to each other:

75 // decimal 0113 // octal 0x4b // hexadecimal All of these represent the same number: 75 (seventy-five)

14

Page 15: OBJECT ORIENTED PROGRAMMING IN C++

Literal constants, like variables, are considered to have a specific data type.

By default, integer literals are of type int. However, we can force them to either be unsigned by appending the u character to it, or long by appending l:

75 // int 75u // unsigned int 75l // long 75ul // unsigned long   In both cases, the suffix can be specified using either

upper or lowercase letters. 15

Page 16: OBJECT ORIENTED PROGRAMMING IN C++

FLOATING POINT NUMBERS They express numbers with decimals and/or

exponents. They can include either a decimal point, an “e” character (that expresses "by ten at the Xth height", where X is an integer value that follows the e character), or both a decimal point and an e character:

3.14159 // 3.14159 6.02e23 // 6.02 x 10^23 1.6e-19 // 1.6 x 10^-19 3.0 // 3.0 These are four valid numbers with decimals expressed

in C++. If you explicitly want to express a float or long double

numerical literal, we can use the f or l suffixes respectively: 3.14159L // long double 6.02e23f // float

16

Page 17: OBJECT ORIENTED PROGRAMMING IN C++

CHARACTER AND STRING LITERALS

There also exist non-numerical constants, like:

'z'

'p'

"Hello world"

"How do you do?" The first two expressions represent single character

constants, and the following two represent string literals composed of several characters.

Notice that to represent a single character we enclose it between single quotes (') and to express a string (which generally consists of more than one character) we enclose it between double quotes (").

17

Page 18: OBJECT ORIENTED PROGRAMMING IN C++

BOOLEAN LITERALS

There are only two valid Boolean values: true and false. These can be expressed in C++ as values of type bool by using the Boolean literals true and false.

18

Page 19: OBJECT ORIENTED PROGRAMMING IN C++

PROGRAM EXAMPLE #include<iostream.h> #include<conio.h> void main(void) { int i=10; int a=0113; int b=0x4b; unsigned long x=7ul; float y=2.3e20f; clrscr(); cout<<i+a+b; cout<<“\n”<<i+x; cout<<“\n”<<y; getch(); }

OUTPUT

16017

2.3 e+20

19

Page 20: OBJECT ORIENTED PROGRAMMING IN C++

THE CONST QUALIFIER:

The keyword const specifies that the value of a variable will not change throughout the program.

Any attempt to alter the value of a variable define with this qualifier will elicit an error message from the complier.

Example: void main() { float rad; const float PI=3.14159;

cout<<"Enter radius of circle: "; cin>>rad; float area=PI*rad*rad; cout<<"Area is "<<area; getch(); }

20

Page 21: OBJECT ORIENTED PROGRAMMING IN C++

THE #DEFINE DIRECTIVE:

The #define directive tells the complier to substitute one thing for another. The directive sets up an equivalence between an identifier and a text phrase.

For example, the line #define PI= 3.14159 specifies that the identifier PI will be replaced by

the text 3.14159 throughout the program.

21

Page 22: OBJECT ORIENTED PROGRAMMING IN C++

EXAMPLES

#include<iostream.h> #include<conio.h> #define PI 3.14159

void main() { float rad; clrscr();

cout<<"Enter radius of circle: "; cin>>rad;

float area=PI*rad*rad; cout<<"Area is "<<area; getch(); }

22