Oops 1
Oops 1
PROGRAMMING
• The Primary motivation in all programming styles is the concern to handle the increasing
complexity of programs that are reliable and maintainable.
• Data move freely between functions where there is chance for the intruders to
hack the data.
• Critical applications should be designed in such a way that the data should be
protected when they are transferred between functions
Object-Oriented Programming
Paradigm
• It ties data more closely to the functions that operate on it, and protects
it from accidental modification from outside functions.
Some characteristics exhibited by procedure-oriented programming are:
functions.
data structure.
1. Objects
2. Classes
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
OBJECTS
• Objects are the basic run-time entities in an object-oriented system.
They may represent a person, a place, a bank account, a table of data or
any item that the program must handle.
• The term objects means a combination of data and program that represent
some real word entity.
• CLASS:
• A group of objects that share common properties for data
part and some program part are collectively called as class.
• Classes use the concept of abstraction and are defined as size, width and
cost and functions to operate on the attributes.
DATA ENCAPSALATION :
• The wrapping up of data and function into a single unit (called class)
is known as encapsulation.
• These functions provide the interface between the objects data and the
program.
Encapsulation
“Mechanism that associates the code and the data it manipulates into a
single unit and keeps them safe from external interference and misuse.”
INHERITENCE :
• Inheritance is the process by which objects of one class
acquire the properties of another class.
Meaning of Polymorphism
Poly Multiple
Morphing Actions
• Message passing involves specifying the name of the object, the name
of the function (message) and information to be sent.
BENEFITS OF OOP:
The most popular application of oops up to now, has been in the area of
user interface design such as windows. There are hundreds of windowing
systems developed using oop techniques.
The endl manipulator, when used in an output statement, causes a line feed to
be insert.(just like \n). If m=2597, n=14 & p=175
Example:
cout<<”m=”<<m<<endl
;
cout<<”n=”<<n<<endl;
cout<<”p=”<<p<<endl;
Example:
cout<<setw(5)<<”m=”<<m<<endl
;
cout<<setw(5)<<”n=”<<n<<endl;
cout<<setw(5)<<”p=”<<p<<endl;
Size & range of C++ basic data types:
Built-in Data types in C++ using sign & size qualifiers.
struct tag_name
{
data_type member1;
data_type member2;
……
……… …..
1. The template terminated with a semicolon
Example:
struct book
{
char title[20];
char author[15];
int pages;
float price;
};
❖ The keyword struct declares a structure to hold the details of four fields,
namely title, author, pages and price.
❖ book is the name of the structure and also called as ‘STRUCTURE TAG’.
In unions all members use the same location – common memory locations
Union may contain many members of different type but can handle only one member at
a time.
union union_name
{
data_type
member1;
Example
data_type mrmber2;
union
… … .. …..
book
} var1, var2, .. ;
{
char
title[15];
char *author;
int pages;
DECLARATION OF VARIABLES:
In ANSI C all the variable which are to be used in programs must be declared at the beginning of the
program.
But in C++ we can declare the variables any where in the program where it requires.
It makes the program easier to understand because the variables are declared in the context of their use.
Example:
main( )
{
float x;
float sum=0.0;
for(int i=1;i<5;i++)
{
cin>>x;
sum=sum+x
}
float average;
average=sum/4;
cout<<average;
return 0;
REFERENCE VARIABLES:
Example:
float total=1500;
float
&sum=total;
* char to int
* int to float
* float to double
Selection
The if statement: Statements
The if statement is implemented in three forms:
1. simple if statement
2. if..else statement
3. if..else if ladder
1. Simple if statement Syntax:
if (condition)
{
Statement block;
}
Eg.
#include<iostream.h
> void main()
{
int a,b;
cin>>a>>b;
if(a>b) { cout<<a<< “ is greater than
“<<b;}
else { cout<<b<<“ is greater than “<<a; }
}
Outout:
3. if..else if ladder
if (condition1)
{Statement
block1…….;} else
if(condition2)
{Statement block2.......;}
else if(condition3)
…………
else
{Statement blockN……;}
Eg. #include<iostream.h>
void main()
{
int a,b,c;
cin>>a>>b>>c;
if((a>b) && (a>c)) { cout<<a<< “ is the largest number”;}
This is a multiple-branching statement where, based on a condition, the
control
is transferred to one of the many possible points;
switch(expr)
{
case 1:
action1;
break
; case
2:
action2
;
break;
..
..
default:
1. The while statement:
Syntax:
while(condition)
{
Statements;
}
2.LECTURE NOTES ON Object Oriented Programming Using C++ by Dr. Subasish Mohapatra,
Department of Computer Science and Application College of Engineering and Technology,
Bhubaneswar Biju Patnaik University of Technology, Odisha