Object Oriented Programming 2

Published on June 2017 | Categories: Documents | Downloads: 24 | Comments: 0 | Views: 153
of x
Download PDF   Embed   Report

Comments

Content

Object Oriented
Programming 2

Tokens in C++
Keywords- These are the explicitly reserved identifiers that cannot
be used as names for the program variables or other user defined
program elements.
eg.auto, break, if, else, float, int etc
Identifiers- The name of variables, functions, array, classes etc.
created by the programmer.
Rule 1: Only alphabetic characters, digits and underscores are
premitted.
Rule 2: The name cannot start with a digit
Rule 3: Upper case and Lower case letters are distinct.
Rule 4: A declared keyword cannot be used as a variable name
Note**: ANSI C recognizes only the first 32 characters in a name while
ANSI C++ places no limit on its length and recognizes all the characters.

Constants: Fixed values that do not change during the
execution
of a program.
Literal Contants:
245
//decimal integer
13.24 //floating point integer
037 //octal integer
0X3 //hexadecimal integer
“C++” //string constant
‘A’
//character constant
L ‘ab’ //wide character constant (Note**: wchar_t type is
used to declar wide character literal beginning with
letter L)

Enumerated Data Type
A user defined data type which provides a way for
attaching
names to numbers, thereby increasing the
comprehensibility of
the code.
enum shape{circle,square,triangle};
shape ellipse;
shape ellipse = 5; //incorrect
shape ellipse= circle; //correct
shape ellipse = (shape) 5; //correct
Note**: C++ does not permit an int value to be automatically
converted to an
enum value. But an enumerated value can be used in place of an int
value.

Eg. int c=circle; // shape type promoted to int

Over-riding Enumerated Data Type Defaults

enum shape {circle, square=4, triangle=8}

Anonymous enums
Eg. enum{off, on};
int switch_1=off
int switch_2=on;

Enumerated Data Type Example
enum shape {circle,rectangle,triangle};
int main()
{
cout<<“Enter shape code:”;
int code;
cin>>code;
while(code>=circle && code<=triangle)
{
switch(code)
{
case circle:
……………
……………
break;
case rectangle:
……………
……………
break;
case triangle:

……………
……………
break;
}
cout << “Enter shape
code:”;
cin >> code;
}
cout << “BYE \n”;
return 0;
}
Note**:
ANSI
C
permits an enum to
be defined within a
structure or a class
but the enum is
globally visible. In
C++,
an
enum
defined
within
a
class(or structure) is
local to that class(or
structure) only.

Some Important Concepts
• In C++, the size of the array should be one larger
than the number of characters in the string.
• char * const ptr1=“Hello”; //constant pointer
(it cannot modify the address that ptr1 is initialized to).

int const * ptr2=&m;

//pointer to a constant

( can point to any variable of correct type, but the contents of
what it points to cannot be changed.)
Combination of both can be applied as
const char * const ptr =“xyz”;


const int size=10;
in this manner, C
char name[size];
constants.

/* Illegal in C to create type constants
strictly uses #define for creating

• In C++, use of const modifier alone, defaults to int
data type.
eg. const size = 10;
which is equivalent to
const int size = 10;
• C++, requires const to be initialized unlike ANSI C
which initializes to 0 by default.
• In C++, const values are local to the file where it is
declared. Whereas, in C, const values are global in
nature and are visible outside the file in which they
are declared. However, can be local by declaring
them as static.
• In C++, to make an const value global, it must be
explicitly defined as extern.
extern const sum=100;

C++ Operators
::

Scope resolution operator

::*

Pointer to member declarator

->*

Pointer to member operator

.*

Pointer to member operator

delete

Memory release operator

endl

Line feed operator

new

Memory allocation operator

setw

Field width operator

Scope resolution operator
…….
…….
{
int x=10;
……..
……..
{
int x=1;
……….
……….
}
………
}

//Block 1

// Block 2

C does not allow a
global version of a
variable to be
accessed from within
the inner block .
Resolved by using the
scope resolution
operator :: .
Eg. :: variable_name

Scope resolution operator Example
#include <iostream.h>
using namespace std:
int m = 10;
int main()
{
int m=20;
{
int k=m;
int m=30;

//global m

//m redeclared, local to main
//m declared again
// local to inner block

cout << “We are in inner block \n”;
cout << “k=“ << k << “\n”;
cout << “m=“ << m << “\n”;
cout << “::m= “ << ::m << “\n”;
the global variable
}
cout << “\n We are in outer block \n”;
cout << “m= “ << m << “\n”;
cout << “::m =“ << ::m << “\n”;
return 0;
}

//::m will always refer to

Memory Management Operators
new and delete in place of malloc and calloc in C
Declaration Style
int *p=new int;
float *q=new float;
int *p=new int(25);
float *q=new float(7.5);

//Spot declaration and intialization

int *p=new int[10];
ptr_array1=new
ptr_array2=new
ptr_array3=new
ptr_array4=new

int [4][5][6];
int [size][5][6];
int[4][5][];
int[][5][6];

//correct
//correct
//incorrect
//incorrect

Memory Management Operators
For variable deletion
delete p;
delete [5]array;
or
delete []array;

Advantages of new operator over malloc()
1. It automatically computes the size of the data object thus
avoiding the use of
sizeof operator.
2. It automatically returns the correct pointer type, so that there is
no need to use
a type cast.
3. It is possible to initialize the object while creating the memory
space.
4. Like any other operator, new and delete can be overloaded.

Some special assignment expressions
Chained Assignment
x=(y=10);

or

int x=y=10;
declaration

x=y=10;
//Wrong to use chain assignment at the time of

Embedded Assignment
x=(y=50)+10 equivalent to

y=50; x=y+10;

Compound Assignment
x=x+10; can be written as x+=10;

Implicit or Automatic Conversions
Principle: Whenever the compiler encounters an
expression that
consist of mixed data types, it divides the expression
into sub
expressions consisting of one operator and one or two
operands and
then converts one of them to match with other by
following the
simple rule that the smaller type is converted to wider
type. The
other name is Integral Widening Conversion.
Eg. When encountered float and int, it will convert int
to the float
type

Thank You

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close