Вы находитесь на странице: 1из 5

Object and Encapsulation

What Is an Object?
Objects are the key to understanding object-oriented technology.
Real-world
objects
share
two
characteristics:
They
all
have state and behavior. Dogs have state (name, color) and behavior
(barking, fetching, wagging tail). Bicycles also have state (current gear,
current pedal cadence, current, speed) and behavior (changing gear,
changing pedal cadence, applying brakes). Identifying the state and behavior
for real-world objects is a great way to begin thinking in terms of objectoriented programming.
Software objects are conceptually similar to real-world objects: they too
consist of state and related behavior. An object stores its state
in fields (variables in some programming languages) and exposes its
behavior through methods (functions in some programming languages).
Methods operate on an object's internal state and serve as the primary
mechanism for object-to-object communication. Hiding internal state and
requiring all interaction to be performed through an object's methods is
known as data encapsulation a fundamental principle of object-oriented
programming.
Consider a bicycle, for example:

A bicycle modeled as a software object.


By attributing state (current speed, current pedal cadence, and current gear)
and providing methods for changing that state, the object remains in control
of how the outside world is allowed to use it. For example, if the bicycle only
has 6 gears, a method to change gears could reject any value that is less
than 1 or greater than 6.
Bundling code into individual software objects provides a number of benefits,
including:
1. Modularity: The source code for an object can be written and
maintained independently of the source code for other objects. Once
created, an object can be easily passed around inside the system.
1 | Page

2. Information-hiding: By interacting only with an object's methods, the


details of its internal implementation remain hidden from the outside
world.
3. Code re-use: If an object already exists (perhaps written by another
software developer), you can use that object in your program. This
allows specialists to implement/test/debug complex, task-specific
objects, which you can then trust to run in your own code.
4. Plugging and debugging ease: If a particular object turns out to be
problematic, you can simply remove it from your application and plug
in a different object as its replacement. This is analogous to fixing
mechanical problems in the real world. If a bolt breaks, you replace it,
not the entire machine.

Encapsulation in C++

Encapsulation is a process of wrapping of data and methods in a


single unit. It is achieved in C++ language by class concept.
Combining of state and behavior in a single container is known as
encapsulation. In C++ language encapsulation can be achieve
using class keyword, state represents declaration of variables on
attributes and behavior represents operations in terms of method.
Advantage of Encapsulation
The main advantage of using of encapsulation is to secure the data
from other methods, when we make a data private then these data
only use within the class, but these data not accessible outside the
class.
Real Life Example of Encapsulation in C++
The common example of encapsulation is Capsule. In capsule all
medicine are encapsulated in side capsule.

Benefits of encapsulation

Provides abstraction between an object and its clients.


Protects an object from unwanted access by clients.
Example: A bank application forbids a client to change an Account's
balance.

2 | Page

Example of Encapsulation in C++


#include<iostream.h>
#include<conio.h>
class sum
{
private: int a,b,c;
public:
void add()
{
clrscr();
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}
Output
Enter any two number:
4

3 | Page

5
Sum: 9

In above example all data and function are bind inside class sum.

GUI DEVELOPMENT
DEFINITION:
A graphical user interface is a type of user interface that allows users
to interact with electronic devices through graphical icons and visual
indicators such as secondary notation, instead of text-based user interfaces,
typed command labels or text navigation
EXPLANATION:
GUI is a program interface that takes advantage of the computers
graphics capabilities to make the program easier to use. Well-designed
graphical user interfaces can free the user from learning complex command
languages. On the other hand, many users find that they work more
effectively with a command-driven interface, especially if they already know
the command language.
Basic Components of a GUI:
Graphical user interfaces, such as Microsoft Windows and the one used by
the Apple Macintosh, feature the following basic components:
Pointer: A symbol that appears on the display screen and that you move
to select objects and commands. Usually, the pointer appears as a small
angled arrow. Text -processing applications, however, use an I-beam
pointer that is shaped like a capital I.
Pointing device: A device, such as a mouse or trackball, that enables you
to select objects on the display screen.
Icons: Small pictures that represent commands, files, or windows. By
moving the pointer to the icon and pressing a mouse button, you
can execute a command or convert the icon into a window. You can also
move the icons around the display screen as if they were real objects on your
desk.
Desktop: The area on the display screen where icons are grouped is often
referred to as the desktop because the icons are intended to represent real
objects on a real desktop.

4 | Page

Windows: You can divide the screen into different areas. In each window,
you can run a different program or display a different file. You can move
windows around the display screen, and change their shape and size at will.
Menus: Most graphical user interfaces let you execute commands by
selecting a choice from a menu.
In addition to their visual components, graphical user interfaces also
make it easier to move data from one application to another. A true GUI
includes standard formats for representing text and graphics. Because the
formats are well-defined, different programs that run under a common GUI
can share data. This makes it possible, for example, to copy a graph created
by a spreadsheet program into a document created by a word processor.
Many DOS programs include some features of GUIs, such as menus, but are
not graphics
based.
Such
interfaces
are
sometimes
called graphical character-based user interfaces to distinguish them from
true GUIs.

The First Graphical User Interface:


The first graphical user interface was designed by Xerox
Corporation's Palo Alto Research Center in the 1970s, but it was not until the
1980s and the emergence of the Apple Macintosh that graphical user
interfaces became popular. One reason for their slow acceptance was the
fact that they require considerable CPU power and a high-quality monitor,
which until recently were prohibitively expensive.

5 | Page

Вам также может понравиться