OOPs Assignment.
OOPs Assignment.
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.
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.
{
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