Friday, June 24, 2016

SMU Assignment - (Sem-2) - MCA2030- OBJECT ORIENTED PROGRAMMING – C++

MCA2030- OBJECT ORIENTED PROGRAMMING – C++

Q1.)    Write short notes on:
a) Switch statement
b) Conditional Operator
Ans.)    (a) Switch Statement: Nested ifs can be confusing if the if statement is deeply nested. One alternative to nested if is the switch statement which can be used to increase clarity in case of checking the different values of the same variable & execute statements accordingly.

Syntax:
Switch (variable name)
{Case value 1: statement1;
Break;
{Case value 2: statement2;
Break;
{Case value 3: statement3;
Break;
Default: statement4;
}

If the variable in the switch statement is equal to value 1 then statement 1 is executed, if it is equal to value 2 then statement 2 is executed, if it is value 3 then statement 3 is executed. If the variable value is not in any of the cases listed then the default case statement 4 is executed. The default case specification is optional; however keeping it is a good practice. It can also be used for displaying any error message. Each case can have any number of statements. However every case should have a break statement as the last statement. Break statement takes the control out of the switch statement. The absence of the break statement can cause execution of statements in the next case. No break is necessary for the last case. In the above example, default case does not contain a break statement.

(b) Conditional Operator: Conditional Operator (? :) is a handy operator which acts like a shortcut for it else statement. If you had to compare two variables a and b ad then depending on which is larger, you wanted to store that variable in another variable called large. You would do this using if else statement in the following way:

If (a>b)
Large=a;
Else
Large=b;
The above can e done using conditional operator in the following way:
Large= (a>b)? a: b ;
--------------------------------------------------------------------------------------------------------------------------

Q2.)    Differentiate Classes and Objects. Write a program to create a class and its object.
Ans.)    Classes provide users a method to create user defined data types. We have seen two types of user defined data types structures and enumeration. Both are primarily used for modeling data only. In several real life situation, we would require to model functionality of the data type along with the data together. However, in C++ we can use structures same as classes by binding functions with data. But usually the structures are only used when we want to group data and classes when we want to group both data and functions.

Classes can model either physical or real things such as employee, product, customer etc. or it can model a new data type such user defined string, distance, etc.

Objects have the same relationship to a class that a variable has to a data type. Object is an instance of a class. Class is a definition, objects are used in the program that enables you to store data use them.

To understand the various syntactical requirements for implementing a class, let us take an example.

#include <iostream.h>
Class distance
{
 Private:
Int feet;
Int inches;
public;
Void set distance (int a, int b)
{
  Feet=a;
  Inches=b;
}
Void display ()
{count<<feet<<”ft”<<inches<<”inches ;}
};
Void main ()
{Distance d1;
D1. Set distance (10, 2);
D1.display ();
}

In the above program, we have created a class called distance with two data members feet and inches and two functions or methods set distance and display (). Classes are defined with the keyword class followed by the name of the class. The object of this class is d1. Every objects of a class have their own copy of data, but all the objects of a class share the functions. No separate copy of the methods is created for each and every object. Objects can access the data through the methods. To invoke a method, object name .method name is used. The methods defined in the class can only be invoked by the objects of the class. Without an object, methods of the function cannot be invoked.

You can see in the above program, the data elements feet inches are defined as private. The methods are defined as public. These are known as access specifies. Data and functions in a class can have any one access specifies: private, public and protected. Protected access specifier. Private data and functions can be accessed only by the member functions of the class. None of the external functions can be accessed through the objects of the class externally from any function. However to access these data and functions, we need to have an object of that class.
--------------------------------------------------------------------------------------------------------------------------

Q3.)    Describe operator overloading. List the operators that cannot be overloaded.
Ans.)    Operator overloading is just “syntactic sugar,” which means it is simply another way for you to make a function call. The difference is that the arguments for this function don’t appear inside parentheses, but instead they surround or are next to characters you’ve always thought of as immutable operators. There are two differences between the use of an operator and an ordinary function call. The syntax is different; an operator is often “called” by placing it between or sometimes after the arguments. The second difference is that the compiler determines which “function” to call. For instance, if we are using the operator + with floating-point arguments, the compiler “calls” the function to perform floating-point addition (this “call” is typically the act of inserting in-line code, or a floating-point-processor instruction). If we use operator + with a floating-point number and an integer, the compiler “calls” a special function to turn the int into a float, and then “calls” the floating-point addition code.

But in C++, it’s possible to define new operators that work with classes. This definition is just like an ordinary function definition except that the name of the function consists of the keyword operator followed by the operator. That’s the only difference, and it becomes a function like any other function, which the compiler calls when it sees the appropriate pattern.

The operators that cannot be overloaded are:

1.   ?: (conditional)
2.   . (Member selection)
3.   .* (member selection with pointer-to-member)
4.   :: (scope resolution)
5.   sizeof (object size information)
6.   typeid (object type information)
--------------------------------------------------------------------------------------------------------------------------

Q4.)    What are the advantages of Polymorphism? How it can be implemented?
Ans.)    Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation.

Advantages of polymorphism:

1.      Same interface could be used for creating methods with different implementations
2.      Reduces the volume of work in terms of distinguishing and handling various objects
3.      Supports building extensible systems
4.      Complete implementation can be replaced by using same method signatures

Polymorphism can be implemented in following ways:

a)      There has to be a pointer of the derived class that has implemented the polymorphic function that holds the address of the derived class object.
b)      The function must be declared as virtual in both the base class and in the derived class that overrides the function.
c)      The function must be declared as pure virtual.
d)      There has to be a base class pointer holding the address of a base or derived class object that has implemented the polymorphic function.
e)      The function must be declared as virtual in the base class.

--------------------------------------------------------------------------------------------------------------------------

Q5.)    Differentiate Containers and Iterators.
Ans.)    Containers: 
STL provides a number of container types, representing objects that contain other objects. The STL contains sequence containers & associative containers. The standard sequence containers include vector, deque & list. Sequence containers in, as their name suggests, store data in linear sequence. The standard associative containers are set, multiset, map & multimap. Associative containers are a generalization of sequences. Sequences are indexed by integers; associative containers can be indexed by any type. Other types of containers are – bitset (stores series of bits similar to a fixed-sized vector of bools, also optimizes for space), & valarray (another C-like array vector, but is designed for high speed numeric’s at the expense of some programming ease & general purpose use. It has many features that make it ideally suited for use with vector processors in traditional vector supercomputers & SIMD units in consumer-level scalar processors, & also ease vector mathematics programming even in scalar computers).

Iterators:

Iterators are like location specifiers for containers or streams of data, in the same way that an int* can be used as location specifier for an array of integers, or an if stream can be used as a location specifier for a file. The STL implements five different types of Iterators. These are input Iterators (which can only be used to read a sequence of values), output Iterators (which can only be used to write a sequence of values), forward Iterators (which can be read, written to, & move forward), bidirectional Iterators (which are like forward Iterators but can also move backwards) & random access Iterators (which can move freely any number of steps in one operation).

It is possibly to have bidirectional Iterators act like random access Iterators, as moving forward ten steps could be done by simply moving forward a step at a time a total of ten times. However, having distinct random access Iterators offers efficiency advantages. For example, a vector would have a random access iterator, but a list only a bidirectional iterator.
--------------------------------------------------------------------------------------------------------------------------

Q6.)    Describe the two basic models in exception handling theory.
Ans.)    There are two basic models in exception-handling theory. In termination (which is what C++ supports) we assume the error is so critical there’s no way to get back to where the exception occurred. Whoever threw the exception decided there was no way to salvage the situation, & they don’t want to come back. The alternative is called resumption. It means the exception handler is expected to do something to rectify the situation, & then the faulting function is retired, presuming success the second time. If you want resumption, you still hope to continue execution after the exception is handled, so your exception is more like a function call – which is how you should set up situations in C++ where we want resumption- like behavior (that is, don’t throw an exception; call a function that fixes the problem). Alternatively, place your try block inside a while loop that keeps reentering the try block until the result is satisfactory.

Historically, programmers using operating systems that supported presumptive exception handling eventually ended up using termination-like code & skipping resumption. So although resumption sounds attractive at first, it seems it isn’t quite so useful in practice. One reason may be the distance that can occur between the exception & its handler; it’s one thing to terminate to a handler that’s far away, but to jump to that handler & then back again may be too conceptually difficult for large systems where the exception can be generated from many points.

--------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment