There are eight questions in this Assignment. Answer all the questions. You may use illustrations and diagrams to enhance your explanations. Answer to each part of the question should be confined to about 300 words.

Q.1Explain four major advantages of Object Oriented Programming with the help of an example each.

Ans.
Advantages of Object Oriented Programming :

I - Inharitance.

Inheritance is the process by which new classes called derived classes are created from existing classes called base 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 as mango, 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 these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.

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 needed while 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.

General Format for implementing the concept of Inheritance:
class derived_classname: access specifier baseclassname

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.

Below is a simple example of the above concept of polymorphism:
6 + 10
The above refers to integer addition.
The same + operator can be used with different meanings with strings:
"Exforsys" + "Training"
The same + operator can also be used for floating point addition:
7.15 + 3.78

Features and Advantages of the concept of Polymorphism:Applications are Easily Extendable:

Once an application is written using the concept of polymorphism, it can easily be extended, providing new objects that conform to the original interface. It is unnecessary to recompile original programs by adding new types. Only re-linking is necessary to exhibit the new changes along with the old application. This is the greatest achievement of C++ object-oriented programming. In programming language, there has always been a need for adding and customizing. By utilizing the concept of polymorphism, time and work effort is reduced in addition to making future maintenance easier.

Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions present inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The concept of restricted access led programmers to write specialized functions or methods for performing the operations on hidden members of the class. Attention must be paid to ensure that the class is designed properly.

Neither too much access nor too much control must be placed on the operations in order to make the class user friendly. Hiding the implementation details and providing restrictive access leads to the concept of abstract data type. Encapsulation leads to the concept of data hiding, but the concept of encapsulation must not be restricted to information hiding. Encapsulation clearly represents the ability to bundle related data and functionality within a single, autonomous entity called a class.

Features and Advantages of the concept of Encapsulation:
* Makes Maintenance of Application Easier:

Complex and critical applications are difficult to maintain. The cost associated with maintaining the application is higher than that of developing the application properly. To resolve this maintenance difficulty, the object-oriented programming language C++ created the concept of encapsulation which bundles data and related functions together as a unit called class. Thus, making maintenance much easier on the class level.

* Improves the Understandability of the Application

* Enhanced Security:

There are numerous reasons for the enhancement of security using the concept of Encapsulation in C++. The access specifier acts as the key strength behind the concept of security and provides access to members of class as needed by users. This prevents unauthorized access. If an application needs to be extended or customized in later stages of development, the task of adding new functions becomes easier without breaking existing code or applications, there by giving an additional security to existing application.

In order to benefit from the powerful feature of encapsulation in object-oriented programming language C++, the programmer must use encapsulation properly. To maximize the benefits of encapsulation, the user must minimize the implementation details in external interfaces as needed.

Example :
For example, a class called LoggerFile might bundle data members such as the log file's path; a buffer of data waiting to be written to that file; and member functions that open, read, write, and close that file, as in the following code listing:

Abstraction is one of the most powerful and vital features provided by object-oriented C++ programming language. Modularity is very important in any programming language, it provides flexibility to users for using the programming language. This aspect is well achieved with high performance by the concept of abstraction in C++. In object-oriented programming language the programmer can abstract both data and code when needed.

The concept of abstraction relates to the idea of hiding data that are not needed for presentation. The main idea behind data abstraction is to give a clear separation between properties of data type and the associated implementation details. This separation is achieved in order that the properties of the abstract data type are visible to the user interface and the implementation details are hidden. Thus, abstraction forms the basic platform for the creation of user-defined data types called objects. Data abstraction is the process of refining data to its essential form. An Abstract Data Type is defined as a data type that is defined in terms of the operations that it supports and not in terms of its structure or implementation.

In object-oriented programming language C++, it is possible to create and provide an interface that accesses only certain elements of data types. The programmer can decide which user to give or grant access to and hide the other details. This concept is called data hiding which is similar in concept to data abstraction.

Advantages of AbstractionFlexibility in approach::
By hiding data or abstracting details that are not needed for presentation, the programmer achieves greater flexibility in approach.

Enhanced Security::
Abstraction gives access to data or details that are needed by users and hide the implementation details, giving enhanced security to application.

Easier Replacement:
With the concept of abstraction in object-oriented programming language, it is possible to replace code without recompilation. This makes the process easier and saves time for users.

Modular Approach:
In object-oriented programming language C++, the abstraction concept helps users to divide the project application into modules and test each of them separately. Then all modules are integrated and ultimately tested together. This approach makes the application development easier.

Q. 2Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each.(a) Single Inheritance(b) Abstraction(c) Encapsulation(d) Function Overloading

(a) Single InheritanceA single inheritance have or else it will be created by a single base class or single derived class
Or
A Single derived class created from a base class.

Function overloading is the practice of declaring the same function with different signatures. The same function name will be used with different number of parameters and parameters of different type. But overloading of functions with different return types are not allowed.

For example let us assume an AddAndDisplay function with different types of parameters.

Q. 3Explain the concept of late binding and dynamic binding with the help of at least two examples. Are these concepts related to polymorphism? Explain your answer.

Q. 4What is the need of UML? Explain the use of any two diagrams used in UML with the help of an example of each.

Q. 5Explain the usage of the following C++ operators with the help of an example program.(a) sizeof operator(b) bitwise AND operator(c) ++ as post increment operator(d) Dereferencing operator

Q. 6Write a function swap that exchanges the values of two variables using a third variable, by using the:(a) Pointers(b) & (address of) operatorExplain the two functions that you have written.

Q. 7Create a class hierarchy for the Worker and Supervisor using C++. The Worker class should include details like worker ID, Name, address of the worker, department ID and Supervisor ID. The classes should have necessary functions to input the required information and a polymorphic function to print the details of the workers or supervisors. For the worker’s this print function should output the worker ID, Name and Supervisor ID. Whereas for the supervisor’s this function should out put the Name and department ID of the supervisor. Please note that a supervisor is also a worker. Make and state suitable assumptions, if any.

Q.8Write a template class “stack” in C++. The class should have functions for push, pop, stack full and stack empty. Use this template to create a stack of integer and a stack of character. Make suitable assumptions, if any.

Q.9Create a class IntStr that stores a positive decimal integer as a string (the maximum number of decimal digits in the string can be assumed to be only 5 digits, so the maximum value that can be stored is 99999, whereas minimum value that can be stored is 00000.). The class should have a copy constructor, and an overloaded + operator. The overloaded + operator should perform the addition. On addition using this + operator, if the length of result exceeds 5 digit then the class should give the result as 00000. Write the necessary main() function to demonstrate the use of the class.