Anda di halaman 1dari 25

Object Orientation

Fundamentals & Overview to


Programming language C++

Bhagwant Singh

Index

Procedure Oriented Programming

OOP Paradigm

Basic concept of object oriented program

Benefits of OOPs

Evolution of C++.

Input and Output operator.

Structure of C++ program

Tokens

Data types

Procedure-Oriented
Programming
Viewed as the sequence of things to be done

The primary focus is on functions


Hierarchical decomposition
In a multi-function program, many
important data items are placed as global
but in a large program it is very difficult to
identify what data is used by which
function.
Functions are action-oriented and do not
really corresponding to the element of the
problem.

OOPS Paradigm
Object Oriented Programming (OOP) is an approach to program organization and
development that attempts to eliminate some of the pitfalls of conventional programming
methods by incorporating the best of structured programming features with several
powerful new concepts.
Ties data more closely to the function that operate on it.
OOP allows decomposition of a problem into a number of entities called objects and then
builds data and function around these objects.

Features
Emphasis is on data rather than procedure.
Programs are divided into what are known as objects.
Data structures are designed such that they characterize the objects.
Functions that operate on the data of an object are ties together in the data structure.
Data is hidden and cannot be accessed by external function.
Objects may communicate with each other through function.
New data and functions can be easily added whenever necessary.
Follows bottom up approach in program design.

Basic Concepts of OOPs

Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic binding
Message passing

Benefits
Through inheritance, we can eliminate redundant code extend the use of existing
Classes.
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.
The principle of data hiding helps the programmer to build secure program that
can not be invaded by code in other parts of a programs.
It is possible to have multiple instances of an object to co-exist without any
interference.
It is possible to map object in the problem domain to those in the program.
It is easy to partition the work in a project based on objects.
The data-centered design approach enables us to capture more detail of a model
can implemental form.
Object-oriented system can be easily upgraded from small to large system.
Message passing techniques for communication between objects makes to
interface descriptions with external systems much simpler.
Software complexity can be easily managed.

Evolution Of C++
C++ was developed by Bjarne
Stroustrup at AT&T Bell Laboratories
in Murray Hill, New Jersey, USA, in the
early 1980s.
C++ is an extension of C
Various features of C++ enable
creating of abstract data types, inherit
properties from existing data types
and support polymorphism.

C++ Program
#include<iostream>
Using namespace std;
int main()
{
cout<< Welcome to C++\n;
return 0;
}

Namespace is a new concept introduced


by the ANSI C++ standards committee.
This defines a scope for the identifiers
that are used in a program.
In C++, main () returns an integer value
to the operating system. Therefore, every
main () in C++ should end with a return
(0) statement; otherwise a warning an
error might occur. Since main () returns
an integer type for main () is explicitly
specified as int. Note that the default
return type for all function in C++ is int.

Input And Output Operator


Input Operator :
Cin>> size;
Cin is Standard input stream
It causes the program to wait for the user to type in a
number
The operator >> is known as extraction or get from
operator.
Output Operator :
Cout << The Size is << size;
Cout is Standard output stream
The operator << is called the insertion or put to
operator.

#include<iostream.h>
Using namespace std
void main()
{
int a=1000;
cout << a is <<a<<endl;
cout <<enter number<<endl;
int b;
cin >>b;
a+=b;
cout << a is <<a<< and b is <<b<<endl;
}

Structure in C++
A typical C++ program would
contain four sections.
It is a common practice to
organize a program into three
separate files.
The class declarations are placed
in a header file
The definitions of member
functions go into another file.
Finally, the main program that
uses the class is places in a third
file which includes: the previous
two files as well as any other file
required.

Classes and Objects


A Class is a static description of a type
It encapsulates the data and all legal
operations on that data
It also defines who has access to that data
An object is an instance of a class
(sometimes we say an instantiation)

#include<iostream.h>
using namespace std;
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 person : : display(void)
{
cout << \nNameame: << name;
cout << \nAge: << age;
}
Int main()
{
person p;
p.getdata();
p.display();
Return 0;
}

Client Server Model

Tokens
Smallest individual units in a programe is called as
tokens.
Keywords are explicitly reserved identifier that cant
be used as names for the programe variable.
Identifiers refer to the names of variables, functions,
arrays , classes etc
Major difference between the C and C++ is the limit
on the length of a name. C recognize only first 32
characters of the name but there no such limit in C+
+
Wchar_t type is a wide-character literal introduce by
C++

Data types
C++ Data
types
User
Defined
Structure
Union
Class
Enumerati
on

Build in Type

Integral Type

Int

Derived
type
Array
Function
Pointer
Reference

Char

Void

Floating Type

float

Doubl
e

Void
Uses of Void
To specify the return type of a function when
it is not returning any value.
To indicate empty argument list to a function
To declare a generic Pointers.
Void *gp;
Statement 1
Int *ip;
gp = ip;
Statement 3
*ip = *gp;

Structure v/s Union


A structure is defined with
struct keyword.
All members of the structure
can
be
manipulated
simultaneously.
The size of structure object
is equal to the sum of the
individual
size
of
the
member function.
Structure
member
are
allocated distinct memory
location.
Structure are not considered
as memory efficient.

An Union is defined with


union keyword.
Members of the union can
be manipulated only one
at a time.
The size of union object is
equal to the size of the
largest member function.
Union
member
share
common memory space
for their exclusive usage.
Union are not considered
as memory efficient.

Enumeration
An enum is a user defined data type which
provides away for attaching name to a
numbers, thereby increasing comprehensibility
of the code.
enum shape(circle, square, triangle);
enum color(red, blue, green, yellow);
In C++ the tag name shape and color becomes
new types names. By using these tag names we
can declare new variables.
Shape ellipse; // ellipse is of type shape
Color background; // background is of type color

Enumeration
Statement 1
Color background = blue;

Statement 2
Color background = 7;

Statement 3
Color background = (color) 7;

Statement 4
Int c = blue;

Storage Classes
In addition to data types, variable also
have storage classes.
A storage class specifies the life time
and visibility with in the program.
There are four storage classes
Automatic
External
Static
Register

Derived Data types


Array
Character Array
(How will you declare a character array
that store xyz ? )

Functions
Pointer
Statement 1: Char * const ptr1 =
GOOD;
Statement2: Const char * const cp =
xyz;

Symbolic Constant
There are two ways to create symbolic constant:
Using the quantifier const
Defining a set of integer constant using enum keyword.

Anything declared constant cant be modified but


the implementation in C++ is bit different we can
use constant in a constant expression. Such as
const int size = 10;
char name[size];

C++ required const to be initialized.


In C++ const has a local scope. To make it global
we have to add extern keyword

Anda mungkin juga menyukai