Chapter-1-Object Oriented Programming
Chapter-1-Object Oriented Programming
com)
11
Concept of Object
Chapter-1
Oriented
Programming
History & features: Its need & requirement, procedure oriented programming
versus object oriented programming, basic concepts object oriented programming,
object oriented languages.
Beginning with C++: Concepts & structure of C++ programming, concepts of
structure.
Q1.
Write short note on software evolution
Ans.C++ is an object-oriented programming language. It was developed by Bjarne
Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early
1980s. Stroustrup, an admirer of Simula67 and a strong supporter of C, wanted to
combine the best of both the language and create a more powerful language that could
support object-oriented programming features and still retain the power and elegance of
C. The result was C++.
Therefore, C++ is an extension of C with a major addition of the class construct
feature of simula67. Since the class was a major addition to the original C language,
Stroustrup initially called the new language C with classes. However, later in 1983, the
name was changed to C++.
The idea of C++ comes from the C increment operator ++, thereby suggesting that
C++ is an augmented (incremented) version of C.
During the early 1990s the language underwent a number of improvements and
changes. In November 1997, the ANSI/ISO standards committee standardized these
changes and added several new features to the language specifications.
C++ is a superset of C. Most of what we already know about C applies to C++ also.
Therefore, most all C programs are also C++ programs. However, there are a few minor
differences that will prevent a C program to run under compiler.
The most important facilities that C++ adds on to C are classes, inheritance,
function overloading, and operator overloading. These features enable creating of
abstract data types, inherit properties from existing data types and support
polymorphism, thereby making C++ a truly object-oriented language.
The object-oriented features in C++ allow programmers to build large programs
with clarity, extensibility and ease of maintenance, incorporating the spirit and efficiency
of C. The addition of new features has transformed C from a language that currently
facilitates top-down, structured design, to one that provides bottom-up, object-oriented
design.
Q2.Define OOP(object oriented programming)
Ans. :Object Oriented programming is 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. Thus an object is
1
main()
main()
Function1
Function1
Function2
Function2
Function3
Function3
While we concentrate on development of function, very little attention is given to the data
that are being used by various functions. Data is after all the reason for program
existence. For e.g. the important part of the program is not the function that displays the
data, or a function that checks the correct input. But yet the data is given secondary
status in procedure oriented.
Characteristics of POP (Procedure Oriented Programming)
Object : Student
Data :
Name
Data of Birth
Marks
Functions
Total
Average
Display
Total
Average
Display
Classes
In OOPS, The entire set of data & codes of an object can be made a user define
data types. With the help of classes. A class is collection of objects of similar type. The
objects are the members of classes almost all computer languages have built in data
types like integer, float etc. In similar way you can define many objects of same class
and the entire code of an object can be made a user define data type with the help of a
class. A class is thus a collection of objects of similar type. For e.g staff and students are
the members of the college.
Data Abstraction and Encapsulation
The wrapping up of data and function into a single unit is called as
Encapsulation. The data is not accessible to the outside world and only those
functions, which are members of class, can access the data. These functions provide
interface between objects and programs. This insulation of data from direct access by
program is called as data hiding. Abstraction refers to the representation of essential
features without including background details or explanations. Classes use the concept
of abstraction and are defined as a list of abstract attributes such as size, cost, etc.
Functions to operate on these attributes.
Inheritance
6
BIRD
BIRD
FLYING
FLYINGBIRD
BIRD
NON-FLYING
NON-FLYINGBIRD
BIRD
SPARROW
SPARROW
Polymorphism
Polymorphism means the ability to take more than one form. For e.g. an operation
may exhibit different behavior in different instants. The behavior depends upon the types
of data used in the operation.
For e.g. consider operation addition for two numbers, the operation will generate sum. If
operands are strings the operation would perform a third string by concatenation. To
handle function overloading, a single function name can be used to handle different
numbers and different types of arguments. This is something similar to particular word
having several different meanings depending upon the context as shown in the fig.
SHAPE
SHAPE
CIRCLE
CIRCLE
DRAW(
DRAW() )
RECTANGLE
RECTANGLE
DRAW(
DRAW() )
POLYGON
POLYGON
DRAW(
DRAW() )
Dynamic binding
Binding refers to a linking procedure call to the code, which is to be executed in
response to the call. Dynamic binding means that the code associated with a given
procedure call is not known until the time of call at the run time.
Consider a procedure draw shown in a fig by inheritance every object will have
this procedure. This draw procedure will be redefined in each class that defines the
object. At the run time, the code matching the object under current reference will be
called.
7
Q14. List some Object Oriented Language and Object based Programming
Languages
Ans. Object oriented Languages
C++
Small talk
Object Pascal
Java
Object Based Programming languages
ADA
Visual Basic
Cardelli 85
Modula-3
10
};
};
struct Y
{
// public by default
int f()
{
return a = 5;
};
private:
int a;
};
Q22.Compare Structure and Class
Ans. In C++, a structure is a class defined with the struct keyword. Its members and
base classes are public by default. A class defined with the class keyword has private
members and base classes by default. This is the only difference between structs and
classes in C++.
Basically in C++ structure and class are same but with some minor differences listed
below
1. In classes all the members by default are private but in structure members are
public by default.
2. There is no term like constructor and destructor for structs, but for class compiler
creates default if you don't provide.
3. Sizeof empty structure is 0 Bytes were as Sizeof empty class is 1 Byte
4. Inheritance between classes is also private by default, and inheritance between
structs is public by default.
5. Structures didnot support data hiding, but classes can.
6. Private functions totally hidden in classes.
7. classes support polymorphism, whereas structures donot
Q.23.Compare C and C++ Structure
Ans.
The definition of the structure in C is limited to within the module and cannot
be initialized outside its scope. Where as in C++ you can initialize the objects
anywhere within the boundaries of the project.
The one more difference is in C++ structure we can have methods(procedures)
but C can note have methods in structure
15
By default C structure are Public while C++ structure are private. In C you can
declare structure as struct A a; But not A a(which is possible in C++);
The object oriented program deals with objects created on the basis of classes
example: java c++, but procedure oriented program deals with functions example:c
, pascal
In Procedural programming Programmer combines related sequences of
statements into one single place called procedure. A procedure call is used to
invoke the procedure. After the sequence is processed flow of control proceeds
right after the position where the call was made. But the approach in oops is that
classes and objects are used to model real world entity taking help of methods
which performs the functions.
Procedure oriented programming - Gives importance to algorithm rather than
data. It follows Top-down structures. Object oriented programming - Gives
importance to data rather than algorithm. It follows Bottom-up structures.Every
thing is viewed as an object.It includes features like data hiding polymorphism
encapsulation and message passing.
OOP supports classes objects and also
1. Incapsulation - Wrap the data in single unit
2. Inheritance - Aquire the properities of parent class
3. Polymorphism - Same function can be used with different signatures
26