mc 0066-02 c++

15
MC0066-02 OOPS USING C ++ 2) Discuss the types of inheritance with suitable example for each. Types of Inheritance: Inheritance have single inheritance with one base and derived class. We can have several classes derived from a single base class. These inheritance can also be multilevel inheritance where another class is derived from the derived class. In such case the grand child class inherits all the properties of child and parent classes. 1

Upload: shrikrush

Post on 24-Dec-2015

1 views

Category:

Documents


0 download

DESCRIPTION

SMUDE MCA MC0066 II

TRANSCRIPT

Page 1: MC 0066-02 C++

MC0066-02

OOPS USING C ++

2) Discuss the types of inheritance with suitable example for each.

Types of Inheritance:

Inheritance have single inheritance with one base and derived class. We

can have several classes derived from a single base class. These inheritance can also be

multilevel inheritance where another class is derived from the derived class. In such

case the grand child class inherits all the properties of child and parent classes.

1

Page 2: MC 0066-02 C++

The derived class can also have multiple parents which is known as

multiple inheritance. Here the child or derived class has two or more parent classes.

The child class inherits all the properties of all its parents. Multiple inheritance is

implemented same as single inheritance except that both the parents have to be specified

while defining the class.

We had seen in the inheritance.cpp program, public inheritance. There can also be

private inheritance. In the public inheritance, keyword public specifies that objects of

derived class can access public member functions of the base class. If the derived class

is derived privately then objects of the derived class cannot access the public member

functions of the base class. Since objects cannot access private or protected members of

a class, no members of the base class will be accessible by the objects of the derived

class.

3) Write a C++ program to implement the relational operator overloading for the

distance class

# include<iostream.h>

class distance

{ private:

int feet, inches;

public:

distance ( )

{feet = 0; inches = 0;}

distance ( int f )

{feet = f; inches = 0;}

distance (int f, int i)

2

Page 3: MC 0066-02 C++

{feet = f; inches = i;}

void display ( );

void getdata( );

int operator < (distance d1)

{ if (feet <d1.feet)

return 1;

else if (feet = = d1.feet) && (inches <d1.inches)

return 1;

else

return 0;

}

};

void distance : : display ( )

{ cout <<” “ <<inches<<endl;}

cin >>feet>>inches;}

void main ( )

{

distance d1, d2;d1.getdata ( );

d2.getdata ( );if (d1<d2)

cout<<d1.display ( ) << “is smaller”;

else

cout <<d2.display ( ) <, “is smaller”;

}

Here the d1 object invokes the operator < and d2 is passed an argument. The return

value is either 0 or 1 which indicates false or true respectively. Certain compilers

support Boolean datatype which can be alternatively usedinstead of integer. The above

program also implements display and getdata member functions differently. The

member functions are declared inside the class but are defined outside the class. But to

3

Page 4: MC 0066-02 C++

specify that the member function belongs to the distance class, the class name is

included along with the function name separated by the scope resolution operator (::).

1) Write advantages of multiple inheritance. And write a C++ program to

implement the multiple inheritance.

Multiple inheritance is the process of inheriting a class from more than one parent class.

This would be required in several instances where you would like to have the

functionalities of several classes. This is also extensively used in class libraries.

The syntax for implementing multiple inheritance is similar to single inheritance. For

ex: if there are two classes A and B, and if we want to derive a class C from A and B,

then the syntax of class definition will be as follows:-

class C : public A, public B

{

};

C++ program to implement the multiple inheritance:-

// multiple.cpp

# include<iostream.h>

# include<string.h>

# include<conio.h>

class student

{ protected:

char qual[6]; // highest degree earned

int percent; // percentage score in the last degree

public:

student ( )

4

Page 5: MC 0066-02 C++

{ strcpy(qual, “ “); percent = 0;}

student (char ch[6], int p)

{ strcpy (qual, ch); percent = p;}

void display ( )

{ cout<<endl<<”Qualification”<<qual;

cout<<endl<<”Score”<<percent;

}};

class employee {

protected:

int empno;

char ename[25];

public

employee ( )

{ empno = 0;

strcpy (ename, “ “);

}

employee (int n, char ch[25])

{ empno = n;

strcpy (ename, ch);

}

void display ( )

{ cout<<endl<<”Emp Code:”,,empno;

cout<<endl<<”Name:”<<ename;

}

};

class manager: public employee, public student

{

protected:

float basic;

5

Page 6: MC 0066-02 C++

float hra;

public:

manager ( ):employee ( ),student ( )

{basic = 0.0; hra = 0.0;}

manager (int n, char ch[25], char ch 1[6], int p, float i, float j): employee (n, ch),

student (ch1, p)

{ basic = i; hra = j;}

void display ( )

{ employee ::display ( );

student::display ( );

cout<<endl <<”Basic”<<basic;

cout<<endl <,”HRA<<hra

}

};

void main ( )

{

clrscr ( );

manager m1 (205, “Pawan”, “MBA”, 80, 40000.00,5000.00);

m1.display ( );

getch ( );

}

Here in the above program, that the derived class constructors calls both the parent class

constructors. This is because every object of the derived class has its own copy of parent

data members. therefore it is required to initialize them as well. The parent class

member functions are invoked using the scope resolution operator as shown in the

display function of the manager class.

The ouput will be as follows-

Emp Code: 205

6

Page 7: MC 0066-02 C++

Name : Pawan

Qualification : MBA

Score : 80

Basic : 40000

HRA 5000

4) Write the advantages of using exception handling with its basic models.

The thrown exception must end up someplace. This is called Exception Handler. and

there’s one for every exception type you want to catch. Exception handlers immediately

follow the try block and are denoted by the keyword catch as follows:-

try {

// code that may generate exceptions

} catch (type 1 id 1) {

// handle exceptions of type 1

} catch (type2 id2) {

// handle exceptions of type2

}

// etc…

Each catch clause (exception handler) is like a little function that takes a single argument

one particular type. The identifier (id1, id2 and so on) may be used inside the handler,

just like a function argument, although, sometimes there is no identifier because it’s not

needed in the handler- the exception type gives you enough information to deal with it.

the handlers must appear directly after the try block. If an exception is thrown, the

exception handling mechanism goes hunting for the first handler with an argument that

matches the type of the exception. Then it enters that catch clause, and the exception is

7

Page 8: MC 0066-02 C++

considered handled. ( the search for handler stops once the catch clause is finished.)

Only the matching catch clause executes; it’s not like a switch statement where you need

a break after each case to prevent the remaining ones from executing. Here within the

try lock, a number of different function calls might generate the same exception, but you

only need one handler.

5) Discuss the various STL components in brief

There are four types of STL components. They are as follows:-

Containers

STL provides a number of container types, representing objects that contain other

objects. The STL contains sequence containers and associative containers. The standard

sequence containers include vector, deque and list. Sequence containers, as their name

suggests, store data in linear sequebnce. 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 and valarray(another C like array like vector, but is

designed for high speed numerics at the expense of some programming ease and general

purpose use.

Iterators

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 the file. The STL implements 5 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).

8

Page 9: MC 0066-02 C++

It is possible to have bidirectional act like random access iterators, as moving

forward ten steps could be done by simply moving forward at a time a total of ten times..

However, having distinct random access iterators offers efficiency advantages. Iterators

are the major features which allow the generality of the STL. User-created containers

only have to provide an iterators which implements one of the 5 standard iterator

interface, and all the algorithms provided in the STL can be used on the container.

Algorithms

The STL algorithms are templates C++ functions to perform operations on

containers. In order to be able to work with many different types of containers, the

algorithms do not take containers as arguments. Instead, they take iterators that specify

part or all of a container. In this way the algorithms can be used to work on entities that

are not containers. A large number of algorithms to perform operations such as

searching and sorting are provided in the STL; each implemented to require a certain

level of iteratorand therefore will work on any container which provides an interface by

iterators.

The algorithms include sorting operations-sort, merge, min, max, etc., searching

operations – find, count, equal, etc., mutating operations – transform, replace, fill, rotate,

shuffle, and generalized numeric operators – accumulate, adjacent difference, etc.

Functors

The STL includes classes that overload the function operator (operator ( ) ).

Classes that do this are called functors or function objects. They are useful for keeping

and retrieving state information in functions passed into other functions. Regular

function pointers can also be used as functors. Function objects are STL’s way of

representing “executable data”. This applies a function to each object in a container. We

need to be able to specify what to do to each object in the container.

9

Page 10: MC 0066-02 C++

6) Discuss the time overhead of operations on sequence containers.

There are 3 types of sequence containers in STL. Theya re

- vector<Type>

- deque<Type>

- list<Type>

To choose a container, we have to decide what sort of operations we most frequently

perform on our data, and then we have to use this table.

Operation Vector Deque List

Access first element Constant constant constant

Access last element constant constant constant

Access random element constant constant linear

Add/delete at beginning linear constant constant

Add/delete at end constant constant constant

Add/delete at random linear linear constant

10

Page 11: MC 0066-02 C++

11