Professional Documents
Culture Documents
Date OF Submission: -
Paper on Prominence of OOP
perform many tasks like talk, walk, pay, eat, run sleep etc etc, but as per the
need of the program we abstract a class called man. We only include the
attributes that have some relation with our program. The chosen attributes and
behavior (tasks) must have connection with about what we are making the
program. Like in of salary of an employee man we may call behavior/method
like give salary etc or in case of student man we may call method like admit,
promote etc. So with proper abstraction we can get the class and object we
want, resulting the desired result we want.
Fig 1.1
Moreover, each subclass inherits state (in the form of variable declarations)
from the superclass. You are not limited to just one layer of inheritance. The
inheritance tree, or class hierarchy, can be as deep as needed. Methods and
variables are inherited down through the levels. In general, the farther down in
the hierarchy a class appears, the more specialized its behavior. A think I would
like to add here is “One of the most compelling features about C++ is
codereuse. But to be revolutionary, you need to be able to do a lot more than
copy code and change it”.
Fig :1.2
Here the draw function has the same name but will perform different
operations per the object it is called for. Like for circle it will draw a circle for
square a square and for triangle a triangle, likewise for erase. The funda behind
this is the variation in message (argument) passed to that method and its
return type difference. So this proves that, polymorphism is difficult to explain,
but easy to demonstrate. Polymorphism allows improved code organization and
readability as well as the creation of extensible programs that can be “grown”
not only during the original creation of the project, but also when new features
are desired. (PS: Overloading is dealt in detail on the topic Function
Overloading and Inline Functions Pg # 5.)
a. Why?
It is used to keep up the size and clarity aspect of the normal function
as well as not break the flow of the program and not cost much time.
On in explaind version: It's definitely fewer keystrokes to inline a
function. However, another good reason to inline is that you can
sometimes speed up your program by inlining the right function.
Instead of calling the function every time it is invoked, the compiler
will replace the function call with a copy of the function body. If it's a
small function which gets called a lot, this can sometimes speed things
up.
return a + b;
}
Ex2:
inline add (int x, int y) {return(x+y);}
The above function where called will be replaced by the body of the
function.
Overloading Functions: is a concept used in OOP to use the same function for
may objectives/job. Plainly, it is method with the same name has two or more
definitions as per the arguments passed and data type returned. Even
operators like +,-,++ etc can be overloaded to do desired operation on self
created objects(data types).
A problem arises when mapping the concept of nuance in human language
onto a programming language. Often, the same word expresses a number of
different meanings, depending on context. That is, a single word has multiple
meanings – it’s overloaded. This is very useful, especially when it comes to
trivial differences. You say “wash the shirt, wash the car.” It would be silly to
be forced to say, “shirt_wash the shirt, car wash the car” just so the hearer
doesn’t have to make any distinction about the action performed. Most human
languages are redundant, so even if you miss a few words, you can still
determine the meaning. We don’t need unique identifiers – we can deduce
meaning from context. Most programming languages, however, require that
you have a unique identifier for each function. If you have three different types
of data that you want to print: int, char, and float, you generally have to create
three different function names, for example, print_int( ), print_char( ), and
print_float( ). This loads extra work on you as you write the program, and on
readers as they try to understand it. In C++, another factor forces the
overloading of function names: the constructor. Because the constructor’s
name is predetermined by the name of the class, it would seem that there can
be only one constructor. But what if you want to create an object in more than
one way? For example, suppose you build a class that can initialize itself in a
standard way and also by reading information from a file. You need two
constructors, one that takes no arguments (the default constructor) and one
that takes a string as an argument, which is the name of the file to initialize the
object. Both are constructors, so they must have the same name: the name of
the class. Thus function overloading is essential to allow the same function
name – the constructor in this case – to be used with different argument types.
Although function overloading is a must for constructors, it’s a general
convenience and can be used with any function, not just class member
functions. In addition, function overloading means that if you have two libraries
that contain functions of the same name, they won’t conflict as long as the
argument lists are different.
Some times when these overloaded functions are called, they might cause
ambiguity errors. This is because the compiler may not be able to decide what
signature function should be called.
If the data is type cast properly, then these errors will be resolved easily.
Typically, function overloading is used wherever a different type of data is to be
dealt with. For example this can be used for a function which converts farenheit
to celsius and vice versa. One of the functions can deal with the integer data,
other can deal float for precision etc.,
When building bicycles, manufacturers take advantage of the fact that bicycles
share characteristics, building many bicycles from the same blueprint. It would
be very inefficient to produce a new blueprint for every individual bicycle
manufactured.
In object-oriented software, it's also possible to have many objects of the same
kind that share characteristics: rectangles, employee records, video clips, and
so on. Like the bicycle manufacturers, you can take advantage of the fact that
objects of the same kind are similar and you can create a blueprint for those
objects. A software blueprint for objects is called a class.
3. Each object has its own memory made up of other objects. Put
another way, you create a new kind of object by making a package
containing existing objects. Thus, you can build complexity in a program while
hiding it behind the simplicity of objects.
4. Every object has a type. Using the parlance, each object is an instance
of a class, where “class” is synonymous with “type.” The most important
distinguishing characteristic of a class is “what messages can you send to it?”
5. All objects of a particular type can receive the same messages. This is
actually a very loaded statement, as you will see later. Because an object of
type “circle” is also an object of type “shape,” a circle is guaranteed to receive
shape messages. This means you can write code that talks to shapes and
automatically handle anything that fits the description of a shape. This
substitutability is one of the most powerful concepts in OOP.
Classes VS Objects=
Many people get confused by the difference between class and object. The
difference is simple and conceptual. A class is a template for objects. A class
defines object properties including a valid range of values, and a default value.
A class also describes object behavior. An object is a member or an "instance"
of a class. An object has a state in which all of its properties have values that
you either explicitly defines or that are defined by default settings.
This subtle conceptual difference between classes and objects shows why there
is a tendency to want to use them interchangeably.
mainly used for data initiallization. We are able to define more than
one constructor in a class as per the need. The user defined
constructor with no arguments, blank body and no return type can be
compared with the default one.
The user defined constructors can be classified into two parts, they
are:
Destructors: is also a special kind of function that as the name implies is used
to destroy the object created by its counter part the constructor. Destructors
also have the same name as the constructors but it is preceded with a tilde ~.
For ex if a destructor is to be defined of class man then it should have name
~man.
Destruction of objects takes place when the object leaves its scope of definition
or is explicitly destroyed. The latter happens, when we dynamically allocate an
object and release it when it is no longer needed.
/* Program Coded to see function of Lion Class with use of construcor its types and
destructor
Coded by GESHAN MANANDHAR,03-00097-2, BIM 3rd Sem, Prime College
as at 12/12/04 for OOP(C++) */
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void sleeping()
{
cout<<"\nThe lion No "<<ln<<" is Sleeping."<<endl; }
void hunting()
{
cout<<"\nThe lion No "<<ln<<" is hunting. "<<endl; }
void running()
{
cout<<"\nThe lion No "<<ln<<" is running. "<<endl; }
lion() //A non parameterized constructor thar works like default one
{ cout<<"Calling Default constructor.Lion Created.\n";
cout<<"Give no to lion: ";
cin>>ln;
cout<<"Give weight of Lion: ";
cin>>wt;
cout<<"Give lion's Height: ";
cin>>h;
void disp()
{
cout<<"\nLion NO: "<<ln<<endl;
cout<<"Lion Weight: "<<wt<<endl;
cout<<"Lion Height: "<<h<<endl;
cout<<endl;
}
~lion() //a user defined destructor to see the effects.
{
cout<<"Lion No "<<ln<<" destroyed by destructor."<<endl;
}
};
void main()
{
clrscr();
lion l1,l2,l3;
lion l6(7,25.52,35.25);
l1.running();
l2.hunting();
l3.sleeping();
lion l5=l3;
cout<<"Info of lion3"<<endl;
l3.disp();
l6.disp();
}
getch();
}//main ends.
4. References, Bibliography:
Websites: