Documente Academic
Documente Profesional
Documente Cultură
system
&
any
complex
real
life
application system.
Since C++ allows us to create hierarchy related objects we
can built special object oriented libraries which can be
used later by many programs.
While C++ is able to map the real world problem
properly. The C part of C++ gives the language the ability
to get close to machine level details.
C++ programs are easily maintainable & expandable.
When a new feature needs to be implemented it is very
easy to add to the existing structure of an object.
It is expected that C++ will replace C as a general
purpose language in a near future.
Striking features of OOPS
Emphasis on data rather than procedure.
Programs are divided into classes & classes can be used
with the creation of objects.
Data structures are designed such that they characterized
the objects.
Basics of OOPS
Objects
Classes
Data Abstraction and Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing
Objects: The object is often instance of class. Objects are
the basic runtime entities in an object oriented system.
They may represent a person, a place, a bank account or
any item that program has to handle. Programming
Box Object
Draw (box)
Triangle Object
Draw (triangle)
with
each
other.
The
process
of
NameSpace
It is the new concept introduced by NCC++ standard and
committee. This defines a scope for the identifier that are
Basic Datatypes:
C++ Datatypes
User Defined
Datatype
Built-in
Datatype
Structure
Union
Class
enum
Integral
Type
int
Derived Datatype
void
char
array
function
pointer
reference
Floating
Point
float
double
Type
Size
Range
Char
Unsigned char
Signed char
int
Unsigned int
Signed int
Long int
Signed long int
Unsigned long int
Float
Double
Long double
1
1
1
2
2
2
4
4
4
4
8
10
-128 to 127
0 to 255
-128 to 127
-32768 to 32767
0 to 65535
-32768 to 32767
-2147483648 to 2147483647
0 to 429496795
0 to 429496795
3.4E-38 to 3.4E+38
1.4E-308 to 1.7E+308
3.4E-4942 to 1.1E4932
Reference Variable:
C++ introduces a new kind of variable known as reference
variable. It provides an alternative name for the previously
defined variable.
Syntax: datatype &reference name=variable name
E.g. float total=100;
float &sum=total;
total is the float type variable that has already been declared.
sum has an alternative name declared to represent the variable
total. Both the variable refer to the same data object in the
memory.
//p1.cpp
Operators
::
::*
->*
.*
Delete
Endl
New
Setw
//p2.cpp
Like C, C++ is also a block structured language. Blocks and
scopes can be used in constructing programs. We know that
same variable name can be used to have differentiation
meanings in differentiation blocks. The scope of the variable
extent from the points of its declaration till the end of the block
containing the declaration. A variable declared in site of block
is said to be local to that block. In C, global version of the
variable cannot be accessed from within the inner block. C++
resolves this problem by introducing a new operator called the
scope resolution operator. This can be used to uncover the
hidden variable. This operator allows access to the global
version of the variable in the inner block.
Member Dereferencing Operators
C++ permits to define a class containing various types of data
and functions as members. C++ also permits to access the class
members through pointers.
Operators
::*
*
Functions
To declare a pointer to a member of a class.
To access a member using object name and a
->*
Object Name
Attribute 1
Object Name
Attribute 1
Attribute 2
Attribute 2
Operation 1
Attribute N
Operation 1
Attribute N
Operation 2
Operation 2
|
|
Operation N
Operation N
Operation 2
Operation 1
Attribute 1
|
|
Attribute N
|
|
Operation N
Account
AccountNumber
AccountType
Name
Balance
Deposit()
Withdraw()
Enquire()
|
|
Account
AccountNumber
AccountType
Deposit()
Name
Balance
Withdraw()
Enquire()
Deposit()
Withdraw()
Accountnumber
Accounttype
Name
Balance
Enquire()
Classes
The objects with the same data structure (attributes) and
behavior (operations) are grouped into a class. All those objects
possessing similar properties are grouped into the same unit.
Person Objects Abstract
Boy
Girl
Into
Person Class
Attributes: Name, Age, Gender
Operations: Speak( ), Listen( ),
Walk( ).
Vehicle Objects
Abstract
Car
Into
Vehicle Class
Attributes: Name, Model, Color
Operations: Start ( ), Stop ( ),
Accelerate ( ).
Polygon Objects
Triangle
Abstract
Into
Polygon Class
Attributes: Vertices, Border,
Hexagon
Color, Fillcolor.
Operations: Draw(), Erase(),
Move().
{
private: char Name[20];
//data members
int AccountType;
int AccountNumber;
float Balance;
public: Deposit( );
//member functions
Withdraw( );
Enquire( );
};
This declaration is similar to the structure declaration in C. It
enables the creation of the class variables called objects.
For E.g. the following statements,
account savings_account;
account current_account;
account fd_account;
create
instances
of
the
class
account.
They
define
Operations
(Functions)
Hello World
The header file iostream.h supports streams programming
features by including predefined stream objects. The C++s
stream insertion operator << sends the message Hello World
to the predefined console object, cout, which in turn prints on
the console. The Hello World program in C++ is shown for the
purpose of comparative analysis.
1. //hello.cpp: printing hello world msg
2. #include<iostream.h>
Comment
Preprocessor Directive
3. void main( )
Function declarator
4. {
Function begin
5.
cout<<Hello World;
6. }
Preprocessor Directive
The preprocessor directive includes all the statements of the
header file iostream.h. It contains instructions and predefined
constants that will be used in the program. It plays a role
similar to that of the header file stdio.h of C. The header file
iostream.h contains declarations that are needed by the cout
and cin stream objects. There are a number of such
preprocessor directive provided by the C++ library, and they
cin>>variable;
Input of more than one item can also be performed using the
cin input stream object. Such input operations in C++ are
called cascaded input operations. The complete syntax of the
standard input streams operations is as follows:
syntax
specifications.
The
following
are two
Literals-Constant Qualifiers
Literals are constants, to which symbolic names are
associated for the purpose of readability and ease of
handling standard constant values. C++ provides the
following 3 ways of defining constants:
#define preprocessor directive
Enumerated data types
const keyword
//p12.cpp
2) int &a=b;
3) float &x=y;
4) double &height=length;
5) int &x=y[100];
6) int n;
int *m=&n;
int &m=*p;
These declarations cause m to refer n ,which is pointed to by
the pointer variable p.
7) int &num=100;
This statement causes compilation error; constants cannot
be made to be pointed to by a reference variable. Hence the
rule, no alias for constant value.
Reference variables are not bounded to a new memory
location, but to the variables to which they are aliases.
//p13.cpp
In main(), the statements,
z=b;
z=c;
Data Type
Actual
parameter
Type
Value
Formal
parameter
Type
Value
(a)
Actual
Formal
parameter
parameter
Storage Location
Call-by-value
Type
Type
Value
(b)
Call-by-pointer/reference
//by pointers
{
int t;
t=*p;
*p=*q;
*q=t;
}
A call to the function swap( )
swap(&x,&y) has effect on the values of x and y i.e. it
exchanges the contents of variables x and y. The above
swap( - - ) function can be redefined by using a new parameter
passing scheme, call by reference as follows:
void swap(int &x, int &y)
{
int t;
//by reference
t=x;
x=y;
y=t;
}
Program having swap( - - ) function with call-by-reference
mechanism for parameter passing is as shown:
//p17.cpp
In main( ), the statement
swap(a,b);
is translated into
swap(&a,&b);
internally during compilation; the prototype of the function
void swap(int &x, int &y)
//by reference
t=*x;