0% found this document useful (0 votes)
240 views9 pages

OOPs Assignment.

The document discusses object oriented programming in C++. It includes summaries of switch statements, conditional operators, classes vs objects, operator overloading, polymorphism, containers vs iterators, and exception handling models in C++. Key points include: - Switch statements allow a variable to be tested against multiple case values. - Conditional operators provide a shorthand for if/else statements. - Classes define types while objects are instances of classes. - Operator overloading allows operators to have different implementations based on arguments. - Polymorphism allows functions to have different behaviors based on an object's type. - Containers store elements while iterators access elements through increment and dereference operations. - Exception handling

Uploaded by

Vibhu Rastogi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
240 views9 pages

OOPs Assignment.

The document discusses object oriented programming in C++. It includes summaries of switch statements, conditional operators, classes vs objects, operator overloading, polymorphism, containers vs iterators, and exception handling models in C++. Key points include: - Switch statements allow a variable to be tested against multiple case values. - Conditional operators provide a shorthand for if/else statements. - Classes define types while objects are instances of classes. - Operator overloading allows operators to have different implementations based on arguments. - Polymorphism allows functions to have different behaviors based on an object's type. - Containers store elements while iterators access elements through increment and dereference operations. - Exception handling

Uploaded by

Vibhu Rastogi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

MCA2030- OBJECT ORIENTED

PROGRAMMING C++
Q1. Write short notes on:
a) Switch statement b) Conditional Operator
Ans: a) Switch Statement: Switch statement is a selection
control statement. Although, weve if statement as well for
that purpose but what if the number of conditions are too many
or theres a lot of nesting needed. So to avoid error we use
Switch statement. A switch statement allows a variable to be
tested for equality against a list of values. Each value is called
a case, and the variable being switched on is checked for each
case.
Syntax:
Switch (variablename)
{ case value1: statement1;
break;
case value2: statement2;
break;
case value3: statement3;
break;
default: statement4;
}
If the variable in the switch statement is equal to value1 then
statement1 is executed, if it is equal to value2 then statement2
is executed, if it is value3 then statement3 is executed. If the
variable value is not in any of the cases listed then the default
case statement is executed. The default case specification is
optional; however keeping it is a good practice. Mostly it is used
to display 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.

b) Conditional Operator: Conditional operator is also an


alternate to if else statement. There is an expression,
which is evaluated and then as per the condition the value
is returned. Conditional operator (?:) is a handy operator
which acts like a shortcut for if else statement.
Syntax:
(Expression)? Value1: Value2;
The expression is evaluated and the condition is checked. If the
condition is true then the Value1 is returned else the Value2 is
returned. For example, if were checking which number is
greatest and want to store the result in a variable then it can be
like this:
large= (a>b) ? a : b ;

Q2. Differentiate Classes and Objects. Write a program


to create a class and its object.
Ans: A class is a user defined data type or data structure
declared with the keyword class that encapsulates data and
functions together. However, an object is an identifiable entity
which has some state and behaviour or in simple words, any
instance of a class is called as object.
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 and use
them.

For example, weve Motorcycle, cycle, cars, trucks, etc.


Although they all have different characteristics and features,
however they have few things common and that is they all are
automobiles. So together they can be put into a single class as
vehicles. A class can be of similar or dissimilar group of data.
On other hand, object is the instance of a class which is used to
invoke the methods encapsulated within the class. To access
the data members of a class, we need objects. Object has the
same relationship to a class that a variable has to a datatype.
Lets create a class and its object through a program:
#include<iostream.h>
#include<stdio.h>
class distance
{
private:
int feet;
int inches;
public:
void setdistance(int a, int b)
{
feet=a;
inches=b;
}
void display()
{
cout<<feet<<ft<<inches<<inches;
}
}; //class end.
void main()
{
distance d1;
d1. setdistance(10, 2);
d1.display();
getch();
}

Q3. Describe operator overloading. List the operators


that cannot be overloaded.
Ans: Basically, overloading is the ability to be used in more
than one form. Its similar to polymorphism, which means to
exist in more than one form. Operator overloading is the ability
to tell the compiler how to perform a certain operation when its
corresponding operator is used on one or more variables. In
operator overloading different operators have different
implementations depending on their arguments. Operator
overloading should be used for a class where it is required to
perform obvious functions with the default operators and there
can be no other meaning for the same.
With such an extensive usability and characteristics, there are
certain limitations as well with operator overloading. There are
several operators which cannot be overloaded. They are as
follows:
.
(Member Access or Dot operator)
?:
(Ternary or Conditional Operator )
::
(Scope Resolution Operator)
.*
(Pointer-to-member Operator )
sizeof
(Object size Operator)
typeid
(Object type Operator)
# and ##(Pre-processor directive)
Example:
#include<iostream.h>
class counter
{
unsigned int count;
public:
counter()
{
count=0;}
int getcount()
{

return count;}
void operator ++()
{
count++;}
};
void main()
{
counter c1;
c1++;
++c1;
cout<<c1.getcount();
getch();
}
Q4. What are the advantages of Polymorphism? How it
can be implemented?
Ans: Polymorphism is the ability to exist in more than one form.
In C++, polymorphism means that a call to a member function
will cause a different function to be executed depending on the
type of object that invokes the function.
Following are the advantages of polymorphism:
Simplicity - If you need to write code that deals with a
family of types, the code can ignore type-specific details
and just interact with the base type of the family. This
makes the code easier for programmer to write and easier
for others to understand.
Extensibility Other subclasses could be added later to
the family of types, and objects of those new subclasses
would also work with the existing code.
Code reusability Same code can be used multiple times.
In other words, a same or different function can perform
same task for different data member.
Maintainability Its easier to insert more functions in the
code without worrying of making changes in the existing
code.

To implement polymorphism, several conditions should be met.


Firstly, all the subclasses should be derived from a single class.
Secondly, the member function implementing polymorphism
must be declared as virtual in its base class. There are several
types of polymorphism, like Operator overloading, function
overloading, virtual functions. The most common type of
polymorphism is function overloading.
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
virtual int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape{
public:
Rectangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape{
public:
Triangle( int a=0, int b=0):Shape(a, b) { }
int area ()

{
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
int main( )
{
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
shape = &rec;
shape->area();
shape = &tri;
shape->area();
return 0;
}
Q5. Differentiate Containers and Iterators.
Ans: A container is a holder object that stores a collection of
other objects (its elements). They are implemented as class
templates, which allow a great flexibility in the types supported
as elements. The container manages the storage space for its
elements and provides member functions to access them,
either directly or through iterators. The STL contains sequence
containers and associative containers. The standard sequence
containers include vector, deque and list. Sequence containers
in, as their name suggests, store data in linear sequence. The
standard associative containers are set, multiset, map and
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 fixedsized vector of bools, also optimizes for space), and valarray.
Many containers have several member functions in common,
and share functionalities. The decision of which type of
container to use for a specific need does not generally depend

only on the functionality offered by the container, but also on


the efficiency of some of its members (complexity).
Whereas, Iterators are like location specifiers for containers or
streams of data, in the same way that an int* can be used as a
location specifier for an array of integers, or an ifstream can be
used as a location specifier for a file. They have the ability to
iterate through the elements of that range using a set of
operators (with at least the increment (++) and dereference (*)
operators).
The most obvious form of iterator is a pointer: A pointer can
point to elements in an array, and can iterate through them
using the increment operator (++).
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, and move forward), bidirectional iterators (which are
like forward iterators but can also move backwards) and
random access iterators (which can move freely any number of
steps in one operation).

Q6. Describe the two basic models in exception handling


theory.
Ans: Exceptions provide a way to transfer control from one part
of a program to another. C++ exception handling is built upon
three keywords: try, catch, and throw.
There are two basic models in exception handling Termination
and Resumption.
In termination, you assume the error is so critical theres no
way to get back to where the exception occurred. Whoever
threw the exception decided there was no way to salvage the
situation, and they dont want to come back. However in
resumption, it means the exception handler is expected to do
something to rectify the situation, and then the faulting

function is retried, presuming success the second time. In


resumption, doesnt throw exception. Simply call the function
that fixes the problem. Alternatively, place your try block inside
a while loop that keeps re-entering the try block until the result
is satisfactory.
In the resumption model this action is supported, in the
termination model, resuming is disallowed and execution is
always transferred to the exception handler and from there to
the next valid instruction in the handler's scope.
Programmers using operating systems that supported
resumptive exception handling eventually ended up using
termination-like code and skipping resumption. Although
resumption sounds attractive at first, it seems it isnt quite so
useful in practice. One reason may be the distance that can
occur between the exception and its handler.

You might also like