0% found this document useful (0 votes)
24 views19 pages

chap 1 oopnotes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views19 pages

chap 1 oopnotes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Chpater 1

Principles of object Oriented Programming

Procedure Oriented Programming Language


In the procedure oriented approach, the problem is viewed as sequence of things
to be done such as reading, calculation and printing.
Procedure oriented programming basically consist of writing a list of instruction
or actions for the computer to follow and organizing these instruction into groups
known as functions.

The disadvantage of the procedure oriented programming languages is:


1. Global data access

2. It does not model real word problem very well

3. No data hiding

Characteristics of procedure oriented programming:


1. Emphasis is on doing things(algorithm)

2. Large programs are divided into smaller programs known as functions.

3. Most of the functions share global data

4. Data move openly around the system from function to function

5. Function transforms data from one form to another.

6. Employs top-down approach in program design

Object Oriented Programing


“Object oriented programming as an approach that provides a way of modularizing
programs by creating partitioned memory area for both data and functions that can
be used as templates for creating copies of such modules on demand”.
Object A Object B

Data Data
Communication

Functions Functions

Object C

Functions

Data

Features /Characteristics of the Object Oriented programming

1. Emphasis is on doing rather than procedure.

2. programs are divided into what are known as objects.

3. Data structures are designed such that they characterize the objects.

4. Functions that operate on the data of an object are tied together in the data

structure.
5. Data is hidden and can’t be accessed by external functions.

6. Objects may communicate with each other through functions.

7. New data and functions can be easily added.

8. Follows bottom-up approach in program design.

Difference Between POP and OOP


Procedure Oriented Programming Object Oriented Programming
1 program is divided into small parts program is divided into parts called
called functions. objects.
Importance is not given to data but to Importance is given to the data rather
2 functions as well as sequence of than procedures or functions because it
actions to be done. works as a real world.
3 follows Top Down approach. OOP follows Bottom Up approach.
4 It does not have any access specifier. OOP has access specifiers named Public,
Private, Protected, etc.
5 Data can move freely from function objects can move and communicate
to function in the system. with each other through member
functions.
6 To add new data and function in POP OOP provides an easy way to add new
is not so easy. data and function.
Most function uses Global data for In OOP, data can not move easily from
7 sharing that can be accessed freely function to function,it can be kept
from function to function in the public or private so we can control the
system. access of data.
8 It does not have any proper way for OOP provides Data Hiding so provides
hiding more
data so it is less secure. security.
In OOP, overloading is possible in the
9 Overloading is not possible. form of Function Overloading and
Operator Overloading.
Example of POP Example of OOP : C++, JAVA,
10 : C, VB, FORTRAN, Pascal. VB.NET, C#.NET.
BENEFITS OF OOP:

Oop offers several benefits to both the program designer and the user. Object-
oriented contributes to the solution of many problems associated with the
development and quality of software products.
The principal advantages are :
1. Through inheritance we can eliminate redundant code and extend the use
of existing classes.
2. We can build programs from the standard working modules that
communicate with one another, rather than having to start writing the code
from scratch. This leads to saving of development time and higher
productivity.
3. This principle of data hiding helps the programmer to build secure
programs that can’t be invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist with out
any interference.
5. It is easy to partition the work in a project based on objects.
6. Object-oriented systems can be easily upgraded from small to large
systems.
7. Message passing techniques for communication between objects
makes the interface description with external systems much simpler.
8. Software complexity can be easily managed.

APPLICATION 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.
Application of oop includes.

1. Real – Time systems.

2. Simulation and modeling

3. Object oriented databases.

4. Hypertext,hypermedia and expertext.

5. Al and expert systems.

6. Neural networks and parallel programming.

7. Dicision support and office automation systems.

8. CIM / CAM / CAD system.

BASIC CONCEPTS OF OBJECTS ORIENTED PROGRAMMING

1. Objects

2. Classes

3. Data abstraction and encapsulation

4. Inheritance

5. Polymorphism

6. Dynamic binding

7. Message passing

OBJECTS

1. Objects are the basic run-time entities in an object-oriented system.

2. They may represent a person, a place, a bank account, a table of data or any
item that the program must handle.
3. The fundamental idea behind object oriented approach is to combine both
data and function into a single unit and these units are called objects.
4. The term objects means a combination of data and program that represent
some real word entity.
5. For example: consider an example named Amit; Amit is 25 years old and
his salary is 2500. The Amit may be represented in a computer program as
an object. The data part of the object would be (name: Amit, age: 25, salary:
2500)

CLASS:

A group of objects that share common properties for data part and
some program part are collectively called as class.

In C ++ a class is a new data type that contains member variables and


member functions that operate on the variables.

Object: Student STUDENT

DATA T otal
Name
Date-of-birth
Marks Av erage

FUNCTIONS
Total
Average isplay
Display

DATA ABSTRACTION :

Abstraction refers to the act of representing essential features without


including the back ground details or explanations. 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. The data is not accessible to the outside world
and only those functions which are wrapped in the class can access it. These
functions provide the interface between the objects data and the program.
INHERITENCE :

Inheritance is the process by which objects of one class acquire the


properties of another class. In the concept of inheritance provides the idea
of reusablity. This mean that we can add additional features to an existing
class with out modifying it. This is possible by desining a new class will have
the combined features of both the classes.

POLYMORPHISIM:

1. Polymorphism means the ability to take more than one form. An


operation may exhibit different instance.

2. The behaviour depends upon the type of data used in the operation.
3. A language feature that allows a function or operator to be given more
than one definition. The types of the arguments with which the function
or operator is called determines which definition will be used.
4. Overloading may be operator overloading or function overloading.
5. Function overloading means a function with same name and different
parameters.
DYNAMIC BINDING :

Binding refers to the linking of a procedure call to the code to


the executed in response to the call. Dynamic binding means the code
associated with a given procedure call is not known untill the time of the
call at run-time. It is associated with a polymorphic reference depends upon
the dynamic type of that reference.
MESSAGE PASSING :
An object oriented program consists of a set of objects that communicate
with each other.
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 specifying the name of
the object, the name of the function (message) and information to be sent.

Employee .Salary (name)

Object Information

Message

BASIC DATA TYPES IN C++

C ++ Data Types

User defined type Built in types Derived type


Structure Array
Function
Union pointer
Class
enumeration
void

Integral type Floating point

float double
int char
Data types in C++ can be classified under various categories.

TYPE BYTES RANGE


char 1 -128 to – 127

usigned 1 0 to 265

sgned char 1 -128 to 127

int 2 -32768 to 32768

unsigned int 2 0 to 65535

singed int 2 -32768 to 32768

short int 2 -32768 to 32768


long int 4 -2147483648 to 2147483648
signed long int 4 -2147483648 to 2147483648

unsigned long int 4 0 to 4294967295

float 4 3.4E-38 to 3.4E+38

double 8 1.7E -308 to 1.7E +308

long double 10 3.4E-4932 to 1.1E+ 4932


Void:-
The type void normally used for:
1) To specify the return type of function when it is not returning any

value.
2) To indicate an empty argument list to a function.

Example:
void function(void);

Another interesting use of void is in the declaration


of genetic pointer Example:
Void *gp;
USER DEFINED DATA TYPES:

STRUCTERS AND CLASSES


We have used user defined data types such as struct,and union in C. While
these more features have been added to make them suitable for object oriented
programming. C++ also permits us to define another user defined data type
known as class which can be used just like any other basic data type to declare a
variable. The class variables are known as objects, which are the central focus of
oops.

ENUMERATED DATA TYPE:

An enumerated data type is another user defined type which


provides a way for attaching names to number, these by increasing
comprehensibility of the code. The enum keyword automatically enumerates
a list of words by assigning them values 0,1,2 and soon. This facility
provides an alternative means for creating symbolic.
Example:

enum shape { circle,square,triangle} enum


colour{red,blue,green,yellow}
enum position {off,on}

Variables:
Defination - A named memory location is called variable.
OR
It is an identifier used to store the value of particular data type in the
memory.
Since variable name is identifier we use following rules which are same as of
identifier

Rules for declaring Variables names:

The first character must be an alphabet or underscore.

It must consist of only letters, digits and underscore.

Identifiers may have any length but only first 31 characters are
significant.

It must not contain white space or blank space.

We should not use keywords as identifiers.

Upper and lower case letters are different.

Variable names must be unique in the given scope

Variable declaration: The declaration of variable gives the name for memory
location and its size and specifies the range of value that can be stored in that
location.
Syntax: Data type variable name;
Ex: int a;

Static Initialization of a variable


Syntax:-
Data type variable name= value;
Ex:- int a=10; float x=2.3;
Dynamic Initialization of a variable :-
int a;
cin>>a;
ex: - int a;
cout <<”\n Enter value:”; cin>>a;

REFERENCE VARIABLES:

1. C++interfaces a new kind of variable known as the reference variable.


2. A references variable provides an alias.(alternative name) for a
previously defined variable. For example ,if we make the variable sum
a reference to the variable total, then sum and total can be used
interchangeably to represent the variuble.
3. A reference variable is created as follows:

Synatx:
Datatype & reference –name=variable name;

Example:
float total=1500;
float &sum=total;

Here sum is the alternative name for variables total, both the variables refer to
the same data object in the memory .
A reference variable must be initialized at the time of declaration .
Note that C++ assigns additional meaning to the symbol & here & is not an
address operator
.The notation float & means reference to float.
Example:
int n[10];
int &x=n[10];
char &a=’\n’;
type casting :- refers notes
OPERATORS AND EXPRESSIONS

An operator is a symbol which represents a particular operation that can be


performed on data. An operand is the object on which an operation is
performed.

By combining the operators and operands we form an expression. An


expression is a sequence of operands and operators that reduces to a single
value.

C operators can be classified as (revise all by yourselves)

1. Arithmetic operators

2. Relational operators

3. Logical operators

4. Assignment operators

5. Increment or Decrement operators

6. Conditional operator

7. Bit wise operators

8. unary operator

9. Special operators

OPERATORS IN C++ :

C++ has a rich set of operators. All C operators are valid in C++
also. In addition. C++ introduces some new operators.

<< insertion operator

>> extraction operator

:: 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:

Scope:-Visibility or availability of a variable in a program is called as scope.


There are two types of scope.
i)Local scope
ii)Global scope
Local scope: visibility of a variable is local to the function in which it is declared.
Global scope: visibility of a variable to all functions of a program
Scope resolution operator in “::” .
This is used to access global variables if same variables are declared as local and
global.

#include<iostream.h> int a=5;


void main()
{
int a=10;
cout<<”Local a=”<<a<<endl; cout<<”Global a=”<<::a<<endl;
}

output:
Local a=10 Global a=5

Manipulators:
Manipulators are the operators used to format the data that is to be
displayed on screen.
The most commonly used manipulators are endl and setw
1.endl:-it is used in output statement and inserts a line feed. It is similar to new
line character (“\n”) ex:
………………..

cout<<”a=2”<<endl; cout<”name=sunil”<<endl;
……………….

Output: a=2 name=sunil


2.setw:-this manipulator allows a specified width for a field that is to be printed
on screen
and by default the value printed is right justified. This function is available in
header file <iomanip.h>
#include<iostream.h>
#include<iomanip.h>
void main()
{
int s=123;
cout<<"s="<<setw(10)<<s ;
}
output
s= 123

Memory management operators

C++ defines two unary operators new and delete that perform the task of
allocating and freeing the memory in a better and easier way.

1. New :- The new operator can be used to create objects of any type.

syntax- datatype variable =new datatype;

Example: int * p=new int;


Where p is a pointer of type int
int *p=new int; float *p=newfloat;

2. Delete:- If a data object is no longer needed, it is destroyed to release the


memory space for reuse.

Syntax: delete pointer-variable;

Example:
delete p;
For example:-refer lecture notes

Structure of C++ refer lecture notes:-

(note already given in a book during lecture)

Basics of C++ :-
C ++ is an object oriented programming language, C ++ was developed by
Jarney Stroustrup at AT & T Bell lab,
USA in early eighties. C ++ was developed from c and simula 67 language.
C ++ was early called ‘C with classes’.

C++ Comments:

C++ introduces a new comment symbol //(double slash).


Comments start with a double slash symbol and terminate at the end of line.
A comment may start any where in the line and what ever follows till the
end of line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment. Multi
line comments can be written as follows:

// this is an example of
// c++ program
// thank you
The c comment symbols /* ….*/ are still valid and more suitable for multi
line comments.

/* this is an example of c++ program */


Basic Input output operators:-
Output Operator:

The statement cout <<”Hello, world” displayed the string with in quotes on
the screen.
The identifier cout can be used to display individual characters, strings and
even numbers.
It is a predefined object that corresponds to the standard output stream.
Stream just refers to a flow of data and the standard Output stream normally
flows to the screen display. The cout object, whose properties are defined in
<iostream.h >represents that stream.
The insertion operator << also called the ‘put to’ operator directs the
information on its right to the object on its left.
Ex:- cout<<”\n Welcome to CPP”;

Input Operator:

The statement in>> number 1; is an input statement and causes.


The program to wait for the user to type in a number.
The number keyed in is placed in the variable number1.
The identifier cin is a predefined object in C++ that corresponds to the standard
input stream.
Here this stream represents the key board.
The operator >> is known as get from operator. It extracts value from the
keyboard and assigns it to the variable on its right.
Ex:- cin>>a;

Return Statement:

In C++ main ( ) returns an integer type value to the operating system.


Therefore every main (
) in C++ should end with a return (0) statement, otherwise a warning or an
error might occur.

Insertion (<<) and Extraction (>>) operators: the operators are use
with output and input objects ex:
cout<<”Enter n”;
cin>>n;
Cascading Of I/O Operator:
If single statement contains more than onr I/O opertaors then it is called
Cascading Of I/O Operator.
cout<<”sum=”<<sum<<”\n”;
cout<<”sum=”<<sum<<”\n”<<”average
=”<<average<<”\n”;
cin>>number1>>number2;

Structure Of A Program :
Probably the best way to start learning a programming language is by
writing a program. Therefore, here is our first program:
// my first program in C++

#includ <iostream.h>

void main ()
{
cout <<
"Hello
World!";
}
Output:-Hello World!

# include<iostream.h>
class person

{
char name[30]; int age;
public:
void getdata(void); void display(void);
};

void person :: getdata ( void )


{
cout<<”enter name”; cin>>name; cout<<”enter age”; cin>>age;
}

void display()
{
cout<<”\n name:”<<name; cout<<”\n age:”<<age;
}

void main( )
{

person p;
p.getdata();
p.display();
}
INLINE FUNCTION:

 To eliminate the cost of calls to small functions C++ proposes a new


feature called inline function.
 An inline function is a function that is expanded inline when it is
invoked .
 That is the compiler replaces the function call with the corresponding
function code.
 The inline functions are defined as follows:-
Syntax
inline functionname
{
function body;
}
Example: inline int cube ()
{
Cout<<”\n cube=”<<(a*a*a);
}
Some of the situations where inline expansion may not work are:
1. For functions returning values if a loop, a switch or a go to exists.
2. for function s not returning values, if a return statement exists.
3. if functions contain static variables.
4. if inline functions are recursive,
For example refer maual experiment number 6

You might also like