c++ . chapter1
c++ . chapter1
1 COURSE CONTENT
UNIT – I
PRINCIPLES OF OBJECT
ORIENTED PROGRAMMING
2
1.1 Procedure oriented Programming
1.2 Object oriented programming paradigm
1.3 Basic concepts of Object Oriented Programming
1.4 Advantages of Object Oriented Programming
1.5 Object Oriented Languages
1.6 Applications of Object Oriented Programming
1.7 C++ Concepts
1.8 Structure of C++ program
1.9 Applications of C++
1.10 Basic Data types in C++
1.11 User defined Data types
1.12 Derived Data types
1.13 Defining Constants
1.14 Declaration of variables and Dynamic initialization of variables
1.15 Reference variables
1.16 Operators in C++
1.17 Scope Resolution Operators
1.18 Member dereferencing Operators 3
1.19 Memory Management Operators and Manipulators
1.20 Type cast Operator
4
INTRODUCTION
4
5
Disadvantage of POP:
To develop a large program, it is very difficult to identify
what data is used by which function.
It does not model(solve) real world problem very well.
Characteristics Exhibited:
Emphasis is on doing things (algorithms)
Large programs divide into smaller programs known as
functions.
Data move openly around the system from function to
function.
Functions transform data from one form to another.
All the function share global data.
Uses top-down approach in program.
6
7
CONTD..
Main Program
Function - 4 Function - 5
7
Structure of the procedure oriented programs
8
OOP PARADIGM
8
9
CONTD..
Features of OOP:
Emphasis on data rather than procedure.
Programs are divided into objects.
Data structures are designed such that they
characterize the objects .
Function that operate on the data of an object are
tied together in the data structure.
Data is hidden and can’t be accessed by external
function.
Objects may communicate with each other through
functions.
New data and functions can be easily added
whenever necessary. 9
10
CONTD..
Object A Object B
Data Data
Functions Functions
Object C
Functions
Data
10
Objects
Classes
Inheritance
Polymorphism
Dynamic binding
Message passing
11
12
OBJECTS
12
13
CONTD..
STUDENT
Total
Average
13
Display
14
CLASSES
Objects are variables of the type class.
Once a class has been defined, we can create any
number of objects belonging to that class.
Each object is associated with the data of type class
with which they are created.
So, class is a collection of objects of similar type.
For example, tiger, lion and panther are members of
the class animal.
Class are user-defined data type and behave like
the built-in types of a programming language.
Example:
animal tiger;
it will create an object tiger of the class animal.14
15
15
16
CONTD..
Abstraction refers to the act of representing the
essential features without including the background
details.
Classes use the concept of abstraction and are
defined as a list of attributes and functions to
operate on these attributes.
They encapsulate all the essential properties of the
objects that are to be created.
The attributes are sometimes called data members
because they hold information.
The functions that operate on those data are
sometimes called methods or member functions.
16
17
INHERITANCE
CONTD..
Bird
Attributes
Feathers
Lay eggs
18
Example of Inheritance
19
POLYMORPHISM
Polymorphism means the ability to take more than
one form.
An operation may exhibit different behaviors in
different instances.
The behavior depends upon the types of data used
in the operation.
Example: The operation of addition.
19
20
CONTD..
The process of making an operator to exhibit
different behavior in different instances is known
as operator overloading.
Same, using a single function name to perform
different types of task is known as function
overloading.
Polymorphism plays an important role in allowing
objects having different internal structures to
share the same external interface.
20
21
CONTD..
Shape
Draw ( )
Example of Polymorphism 21
22
22
23
MESSAGE PASSING
CONTD..
A message for an object is a request for execution of
a procedure and therefore will invoke a
function(procedure) in the receiving object that
generates the desired result.
Message passing involves the name of the object,
the name of the function(message) and the
information to be sent.
Example:
employee. salary(name)
BENEFITS OF OOP
Through inheritance, we can eliminate the
redundant code and extend the use of existing
classes.
Save the development time and higher productivity.
25
26
CONTD..
The data centered design approach enables us to
capture more details of a model in implementable
form.
Object-oriented system can be easily upgraded from
small to large system.
Message passing techniques for communication
between objects makes the interface descriptions
with external systems much simpler.
Software complexity can be easily managed.
26
27
27
28
CONTD..
Object-based programming language:
It supports encapsulation and object identity.
Major features required for object-based programming
are :
Data encapsulation
Operator overloading
28
29
CONTD..
Object-oriented programming language:
object-oriented programming incorporates all
of object-based programming features along with
two additional features, inheritance and dynamic
binding.
29
30
APPLICATION OF OOP
Real-time systems.
Simulation and modeling.
Object-oriented databases.
CIM/CAM/CAD systems.
30
31
C++ CONCEPT
C++ is an object-oriented programming
language.
It is developed by Bjarne Stroustrup at AT&T
bell laboratories in 1980.
C++ is a superset of C.
31
32
Include Files
Class Declarations
32
33
CONTD..
Member Functions
Server
Class Definitions
Client
Member Function Program
CONTD..
34
35
TOKENS
35
36
KEYWORDS
Example:
auto, class, return, int, friend, public, private,
protected, wchar_t, bool, etc...
36
37
IDENTIFIERS
37
38
CONTD..
38
39
CONSTANTS
CONTD..
40
41
APPLICATION OF C++
Editors
Compilers
Databases
Communication systems
Complex real-life application systems
For to build special object-oriented libraries which can
be used later by the programmers.
41
42
Data types
42
43
class:
A class is collection of object.
In C++, structure is known as class. But it has some
difference.
In structure, we can declare only a variable. Where in class
we can declare a variable (data member) as well as function
(member function).
enumerated:
An enumerated data type provides a way for attaching
names to numbers.
The enum keyword is used to create a new data type.
44
Example:
Syntax: enum day
enum newdatatype {
{ Mon,
Value1, Tues,
Value2, Wed
Value3 };
};
In the above example,
Mon has value 0, Tues
has value 1 and Wed
has value 2.
Value of Mon, Tues and
Wed is not changed
during the execution of
the program.
45
46
Arrays:
An array is fixed size sequenced collection of elements of
same data type.
Syntax: data-type array-name[size];
Example: int a[10];
In above example, a is an array which stores 10
different elements of integer data type.
By default, the index of array is start from 0.
46
Functions:
Function is a group of instruction to perform a specific
task.
There is mainly two types of functions are available.
Library function
data-type *variable-name;
Example:
DEFINING CONSTANTS
Symbolic constants can be created by two ways.
Using the qualifier constant
Using constant,
Example:
DECLARATION OF VARIABLES
There is some difference between C and C++
regarding to the declaration of the variable.
In C, all the variables are declared at the beginning
of the scope.
Where in C++, we can declare a variable anywhere
before they are used in the executable statements.
Syntax of variable declaration: data-type
variable-name;
Example: int i;
In C++, we can also declare a variable in the
initialization section of the for loop statement which
is not possible with C.
49
50
Example:
void main()
{
int sum=0;
for (int i=1;i<10;i++)
{
sum = sum + i;
}
cout<< “ Sum of 1 to 10 is :” << sum;
}
50
51
REFERENCE VARIABLES
51
Example:
int a=15,b=25,total;
total = a + b;
int & sum = total;
cout<<total; // it will print 40
cout<<sum; // it will print 40
sum = 20;
cout<<total; // it will print 20
cout<<sum; // it will print 20
A reference variable must be initialized
at the time of declaration.
52
OPERATOR IN C++
C++ has rich set of operators all C operators are valid
in C++.
In addition, C++ introduce some new operators. Some
new operators:-
:: scope resolution operator
::* pointer-to-member declarator
->* pointer-to-member operator
.* pointer-to-member operator
delete memory release operator
new memory allocation operator
53
54
55
MEMBER DEREFERENCING OPERATORS
• The Member dereferencing operators use in the
class, the following
the member dereferencing operators use for
different purpose.
57
Syntax:
pointer-variable = new data-type;
Example:
int *p = new int;
float *q = new float;
Using the new operator we can also initialize
the memory.
Syntax:
Pointer-variable = new data-type(value);
Example:
int *p= new int(25);
float *q = new float(10.5);
The new operator can be used to create a
memory space for any data type including
user-defined types such as arrays, structures
and classes.
58
Syntax:
pointer-variable = new data-type[size];
where, size specifies the number of elements in the
array.
Example:
int *p = new int [10];
which create a memory space for an array of 10
integers.
When a data object is no longer needed, it is destroyed
to release the memory space for reuse.
Syntax:
delete pointer-variable;
Example:
delete p;
delete q;
59
To free a dynamically allocated array we can use the
delete operator.
Syntax:
delete [size] pointer-variable;
The size specifies the number of elements in the
array to be freed.
Example:
delete [ ] p;
60
61
MANIPULATORS
Example:
Manipulators are operators #include<iostream.h>
that are used to format the #include<conio.h>
output. void main()
Most commonly used {
manipulators are endl and clrscr();
setw. int a=15,b=420,c=3542;
The endl manipulator is cout<< “a =”<<a<<endl;
used in the output cout<< “b =”<<b<<endl;
statement when we want to cout<< “c =”<<c<<endl;
insert a linefeed. getch();
It has the same effect as }
using the newline character
“\n”. Output:
a = 15
b = 420
c = 3542 61
The setw manipulator is #include<iostream.h>
used to format the output. #include<conio.h>
void main()
When we use the setw {
manipulator the output is clrscr();
formatted in right justified. int a=15, b=420, c=3542;
Before using the setw cout<< “a =”<<setw(4)<<a<<endl;
manipulator we have to cout<< “b =”<<setw(4)<<b<<endl;
include the header file cout<< “c =”<<setw(4)<<c<<endl;
iomanip. getch();
}
Syntax:
setw(width) Output:
Where, width specifies the a = 15
field width. b = 420
c =3542
62
TYPE CAST OPERATOR
Casts can be used to convert objects of any scalar
type to or from any other scalar type.
float x = 3.1;
int i;
i = (int)x; //the value of I is now 3
63
Types cast operators
Casts can be used to convert objects of any scalar type to or
from any other scalar type.
Example
float x = 3.1;
int i;
i = (int)x; //the value of I is now 3