mc0066 – oops using c++

9
Spring 2012 Master of Computer Application (MCA) – Semester II MC0066 – OOPS using C++ 1. Distinguished between procedural language and OOP language. And Explain the key features of OOP. Ans: Procedural Language OOP Language Procedural programming creates a step by step program that guides the application through a sequence of instructions. Each program is made up of many entities called objects. Procedural programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. Procedural programming also focuses on the idea that all algorithms are executed with functions and data that the programmer has access to and is able to change. Object-Oriented programming is much more similar to the way the real world works; it is analogous to the human brain. Each program is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Objects cannot directly access another object’s data. Instead, a message must be sent requesting the data, just like people must ask one another for information; we cannot see inside each other’s heads. Benefits of Object-Oriented programming include: ability to simulate real-world event much more effectively code is reusable thus less code may have to be written data becomes active

Upload: ashulotus

Post on 23-Oct-2014

114 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: MC0066 – OOPS using C++

Spring 2012

Master of Computer Application (MCA) – Semester II

MC0066 – OOPS using C++

1. Distinguished between procedural language and OOP language. And Explain the key features of OOP.

Ans:

Procedural Language OOP LanguageProcedural programming creates a step by step program that guides the application through a sequence of instructions.

Each program is made up of many entities called objects.

Procedural programming creates a step by step program that guides the application through a sequence

of instructions. Each instruction is executed in order. Procedural programming also focuses on the idea

that all algorithms are executed with functions and data that the programmer has access to and is able to

change. Object-Oriented programming is much more similar to the way the real world works; it is

analogous to the human brain. Each program is made up of many entities called objects. Objects become

the fundamental units and have behavior, or a specific purpose, associated with them. Objects cannot

directly access another object’s data. Instead, a message must be sent requesting the data, just like

people must ask one another for information; we cannot see inside each other’s heads. Benefits of

Object-Oriented programming include: 

ability to simulate real-world event much more effectively

code is reusable thus less code may have to be written

data becomes active

better able to create GUI (graphical user interface) applications

programmers are able to reach their goals faster

Programmers are able to produce faster, more accurate and better-written applications (in the

case of a veteran programmer, by a factor of as much as 20 times compared with a procedural

program).

Page 2: MC0066 – OOPS using C++

2. What is function overloading? Write a c++ program to implement a function overloaded.

Ans: Function overloading is process that allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. It is simply defined as the ability of one function to perform different tasks.

e.g.:

main()

{

cout<<volume(10);

cout<<volume(2.5,8);

cout<<volume(100,75,15);

}

// volume of a cube

int volume(int s)

{

return(s*s*s);

}

// volume of a cylinder

double volume(double r,int h)

{

return(3.14*r*r*h);

}

// volume of a cuboid

long volume(long l,int b,int h)

{

return(l*b*h);

}

3. Discuss the constructors and Destructors with suitable example.

Ans: Constructor function gets invoked when an object of a class is constructed (declared) and destructor function

gets invoked when the object is destructed (goes out of scope).Use of Constructor and Destructor function of a class.Constructor function is used to initialize member variables to

Page 3: MC0066 – OOPS using C++

pre-defined values as soon as an object of a class is declared.Constructor function having parameters is used to initialize the data members to the values passed values, upon declarationGenerally, the destructor function is needed only when constructor has allocated dynamic memory.Example : constructor

#include<iostream.h>class myclass{ private: int a; int b;public:myclass() //here constructor function is used to{a=10; //initialize data members to pre-def Valuesb=10;}

int add(void){return a+b;}};

void main(void){myclass a;

cout<<a.add();}

Example Destructor#include<iostream.h>

class myclass{public:

~myclass(){cout<<"destructed\n";}};

void main(void){myclass obj;cout<<"inside main\n";}

4. What do you mean by operator overloading? Illustrate with suitable example for overloading Unary operators.

Ans: Operator Overloading is feature of C++ that allows programmers to specify how various arithmetic, relational

and many other operators work with defined datatypes or classes.

Page 4: MC0066 – OOPS using C++

Example: Unary Operator Overloading

#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();

}

5. Write C++ program which demonstrate the difference between static and dynamic binding.

Ans

6. Difference between a static member function and non-static member functions with appropriate example.

Ans: The difference between a static member function and non-static member function are as follows:

A static member function can access only static member data, static member function and functions outside the class. A non-static member function can access all of the above including the static data member.

A static member function can be called , even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.

A static member function can’t be declared as virtual whereas non-static can be declared as virtual

Page 5: MC0066 – OOPS using C++

Example:

#include<iostream.h>

Using namespace std;

class X

{

private:

int I;

static int si;

public:

void set_i(int arg)

{i-arg;}

static void set_si(int arg)

{si=arg;}

void print_i{

cout<<”Value of I =”<<i<<’\n’;;

cout<<”Again, Value of i= ”<<this->i<<’\n’;

}

static void print_si(){

cout<<”Value of si =”<<si<<’\n’;;

cout<<”Again, Value ofs i= ”<<this->si<<’\n’;

}

};

int X::si =77; ///Initialize static data member

int main() {

X ab;

ab.set_i(11);

ab.print_i();

X::print_si();

Page 6: MC0066 – OOPS using C++

X::set_si(22);

X::print_si();

}

7. Writer C++ program to demonstrate the complete implementation of class template stack.

Ans:

#pragma once

template <class T>

class Stack {

public:

Stack(int=10);

~Stack(){delete[] stackPtr; }

Int push(const T&);

Int pop(T&); //pop an element off the stack

Int Isempty()const {return top==-1; }

Int isfull()const {return top == size-1; }

private:

int size;

int top;

T* staackPtr;

};

template<classT> //contructor with the default size 100

Stack<T>::Stack(int s){

Size= s > 0 && s < 1000 ? s:10;

Top=-1;

stackPtr=new T[size];

}

template <class T>

Page 7: MC0066 – OOPS using C++

int Stack<T>::push(const T& item){

if(!isFull())

{

stackPtr[++top]== item;

return 1; } //push Successful

return 0; //push unsuccessful

}

template <class T>

int Stack<T>::pop(const T& item){

if(!isempty())

{

popValue= stackPtr[top--];

return 1; } //pop Successful

return 0; //pop unsuccessful

}

8. What is template specialization? Describe a scenario in which template class partial specialization is considered appropriate.

Ans: Template Specialization:  it allows the programmer to specialize only some arguments of a class template, as opposed to explicit specialization, where all the template arguments are provided.

OR

In some cases it is possible to possible to override the tem[late-generated code by providing special definations for specific types

Example:

#include<iostream.h>

Using namespace std;

template <class T>

class stream{

Public:

Page 8: MC0066 – OOPS using C++

Void f() { cout<<”stream<T>::f()”<<endl; }

};

template <class T>

class stream<char>{

public:

void f() { cout<<”stream<char>::f()”<<’\n’;

};

Int main() {

Stream<int> si;

Stream<char> sc;

Si.f();

Sc.f();

return 0;

}

Assignment Set -2

1. Write advantage of multiple inheritances. And write a c++ program to implement the multiple inheritances.

2. Discuss the types of Inheritance with suitable example for each.3. Write a c++ program to implements the relational operator overloading for the distance class.

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

5. Discuss the various STL components in brief. 6. Describe the time overhead of operations on sequence containers.7. Discuss how object diagrams are different from class diagram.