8/14/2019 Important Basic Concept in Object Oriented
1/25
Important basic concept in object oriented
Programming
Introduction
Object-oriented programming (OOP) is a programming
language model organized around "objects" rather than
"actions" and data rather than logic. Historically, a program
has been viewed as a logical procedure that takes input data,
processes it, and produces output data.
The programming challenge was seen as how to write the logic,
not how to define the data. Object-oriented programming takes
the view that what we really care about are the objects we want to
manipulate rather than the logic required to manipulate them.
Examples of objects range from human beings (described by
name, address, and so forth) to buildings and floors (whose
properties can be described and managed) down to the littlewidgets on your computer desktop (such as buttons and scroll
bars).
Getting started with object-oriented programming
The first step in OOP is to identify all the objects you want to
manipulate and how they relate to each other, an exercise often
known as data modeling. Once you've identified an object, you
generalize it as a class of objects (think of Plato's concept of the"ideal" chair that stands for all chairs) and define the kind of
data it contains and any logic sequences that can manipulate it.
Each distinct logic sequence is known as a method. A real
instance of a class is called (no surprise here) an "object" or, in
some environments, an "instance of a class." The object or class
http://searchsoa.techtarget.com/sDefinition/0,,sid26_gci212681,00.html#anchor1http://searchsoa.techtarget.com/sDefinition/0,,sid26_gci212681,00.html#anchor18/14/2019 Important Basic Concept in Object Oriented
2/25
instance is what you run in the computer. Its methods provide
computer instructions and the class object characteristics provide
relevant data. You communicate with objects - and they
communicate with each other - with well-defined interfaces
calledmessages.
The concepts and rules used in object-oriented programming
provide these important benefits:
The concept of a data class makes it possible to define
subclasses of data objects that share some or all of the main
class characteristics. Called inheritance, this property of
OOP forces a more thorough data analysis, reduces
development time, and ensures more accurate coding.
Since a class defines only the data it needs to be concerned
with, when an instance of that class (an object) is run, the
code will not be able to accidentally access other program
data. This characteristic of data hiding provides greater
system security and avoids unintended data corruption.
The definition of a class is reuseable not only by the
program for which it is initially created but also by other
object-oriented programs (and, for this reason, can be moreeasily distributed for use in networks).
The concept of data classes allows a programmer to create
any new data type that is not already defined in the
language itself.
Simula was the first object-oriented programming language.
Java, Python, C++, Visual Basic .NET and Ruby are the most
popular OOP languages today. The Java programming language
is designed especially for use in distributed applications on
corporate networks and the Internet. Ruby is used in many Web
applications. Curl, Smalltalk, Delphi and Eiffel are also
examples of object-oriented programming languages.
OOPSLA is the annual conference for Object-Oriented
8/14/2019 Important Basic Concept in Object Oriented
3/25
Object-Oriented Programming (OOP) is the most dramatic
innovation in software development in the last decade. It ranks
in importance with the development of the first higher-level
languages at the dawn of the computer age. Sooner or later,
every programmer will be affected by the object-oriented
approach to program design
object-oriented programming
A type ofprogrammingin whichprogrammers definenot only the data type of a data structure, but also thetypes of operations (functions) that can be applied tothe data structure. In this way, the data structurebecomes an objectthat includes both data andfunctions. In addition, programmers can createrelationships between one object and another. Forexample, objects can inherit characteristics from
other objects.One of the principal advantages of object-oriented
programming techniques over proceduralprogramming techniques is that they enableprogrammers to create modules that do not need tobe changed when a new type of object is added. A
programmer can simply create a new object thatinherits many of its features from existing objects.
This makes object-oriented programs easier tomodify.To perform object-oriented programming, one needsan object-orientedprogramming language (OOPL).Java, C++ andSmalltalkare three of the more popularlanguages, and there are also object-oriented
http://www.webopedia.com/TERM/O/program.htmlhttp://www.webopedia.com/TERM/O/programmer.htmlhttp://www.webopedia.com/TERM/O/data_type.htmlhttp://www.webopedia.com/TERM/O/data_structure.htmlhttp://www.webopedia.com/TERM/O/function.htmlhttp://www.webopedia.com/TERM/O/object.htmlhttp://www.webopedia.com/TERM/O/data.htmlhttp://www.webopedia.com/TERM/O/module.htmlhttp://www.webopedia.com/TERM/O/feature.htmlhttp://www.webopedia.com/TERM/O/programming_language.htmlhttp://www.webopedia.com/TERM/O/Java.htmlhttp://www.webopedia.com/TERM/O/C_plus_plus.htmlhttp://www.webopedia.com/TERM/O/Smalltalk.htmlhttp://www.webopedia.com/TERM/O/program.htmlhttp://www.webopedia.com/TERM/O/programmer.htmlhttp://www.webopedia.com/TERM/O/data_type.htmlhttp://www.webopedia.com/TERM/O/data_structure.htmlhttp://www.webopedia.com/TERM/O/function.htmlhttp://www.webopedia.com/TERM/O/object.htmlhttp://www.webopedia.com/TERM/O/data.htmlhttp://www.webopedia.com/TERM/O/module.htmlhttp://www.webopedia.com/TERM/O/feature.htmlhttp://www.webopedia.com/TERM/O/programming_language.htmlhttp://www.webopedia.com/TERM/O/Java.htmlhttp://www.webopedia.com/TERM/O/C_plus_plus.htmlhttp://www.webopedia.com/TERM/O/Smalltalk.html8/14/2019 Important Basic Concept in Object Oriented
4/25
programming language
A vocabulary and set of grammatical rules for instructing acomputerto perform specific tasks. The term programming
language usually refers to high-level languages, such as
BASIC, C, C++, COBOL, FORTRAN,Ada, andPascal. Each
language has a unique set ofkeywords (words that it
understands) and a specialsyntaxfor organizingprogram
instructions.
High-level programming languages, while simple compared to
human languages, are more complex than the languages thecomputer actually understands, calledmachine languages.
Each different type ofCPUhas its own unique machine
language.
Lying between machine languages and high-level languages
are languages calledassembly languages. Assembly languages
are similar to machine languages, but they are much easier to
program in because they allow aprogrammerto substitutenames for numbers. Machine languages consist of numbers
only.
Lying above high-level languages are languages calledfourth-
generation languages (usually abbreviated 4GL). 4GLs are far
http://www.webopedia.com/TERM/P/computer.htmlhttp://www.webopedia.com/TERM/P/high_level_language.htmlhttp://www.webopedia.com/TERM/P/BASIC.htmlhttp://www.webopedia.com/TERM/P/C.htmlhttp://www.webopedia.com/TERM/P/C_plus_plus.htmlhttp://www.webopedia.com/TERM/P/COBOL.htmlhttp://www.webopedia.com/TERM/P/FORTRAN.htmlhttp://www.webopedia.com/TERM/P/Ada.htmlhttp://www.webopedia.com/TERM/P/Pascal.htmlhttp://www.webopedia.com/TERM/P/language.htmlhttp://www.webopedia.com/TERM/P/keyword.htmlhttp://www.webopedia.com/TERM/P/syntax.htmlhttp://www.webopedia.com/TERM/P/program.htmlhttp://www.webopedia.com/TERM/P/instruction.htmlhttp://www.webopedia.com/TERM/P/machine_language.htmlhttp://www.webopedia.com/TERM/P/CPU.htmlhttp://www.webopedia.com/TERM/P/assembly_language.htmlhttp://www.webopedia.com/TERM/P/programmer.htmlhttp://www.webopedia.com/TERM/P/name.htmlhttp://www.webopedia.com/TERM/P/fourth_generation_language.htmlhttp://www.webopedia.com/TERM/P/fourth_generation_language.htmlhttp://www.webopedia.com/TERM/P/computer.htmlhttp://www.webopedia.com/TERM/P/high_level_language.htmlhttp://www.webopedia.com/TERM/P/BASIC.htmlhttp://www.webopedia.com/TERM/P/C.htmlhttp://www.webopedia.com/TERM/P/C_plus_plus.htmlhttp://www.webopedia.com/TERM/P/COBOL.htmlhttp://www.webopedia.com/TERM/P/FORTRAN.htmlhttp://www.webopedia.com/TERM/P/Ada.htmlhttp://www.webopedia.com/TERM/P/Pascal.htmlhttp://www.webopedia.com/TERM/P/language.htmlhttp://www.webopedia.com/TERM/P/keyword.htmlhttp://www.webopedia.com/TERM/P/syntax.htmlhttp://www.webopedia.com/TERM/P/program.htmlhttp://www.webopedia.com/TERM/P/instruction.htmlhttp://www.webopedia.com/TERM/P/machine_language.htmlhttp://www.webopedia.com/TERM/P/CPU.htmlhttp://www.webopedia.com/TERM/P/assembly_language.htmlhttp://www.webopedia.com/TERM/P/programmer.htmlhttp://www.webopedia.com/TERM/P/name.htmlhttp://www.webopedia.com/TERM/P/fourth_generation_language.htmlhttp://www.webopedia.com/TERM/P/fourth_generation_language.html8/14/2019 Important Basic Concept in Object Oriented
5/25
removed from machine languages and represent the class of
computer languages closest to human languages.
Regardless of what language you use, you eventually need toconvertyour program into machine language so that the
computer can understand it. There are two ways to do this:
See compile andinterpreterfor more information about these
two methods.
The question of which language is best is one that consumes alot of time and energy among computer professionals. Every
language has its strengths and weaknesses. For example,
FORTRAN is a particularly good language for processing
numericaldata, but it does not lend itself very well to
organizing large programs. Pascal is very good for writing
well-structured and readable programs, but it is not as flexible
as the C programming language. C++ embodies powerful
object-orientedfeatures, but it is complex and difficult tolearn.
The choice of which language to use depends on the type of
computer the program is to run on, what sort of program it is,
and the expertise of the programmer.
ADVANTAGES OF OOPS
Why is everyone so excited about OOP? The chief problem with
computer programs is complexity. Large programs are probably
the most complicated entities ever created by humans. Because of
this complexity, programs are prone to error, and software errors
http://www.webopedia.com/TERM/P/convert.htmlhttp://www.webopedia.com/TERM/P/interpreter.htmlhttp://www.webopedia.com/TERM/P/data.htmlhttp://www.webopedia.com/TERM/P/object_oriented.htmlhttp://www.webopedia.com/TERM/P/feature.htmlhttp://www.webopedia.com/TERM/P/run.htmlhttp://www.webopedia.com/TERM/P/convert.htmlhttp://www.webopedia.com/TERM/P/interpreter.htmlhttp://www.webopedia.com/TERM/P/data.htmlhttp://www.webopedia.com/TERM/P/object_oriented.htmlhttp://www.webopedia.com/TERM/P/feature.htmlhttp://www.webopedia.com/TERM/P/run.html8/14/2019 Important Basic Concept in Object Oriented
6/25
can be expensive and even life threatening (in air-traffic control,
for example). Object-Oriented Programming offers a new and
powerful way to cope with this complexity. Its goal is clearer,
more reliable, more easily maintained programs.
Languages and Development Platforms
Of the Object-Oriented Programming languages, C++ is by far
the most widely used. (Java, a recent addition to the field of OO
languages, lacks certain features, such as pointers, that make it
less powerful and versatile than C++.)
In past years the standards for C++ have been in a state of
evolution. This meant that each compiler vendor handled certain
details differently. However, in November 1997, the ANSI/ISO
C++ standards committee approved the final draft of what is now
known as Standard C++. (ANSI stands for American National
Standards Institute, and ISO stands for International Standards
Institute.) Standard C++ adds many new features to the
language,such as the Standard Template Library (STL). In this
book we llow Standard C++ (except for a few places which well
note as we go
The most popular development environments for C++ are
manufactured by Microsoft and Borland and run on the various
flavors of Microsoft Windows. In this book weve attempted in
ensure that all example programs run on the current versions of
both Borland and Microsoft compilers. (See Appendixes C and D
for more on these compilers.)
Object-Oriented Design
Students are frequently mystified by the process of breaking a
programming project into appropriate classes. For this reason
weve added a chapter on object-oriented design. This chapter is
placed near the end of the book, but we encourage students to
8/14/2019 Important Basic Concept in Object Oriented
7/25
skim it earlier to get the flavor of OOD. Of course, small
programs dont require such a formal design approach, but its
helpful to know whats involved even when designing programs
in your head. C++ is not the same as C.
Some institutions want their students to learn C before learning
C++. In our view this is a mistake. C and C++ are entirely
separate languages. Its true that their syntax is similar, and C is
actually a subset of C++. But the similarity is largely a historical
accident. In fact, the basic approach in a C++ program is
radically different from that in a C program.
C++ has overtaken C as the preferred language for serious
software development. Thus we dont believe it is necessary or
advantageous to teach C before teaching C++. Students who
dont know C are saved the time and trouble of learning C and
then learning C++, an inefficient approach. Students who
already know C may be able to skim parts of some chapters, but
they will find that a remarkable percentage of the material is
new.
Optimize Organization for OOP
We could have begun the book by teaching the procedural
concepts common to C and C++, and moved on to the new OOP
concepts once the procedural approach had been digested. That
seemed counterproductive, however, because one of our goals is
to begin true Object-Oriented Programming as quickly as
possible. Accordingly, we provide a minimum of procedural
groundwork before getting to objects in Chapter 7. Even the
initial chapters are heavily steeped in C++, as opposed to C,
usage.
We introduce some concepts earlier than is traditional in books
on C. For example, structures are a key feature for
understanding C++ because classes are syntactically an
8/14/2019 Important Basic Concept in Object Oriented
8/25
extension of structures. For this reason, we introduce structures
in Chapter 5 so that they will be familiar when we discuss
classes.
Some concepts, such as pointers, are introduced later than intraditional C books. Its not necessary to understand pointers to
follow the essentials of OOP, and pointers are usually a
stumbling block for C and C++ students. Therefore, we defer a
discussion of pointers until the main concepts of OOP have been
thoroughly digested.
New Concepts
OOP involves concepts that are new to programmers of
traditional languages such as Pascal, Basic, and C. These ideas,
such as classes, inheritance, and polymorphism, lie at the heart
of Object-Oriented Programming. But its easy to lose sight of
these concepts when discussing the specifics of an object-
oriented language. Many books overwhelm the reader with the
details of language features, while ignoring the reason these
features exist. This book attempts to keep an eye on the big
picture and relate the details to the larger concepts.
Basic concepts of OOPS
Basic concepts of OOPS and Structure of C++ program
In this tutorial you will learn about Objects, Classes,
Inheritance, Data Abstraction, Data Encapsulation,
Polymorphism, Overloading, Reusability.
Before starting to learn C++ it is essential that one must have a
basic knowledge of the concepts of Object oriented
8/14/2019 Important Basic Concept in Object Oriented
9/25
programming. Some of the important object oriented features are
namely:
Objects Classes
Friend function
Static function
Inheritance
Data Abstraction
Data Encapsulation
Polymorphism
Overloading
Reusability
In order to understand the basic concepts in C++, the
programmer must have a command of the basic terminology in
object-oriented programming. Below is a brief outline of the
concepts of Object-oriented programming languages:
Objects:
Object is the basic unit of object-oriented programming. Objects
are identified by its unique name. An object represents a
particular instance of a class. There can be more than one
instance of an object. Each instance of an object can hold its
own relevant data.
8/14/2019 Important Basic Concept in Object Oriented
10/25
8/14/2019 Important Basic Concept in Object Oriented
11/25
form Properties of the Car class and the associated actions i.e.,
object functions like Start, Move, Stop form the Methods of Car
Class.
No memory is allocated when a class is created. Memory is
allocated only when an object is created, i.e., when an instance
of a class is created.
Friend Function
C++ Friend Functions
In this C++ tutorials, you will learn about friend functions, need
for friend function, how to define and use friend function and
few important points regarding friend function, explained with
example.
Need for Friend Function:
As discussed in the earlier sections on access specifiers, when a
data is declared as private inside a class, then it is not accessible
from outside the class. A function that is not a member or an
external class will not be able to access the private data. A
programmer may have a situation where he or she would need to
access private data from non-member functions and external
classes. For handling such cases, the concept of Friend
functions is a useful tool.
8/14/2019 Important Basic Concept in Object Oriented
12/25
What is a Friend Function?
A friend function is used for accessing the non-public members
of a class. A class can allow non-member functions and other
classes to access its own private data, by making them friends.Thus, a friend function is an ordinary function or a member of
another class.
How to define and use Friend Function :
The friend function is written as any other normal function,
except the function declaration of these functions is precededwith the keyword friend. The friend function must have the class
to which it is declared as friend passed to it in argument.
Features of friend function
The keyword friend is placed only in thefunction declaration of the friendfunction and not in the functiondefinition.. It is possible to declare a function asfriend in any number of classes.. When a class is declared as a friend, thefriend class has access to the private
data of the class that made this a friend.. A friend function, even though it is nota member function, would have the rightsto access the private members of the
8/14/2019 Important Basic Concept in Object Oriented
13/25
class.. It is possible to declare the friendfunction as either private or public.
. The function can be invoked without theuse of an object. The friend function hasits argument as objects, seen in example
below.
Static Functions
Static Functions
Static member functions have a class scope and they do not have
access to the 'this' pointer of the class. When a member is
declared as static, a static member of class, it has only one data
for the entire class even though there are many objects created
for the class. The main usage of static function is when the
programmer wants to have a function which is accessible even
when the class is not instantiated.
Accessing Static Function:
A normal member function is accessed using the object and an
operator called the dot member access operator. The functions
declared static or static functions are accessed using only the
class name and the scope resolution operator, unlike in normal
member functions where these are not used.
Defining Static Function:
Static function is defined by using the keyword static before the
member function that is to be declared as static function.
8/14/2019 Important Basic Concept in Object Oriented
14/25
General syntax:
static return_data_type fucntionname()//Static function defined with keyword static{statement1;
//Statements for execution inside static functionstatement2;....................
}
For example if a function exforsysreturning nothing is to be declared asstaic function it is done as follows:
static void exforsys(){........;.......;}
Features of static member functions:
A static member function can only accessstatic member data, static member functions
and data and functions outside the class. The
8/14/2019 Important Basic Concept in Object Oriented
15/25
programmer must take note not to use staticmember function in the same manner as non- static member function, as non-static
member function can access all of the aboveincluding the static data member.
.
A non-static member function can bedeclared as virtual but care must be takennot to declare a static member function as
virtual..
The programmer must first understand theconcept of static data while learning thecontext of static functions. It is possible todeclare a data member of a class as static
irrespective of it being a public or a privatetype in class definition. If a data is declared
as static, then the static data is created andinitialized only once. Non-static data
members are created again and again. Foreach separate object of the class, the static
data is created and initialized only once. Asin the concept of static data, all objects of
the class in static functions share thevariables. This applies to all objects of theclass..
A non-static member function can be calledonly after instantiating the class as an
object. This is not the case with staticmember functions. A static member functioncan be called, even when a class is not
8/14/2019 Important Basic Concept in Object Oriented
16/25
instantiated..
A static member function cannot have
access to the 'this' pointer of the class.
Inheritance:
Inheritance is the process of forming a new class from an
existing class orbase class. The base class is also known
as parent class or super class , The new class that is
formed is calledderived class. Derived class is also known
as a child class orsub class.Inheritance helps in reducingthe overall code size of the program, which is an important
concept in object-oriented programming
Inheritance is the process by which new classes calledderived
classes are created from existing classes calledbase classes. The
derived classes have all the features of the base class and the
programmer can choose to add new features specific to the newly
created derived class.
For example, a programmer can create a base class named
fruit and define derived classes asmango, orange,banana, etc. Each of these derived classes, (mango,orange,banana, etc.) has all the features of the base class(fruit) with additional attributes or features specific to thesenewly created derived classes.Mangowould have its own defined
features, orange would have its own defined features,bananawould have its own defined features, etc.
8/14/2019 Important Basic Concept in Object Oriented
17/25
This concept of Inheritance leads to the concept of
polymorphism.
Features or Advantages of Inheritance:
Reusability:
Inheritance helps the code to be reused in many situations. The
base class is defined and once it is compiled, it need not be
reworked. Using the concept of inheritance, the programmer can
create as many derived classes from the base class as neededwhile adding specific features to each derived class as needed.
Saves Time and Effort:
The above concept of reusability achieved by inheritance saves
the programmer time and effort. Since the main code written can
be reused in various situations as needed.
Increases Program Structure which results in greater reliability.
Polymorphism (to be discussed in detail in later sections)
8/14/2019 Important Basic Concept in Object Oriented
18/25
General Format for implementing the conceptof Inheritance:
class derived_classname: access specifier
baseclassname
For example, if the base class is exforsys and the derived class is
sample it is specified as:
class sample: public exforsys
The above makes sample have access to both public and
protected variables of base class exforsys . Reminder about
public, private and protected access specifiers:
If a member or variables defined in aclass is private, then they are accessible
by members of the same class only andcannot be accessed from outside the class.
. Public members and variables are
accessible from outside the class.. Protected access specifier is a stage
between private and public. If a memberfunctions or variables defined in a classare protected, then they cannot be
8/14/2019 Important Basic Concept in Object Oriented
19/25
accessed from outside the class but can beaccessed from the derived class.
Inheritance Example:
class exforsys{
public:exforsys(void) { x=0; }void f(int n1){x= n1*5;}
void output(void) { cout
8/14/2019 Important Basic Concept in Object Oriented
20/25
exforsys::output();cout
8/14/2019 Important Basic Concept in Object Oriented
21/25
the derived class sample has new member function f1( ) added in
it. The line:
sample s;
creates a derived class object named as s. When this is created,
space is allocated for the data members inherited from the base
class exforsys and space is additionally allocated for the data
members defined in the derived class sample.
The base class constructorexforsys is used to initialize the base class data members
and the derived class constructor sample is used to initialize the data members
defined in derived class.
The access specifier specified in the line:
class sample: public exforsys
Public indicates that the public data members which are inherited from the base class
by the derived class sample remains public in the derived class.
Data Abstraction:
Data Abstraction increases the power of programming language
by creating user defined data types. Data Abstraction also
8/14/2019 Important Basic Concept in Object Oriented
22/25
represents the needed information in the program without
presenting the details.
Data Encapsulation:
Data Encapsulation combines data and functions into a single
unit called Class. When using Data Encapsulation, data is not
accessed directly; it is only accessible through the functions
present inside the class. Data Encapsulation enables the
important concept of data hiding possible.
Polymorphism:
Polymorphism allows routines to use variables of different types
at different times. An operator or function can be given different
meanings or functions. Polymorphism refers to a single function
or multi-functioning operator performing in different ways.
Overloading:
Overloading is one type of Polymorphism. It allows an object to
have different meanings, depending on its context. When an
exiting operator or function begins to operate on new data type,
or class, it is understood to be overloaded.
Reusability:
This term refers to the ability for multiple programmers to use
the same written and debugged existing class of data. This is a
time saving device and adds code efficiency to the language.
8/14/2019 Important Basic Concept in Object Oriented
23/25
Additionally, the programmer can incorporate new features to
the existing class, further developing the application and
allowing users to achieve increased performance. This time
saving feature optimizes code, helps in gaining secured
applications and facilitates easier maintenance on theapplication.
The implementation of each of the above object-oriented
programming features for C++ will be highlighted in later
sections.
A sample program to understand the basic structure of C++
//program to read employee details and tooutput the data#include Preprocessor directive
classemployee Class Declaration{
private:char empname[50];int empno;
public:
void getvalue(){
coutempname;coutempno;
8/14/2019 Important Basic Concept in Object Oriented
24/25
}
void displayvalue(){
cout
8/14/2019 Important Basic Concept in Object Oriented
25/25
included placed between < >..
The next structure present in the program
is the class definition. This starts with thekeyword class followed by class name
employee. Within the class are data andfunctions. The data defined in the class are generally private and functions are public.These explanations we will be detailed in
later sections. The class declaration endswith a semicolon.
.
main() function is present in all C++programs..
An object e1 is created in employee class.Using this e1 the functions present in the
employee class are accessed and there bydata are accessed.
. The input namely ename and eno is got
using the input statement namely cin and thevalues are outputted using the output
statement namely cout.