OOPS
OOPS
Classes
We just mentioned that objects contain data, and code to manipulate that
data. The entire set of data and code of an object can be made a user-defined data
type with the help of a class. In fact, objects are variables of the type class. Once a
class has been defined, we can create any number of objects belonging to I.hat
class. Each object is associated with the data of type class with which they are
created. A class is thus a collection of .objects of similar type. For example,
mango, apple and orange are members of the class fruit. Classes are user-defined
data types and behave like the built-in types of a programming language.
Inheritance
Inheritance is the process by which objects of one class acquire the properties of
objects of another class. It supports the concept of hierarchical classification. For
example, the bird 'robin' is a part of the class 'flying bird' which is again a part of
the class 'bird'. In OOP, the concept of inheritance provides the idea of reusability.
This means that we can add additional features to an existing class without
modifying it. This is possible by deriving a new class from the existing one. The
new class will have the combined features of both the classes.
Polymorphism
Polymorphism is another important OOP concept. Polymorphism a Greek term,
means the ability to take more than one form. An operation may exhibit different
behaviors in different instances. The behavior depends upon the types of data used
in the operation. For example, consider the operation of addition. For two numbers,
the operation will generate a sum. If the operands are strings, then the operation
would produce a third string by concatenation. The process of making an operator
to exhibit different behaviors in different instances is known as operator
overloading. . Using a single function name to perform different types or tasks is
known as function overloading.
Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in
response to the call. Dynamic binding (also known as late binding) means that the
code associated with a given procedure call is not known until the time of the call
at run-time. It is associated with polymorphism and inheritance.
Message Passing
An object-Oriented program consists of a set of objects that communicate with
each other. The process of programming in an object-oriented language, therefore,
involves the following basic steps:
1. Creating classes that define objects and their behavior,
2. Creating objects from class definitions, and
3. Establishing communication among objects.
Objects communicate with one another by sending and receiving information much
the same way as people pass messages to one another.
BENEFITS OF OOPS
OOP offers several benefits to both the program designer and the user.
Objectorientation contributes to the solution of many problems associated with the
development and quality of software products. The new technology promises
greater programmer productivity, better quality of software and lesser maintenance
cost. The principal advantages are:
• Through inheritance, we can eliminate redundant code and 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 c.ode from scratch. This
leads to saving of development time and higher productivity.
• The principle of data hiding helps the programmer to build secure programs that
cannot be invaded by code in other parts of the program.
• It is possible to have multiple instances of an object to co-exist without any
interference.
• It is possible to map objects 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 details of a model
in implementable form.
• Object-oriented systems can be easily upgraded from small to large systems.
• A message passing techniques for communication between objects makes the
interface descriptions with external systems much simpler.
• Software complexity can be easily managed.
APPLICATIONS OF OOPS
Applications of OOP are beginning to gain importance in many areas. The most
popular application of object oriented programming, up to now, has been in the
area of user interface design such as windows.
Real-business systems are often much more complex and contain many more
objects with complicated attributes and methods. OOP is useful in these types of
applications because it can simplify a complex problem. The promising areas for
application of OOP include:
• Real-time systems
• Simulation and modeling
• Object-oriented databases
• Hypertext, hypermedia and expert text
• Al and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
OVERVIEW OF C++
C++ is an object-oriented programming language. It was developed by
Bjarne Stroustrup at AT&T Bell Laboratories. C++ is a superset of C. Most of hat
we already know about C applies to C++ also. Therefore almost all C programs are
also C++ programs.
The most important facilities that C++ adds on to C are classes, inheritance,
function overloading, and operator overloading. Those 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 t.he
spirit and efficiency of C.
STRUCTURE OF C++
Declaring Objects:
When a class is defined, only the specification for the object is defined. no memory
or storage is allocated. To use the data and access functions defined in the class,
you need to create objects.
Syntax:
ClassName ObjectName;
main() {
student a;
a.readdata();
a.dispay();
return 0;
}
To define a member function outside the class definition we have to use the scope
resolution :: operator along with class name and function name. And function
declaration should be there inside the class.
CONSTRUCTORS
Constructer is a special type of member function that initializes an object
automatically when it is created. The compiler calls the constructor whenever an
object is created. Constructors have the same name as the class. Constructor can be
defined inside or outside the class definition. A constructer is defined without a
return type. By default constructors are defined in the public section of class, so
that constructor is called automatically when an object is created outside the class.
A constructer is defined without a return type
Default constructors
A default constructor is the constructor which doesn‟t take any argument. It has no
parameter. Above program is an example of default constructor.
Note: Even if we do not define any constructor explicitly, the compiler will
automatically provide a default constructor implicitly. The default value of all data
members will be initialized to 0 Example:
Parameterized constructors
It is possible to pass arguments to constructors. Using this constructor you can
provide different values to data members of different objects. To create a
parameterized constructor, simply add parameters to it, similar to any other
functions. When you define the constructor‟s body, use the parameters to initialize
the object.
DESTRUCTORS
Destructor is another special member function which destroys the object as soon as
the scope of the object ends. The destructor is automatically called by the compiler
when the object goes out of the scope. Syntax of destructor is same that of
constructor, the class name is used as the name of destructor, with a tilde „~‟ sign
as prefix to it.