OBJECT ORIENTED PROGRAMMING USING C++
Difference between object oriented programming and procedural oriented programming
language?
OOP’s Procedural Programming
1 Here emphasis is given on data rather than Here emphasis is given on
procedures. algorithms or steps through
which a problem is solved.
2 It follows bottom up approach in program design It follows top down approach in
program design
3 Its data hiding features prevents accidental change Data can freely move among
in data functions
4 Its features like data encapsulation, polymorphism, Such features are not available
inheritance are present. in this type of programming.
5 Reusability is achieved using Inheritance,functions. Reusability is achieved using
Functions only.
6 Examples of such languages are C++,java,VB.Net,C# Examples of such languages are
etc. B,BCPL,C ,VB etc
7 Run Time errors can be handled. Run Time errors can’t be
handled .
Difference between Class and Structure?
Structure Class
It is declared with the keyword struct It is declared with the keyword class
By default all members are public By default all members are private
OBJECT-ORIENTED PROGRAMMING:
Object-oriented programming language is a feature that allows a mode of
modularizing programs by forming separate memory area for data as well as functions
that is used as object for making copies of modules as per requirement.
There are several fundamental or key concepts in object-oriented programming. Some
of these are shown below.
a ) Objects:
Objects are primary run-time entities in an object-oriented programming. They
may stand for a thing that has specific applications, for example,a person, any data
item related to program, including user-defined data types.Objects occupy space in
memory. Every object has its own properties or features. An object is a specimen of a
class.
Objects are the instances of a class or we can say that objects are the variables
of a class.
b) Class: A class is a user defined data type that consist of data member and member
function into a single unit under the access specifiers public ,private or protected.
A class is a collection of similar types of objects.
A class is grouping of objects having identical properties, common behavior,
and shared relationship.
A class is the accomplish of abstract data type. It defines the nature and
methods that act on the data structure and abstract data type respectively. Specimens
are also known as objects.
In other words, a class is a grouping of objects having identical properties,
common behavior, and shared relationship. A class is a model of the object.
c) Method:
An operation required for an object or entity when coded in a class is called a
method.
The operations that are required for an object are to be defined in a class. All
objects in a class perform certain common actions or operations. Each action needs an
object that becomes a function of the class that defines it and is referred to as a
method.
d) Data Abstraction:
Abstraction directs to the procedure of representing essential features without
including the background details.
Classes use the theory of abstraction and are defined as a list of abstract
properties such as size, cost, height, and few functions to operate on these properties.
Data abstraction is the procedure of identifying properties and methods related to a
specific entity as applicable to the application.
e) Encapsulation:
The packing of data and functions into a single component is known as
encapsulation.
With encapsulation data hiding can be accomplished. Data hiding is an
important feature. By data hiding an object can be used by the user without knowing
how it works internally.
f) Inheritance:
Inheritance is the method by which objects of one class get the properties of
objects of another class.
In object-oriented programming, inheritance provides the thought of
reusability. The programmer can add new properties to the existing class without
changing it.
g) Polymorphism:
Polymorphism allows the same function to act differently in different classes.
It is an important feature of OOP concept and has the ability to take more than
one form.
h) Dynamic Binding:
Binding means connecting one program to another program that is to be
executed in reply to the call.
Dynamic binding is also known as late binding. The code present in the
specified program is unknown till it is executed. It is analogous to inheritance and
polymorphism.
i) Message passing:
Object oriented programming includes objects which communicate with each
other.
Declaring classes that define objects and their actions.
Declaring objects from classes.
Implementing relation between objects.
j) Reusability:
Object-oriented technology allows reusability of the classes by extending them
to other classes using inheritance.
Once a class is defined, the other programmer can also use it in their programs
and add new features to the derived classes. Once verified the qualities of base classes
need not be redefined.
k) Delegation:
When object of one class is used as data member in other class, such
composition of objects is known as delegation.
l) Genericity:
The software components of a program have more than one version depending
on the data types of arguments. This feature allows declaration of variables without
specifying exact data type. The compiler identifies the data type at run-time.
Access Specifier/Access Modifiers
An access specifier is one of the following three keywords: private, public or protected. These
specifiers modify the access rights that the members following them acquire:
private members of a class are accessible only from within other members of the same
class or from their friends.
protected members are accessible from members of their same class and from their
friends, but also from members of their derived classes.
Finally, public members are accessible from anywhere where the object is visible.
By default, all members of a class declared with the class keyword have private access for all
its members. Therefore, any member that is declared before one other class specifier
automatically has private access.
HOW TO DEFINE A FUNCTION OUTSIDE THE CLASS:
A function can be defined outside the class by using scope Resolution operator(::).A scope
resolution operator is used to access global value of the variable.
#include<iostream.h>
#include<conio.h>
Class sum
{
private:
Int x,y,z;
public:
void input();
void process();
void show();
};
void sum::input()
{
cout<<”enter 2 nos”;
cin>>x>>y;
}
void sum::process()
{
z=x+y;
}
void sum::show()
{
cout<<”sum=”<<z;
}
void main()
{
sum obj;
obj.input();
obj.process();
obj.show();
getch();
}
HOW to access Private Member Function
A private member function can not be accessed outside the class.However it can be accessed
within the member function present in the same class.
#include<iostream.h>
#include<conio.h>
class sum
{
private:
int x,y,z;
void getdata()
{
cout<<"Enter two numbers";
cin>>x>>y;
}
void calculate()
{
z=x+y;
}
public:
void display()
{
getdata();
calculate();
cout<<"sum="<<z;
}
};
void main()
{
sum obj;
//obj.getdata(); can't access
//obj.calculate(); can't access
obj.display();
getch();
}
PASSING PARAMETER TO FUNCTION
#include<iostream.h>
#include<conio.h>
class sum
{
private:
int x,y,z;
void getdata()
{
cout<<"Enter two numbers";
cin>>x>>y;
}
void calculate()
{
z=x+y;
}
public:
void display()
{
getdata();
calculate();
cout<<"sum="<<z;
}
};
void main()
{
sum obj;
//obj.getdata(); can't access
//obj.calculate(); can't access
obj.display();
getch();
}
PASSING PARAMETER TO FUNCTION
#include<iostream.h>
#include<conio.h>
class sum
{
private:
int x,y,z;
public:
void getdata(int a,int b)
{
x=a;
y=b;
}
void calculate()
{
z=x+y;
}
void display()
{
cout<<"sum="<<z;
}
};
void main()
{
sum obj;
obj.getdata(2,5);
obj.calculate();
obj.display();
getch();
}