Object Oriented Programming
Object Oriented Programming
CH-1
Object oriented programming as 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. Example: C++, Smalltalk, Java.
Benefits of OOP:
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 code 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 La 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.
Message passing techniques for communication between objects makes the interface descriptions
with external systems much simpler.
Software complexity can be easily managed.
Applications of OOP:
Real-time systems.
Simulation and modeling.
Object-oriented databases.
Hypertext, hypermedia and expert text.
Artificial Intelligence systems and expert system.
Neural networks and parallel programming.
Decision support and office automation system.
CIM/CAM/CAD systems.
Basic concepts of OOP:
Object:
Objects are the basic runtime entities in an object-oriented system. They may represent a
person, a place, a bank account, a table of data or any item that the program has to handle.
Class:
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. Each object is associated with the data of type class with
which they are created.
The wrapping up of data and functions into a single unit (called class) is known as
encapsulation. The data is not accessible to the outside world, and only those functions which are
wrapped in the class can access it.
Abstraction, refers to the act of representing essential features without including the
background details or explanations. Classes use the concept of abstraction and are defined as a list of
abstract attributes such as size, weight and cost, and function to operate on these attributes.
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. In OOP, the concept of inheritance
provides the idea of reusability.
Polymorphism:
Polymorphism is another important OOP concept. It 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.
Dynamic binding:
Binding refers to the linking of a procedure call to the code the 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.
Message passing:
An object-oriented program consists of a set of objects that communicate with each other. Those objects
communicate with one another by sending and receiving information much the same way as people pass
messages to one another.
Q2. How are data and functions organized in an object oriented programming?
Ans:
In an object oriented programming data treats as a critical element in the program development and does
not allow it to flow freely around the system. It ties data more closely to the functions that operate on it,
and protects it from accidental modification from outside functions. OOP allows decomposition of a
problem into a number of entities called objects and then builds data and functions around these objects.
The organization of data and functions in object-oriented programs is shown in Fig. The data of an object
can be accessed only by the functions associated with that object. However, functions of one object can
access the functions of other objects.
Ans: class and objects are two different concepts but there is a relationship between them. Every object
belong to a class and every class contains one or more related objects. A class is static, so all the attributes
of class are fixed at the time of execution of a program. Objects are variables of the type class. Once a
class has been defined, we can create any number of objects belonging to that class. Each object is
associated with the data of type close with which they are created. A class is thus a collection of objects of
similar type.
For example, mango, apple and mange are members of the class fruit. Classes are user-defined data types
and behave like the built-in types of a programming language. If fruit has been defined as a class, then the
statement
fruit mango;
will create an object mango belonging to the class fruit.
The general format of the class and objects are:
class fruit
{
………..........
………………..
}; mango, orange, apple;
Q4. What is ADT?
Ans:
The meaning of ADT is Abstract Data Types. The data abstraction is the process of highlighting the essential,
inherent aspects of an entity while ignoring irrelevant details. The classes use the concept of data
abstraction, so they are known as Abstract Data Types (ADT).
Q5. Difference between OOP (Object Oriented Programming) & POP (Procedure Oriented Programming):
2. Large program divided into object. 2. Large program divided into function.
Q6. Difference between OOP (Object-Oriented Programming) & OBP (Object-Based Programming):
Objects are the basic run-time entities whereas classes are the user defined data types which
behaves like a built-in data types.
Objects are variables of type class but class helps to make an object as user defined data type.
An object has physical existence, but a class is a logical abstraction.
1. Data abstraction refers to the act of representing essential features without including the
background details. Whereas encapsulation means wrapping up of data and functions into a single
unit.
2. Data abstraction is used on class so that it can be treated like any other built-in data type. On the
other side encapsulation protects data from outside accessing.
The mechanism of deriving a new class from an old one is called inheritance, where polymorphism
means one thing with several distinct terms.
Inheritance provides the idea of reusability but polymorphism provides the ability to take more
than one form.
1. Dynamic Binding refers to the linking of a procedure call to be executed in response to the call,
but Massage passing involves specifying the name of the object, the name of the function and the
information to be sent.
2. Dynamic binding is used as a function call associated with a polymorphic reference depends on
the dynamic type of that reference, whereas massage passing is the way of communicating
between objects.
CH-3
In the statement,
char const*p;
p is declared as pointer to a constant. It can point to any variable of correct type, but the contents of what
it points to cannot be changed.
Advantages:
The inline function eliminates the cost of calls of small function.
It makes the program run faster because the overhead of a function call and return is eliminated.
Disadvantages:
The speed benefits of inline function diminish as the function grows in size.
It makes the program to take up more memory because the statements that define the inline
function are reproduced at each point where the function is called. So, a trade-off becomes
necessary.
// declaration
int add(int a, int b): // prototype 1
int add(int a, int b, int c) ; // prototype 2
double add(int p, double q): // prototype 3
// Function calls
cout add(5, 10); // uses prototype 1
cout << add(5. 10. 15) ; // uses prototype 2
cout << 5dd(15, 10.0); // uses prototype 3
Ans:
Ans:
If an exact match is not found, the compiler uses the integral promotions to the actual arguments,
such as:
char to int
float to double
to find a match.
When either of them fails, the compiler tries to use the built-in conversions the implicit assignment
conversions to the actual arguments and then uses the function whose match is unique. If the
conversion is possible to have multiple matches, then the compiler will generate on error message.
Suppose we use the following two functions:
long square (long n)
double Square(double x)
A function call such as :
square(20)
will cause an error because int argument can be converted to either long or double, thereby creating
an ambiguous situation as to which version of square( ) should be used. If all of the steps fail, then the
compiler will try the user-defined conversions in combination with integral promotions and built-in
conversions to find a unique match.
CH-5
Q3. Describe the mechanism of accessing data members and member functions in the following cases:
(a) Inside the main program.
(b) Inside a member function of the same class.
Ans:
(a) Using object and dot membership operator. (b) Just like accessing a local variable of a
function.
Example: Example:
Several different clauses can use the same function name. The 'membership label' will resolve their
scope.
Member functions can access the private data of the class. A non-member function cannot do so.
A member function can call another member function directly, without using the dot operator.
There are some restrictions in constructing local classes. They cannot have static data members and
member functions must be defined inside the local classes. Enclosing function cannot access the private
members of a local class. However, we can achieve this by declaring the enclosing function as a friend.
Q8. What is forward declaration?
Ans:
A forward declaration is a process which allows us to tell the compiler about the existence of a class
before actually defining the class object.
In this case it allows us to tell the compiler about the existence of an object before we define the object’s
class. This way, when the compiler encounters an object, it’ll understand that we’re making a class, and
can check to ensure we’re making the class correctly, even if it doesn’t yet know how or where the object
is defined.
To write a forward declaration for a object, we use a declaration statement which is known as forward
declaration and it is a statement, so it ends with a semicolon.
Merits:
To do with operator overloading.
The creation of certain types of I/O function.
To have access to the private members of two or more different classes.
Demerits/Drawbacks:
A friend function is not inherited i.e. when a base class includes a friend function, that friend
function is not a friend of a derived class.
Although a friend function has knowledge of the private elements of the class for which it is a
friend, it can only access then through an object of the class.
Maximum size of the memory will occupied by objects according to the size of friend members.
It breaks the data hiding concept.
We know that a non-member function cannot have an access to the private data of a class. However,
there could be a situation where we would like two classes to share a particular function. For example
consider a case where two classes manager and scientist, have been defined. We would like to use a
function income_tax( ) to operate on the objects of both these classes. In such situations, C++ allows the
common function to be made friendly with both the classes. Thereby allowing the function to have access
to the private data of these classes. Such a function need not be a member of any of these classes.
Example:
class manager
{
…………..
public:
………………….
friend income_tax( );
};
class scientist
{
…………..
public:
………………….
friend income_tax( );
};
CH-6
Ans:
time T2 (T1); --> explicitly called of copy constructor
time T2 = T1; --> implicitly called of copy constructor.
Q4. All about constructor………………………………
Ans:
What is constructor?
A constructor is a 'special' member function whose task is to initialize the objects of its class. It is special
because its name is the same as the class name. The constructor is invoked whenever an object of its
associated class is created. It is called constructor because it constructs the values of data members of the
class.
Advantages:
Constructor is used to construct the values of data members of the class.
They are invoked automatically when the objects are created.
They can have default arguments.
They make 'implicit calls’ to the operators new and delete when memory allocation is required.
When a class contains a constructor, it is guaranteed that an object created by the class will be
initialized automatically.
Disadvantages:
They cannot be inherited, though a derived class can call the base class constructor.
They do not have return types, not even void and therefore, and they cannot return values.
Constructors cannot be virtual.
We cannot defer to their addresses.
An object with a constructor cannot be used as a member of a union.
When a constructor is declared for a class, initialization of the class objects becomes mandatory.
What is destructor?
A destructor, as the name implies, is used to destroy the objects that have been created by a constructor.
Like a constructor. The destructor is a member function whose name is the same as the class name but is
preceded by a tilde (~). Example:
class alpha
{
……………….
public:
alpha()
{
……………………
…………………..
}
~alpha{ }
{
…………….
…………….
};
Constructor Destructor
1. A constructor enables an object to initialize 1. A destructor destroyed the objects that
itself when it is created. have been created by a constructor.
2. Its name is the same as the class name. 2. Its name is the same as the class name
but is preceded by a tilde (~) sign.
3. It allocates memory space. 3. It releases memory space.
4. It will be invoked whenever an object of its 4. It will be invoked implicitly by the
associated class is created. compiler upon exit from the program to
clean up the storage.
5. Constructor can have a default argument. 5. Destructor never takes any arguments, so
it does not have any default arguments.
Note to be point:
It is not possible to take the address of either a constructor or a destructor.
Local objects are destroys when they go out of scope.
Global objects are destroyed when the program ends.
Unlike constructor functions, destructor functions cannot have parameters. The reason for this is
simple enough to understand: “There exists no mechanism by which to pass arguments to an
object that is being destroyed”.
CH-7
Q5. A friend function cannot be used to overload the assignment operator =. Explain why?
Ans:
A friend function is a non-member function of the class to which it has been defined as friend. Therefore it
just uses the functionality (functions and data) of the class. So it does not consist the implementation for that
class. That’s why it cannot be used to overload the assignment operator.
CH-8
Ans:
(a) Firstly A will inherit B and then C. Private member of B and C can not be inherited in A. Protected
member of B and C become protected in A. public member of B and C become public in A.
(b) Firstly A will inherit C and then B. Private member of C and B can not be inherited in A. Protected
member of C and B and C become protected in A. public member of C and B become public in A.
Q8. When do we make a virtual function “pure”? What are the implications of making a function a pure
virtual function?
Ans:
When a function is defined as empty, then we make a virtual function “pure”, so that this function act like a
“do nothing” function.
The implications of making a function a pure virtual function is to achieve run time polymorphism.