MC0066 Smu Mca Sem2 2011

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

MC0066 – OOPS using C++

(Book ID: B0681 & B0715)

Set-1

1. Write a program that accepts a number ‘n’ from the user and generates Fibonacci
series till n (Fibonacci series starts with 0 and 1 and then the subsequent numbers are
generated by adding the two previous numbers in the series.

Ans:1
Fibonacci Numbers:
0,1,1,2,3,5,8,13,21,34,55,89,. . . …..
By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent number is the
sum of the previous two.

#include<iostream.h>
void main()
{
int number;
int i=0,j=1,k;
cout<<”Enter the upper limit of the series”;
cin>>number;
cout<<”The fibonaci series till”<<number<<”is”<<endl;
if(number<0)
cout<<”invalid number”;
else if(number= = 0)
cout<<i;
else
{
cout<<j<<endl<<j<<endl;
K=i+j;
Do
{
cout<<k<<end;
K=i+j;
I=j;
J=k;
}
while(k<=number);
}
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
2. Write a program to check whether a string is a palindrome or not. Please note that
palindrome is one which remains the same if you reverse the characters in the string. For
example “MADAM”.

Ans:2
Palindrome:

#include <iostream>
#include <string>

using namespace std;

void rString(string s, char *c0)


{
int i = 0;
int j = s.size();
j--;

while (j != -1)
{
c0[i] = s[j];
j--;
i++;
}
c0[i] = '\0';
}

int main()
{
string s = "";
string s1 = "";
string s2 = "";
cout << "Enter a string to test for a Palindrome: ";
getline(cin,s);
int i = 0;
int slen = s.size();
int hslen = slen/2;
char *c0;
c0 = new char[hslen+1]; // allocate new memory for char

if (slen%2 == 0) // string is even, split the string in half, reverse second half and compare
two pieces
{
s1 = s.substr(0, hslen); // first half of string into s1
s2 = s.substr(hslen, hslen); // second half of string into s2
rString(s2, c0); // reverse s2
const char *c1 = s1.c_str();
i = strcmp(c0, c1); // compare s1 with s2 (converted to char array's)
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
if (i == 0)
{
cout << " Palindrome........: " << s << endl;
}
else
{
cout << “ not a Plaindrome" << endl;
delete[] c0;
return 0;
}
}
if (slen%2 != 0) // string is odd, ignore centre char and test the two even halves
{

s1 = s.substr(0, hslen); // move the first half of the string to s1


s2 = s.substr(hslen+1, hslen); // ignore the middle odd char and move the second
half of the string to s2
rString(s2, c0); // reverse the second half of the string
const char *c1 = s1.c_str(); // convert s1 to char to test c0 vs c1 with strcmp();
i = strcmp(c0, c1);

if (i == 0) // if i = 0 c0 and c1 are the same


{
cout << " Palindrome.........: " << s << endl;
}
else
{
cout << " not a Plaindrome" << endl;
delete[] c0;
return 0;
}
}

delete[] c0;
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
3. What is structure in C++? Define a structure named product with elements
productcode, description, unitprice and qtyinhand. Write a C++ program that implements
the structure and enables to store atleast 100 product data.
Ans:3
(a)
Structures in C++:
Structure is a feature in C++ that enables us to define a user-defined data type.
Structures are a group of dissimilar data that are related with each other.
We can define a structure using the keyword struct followed the name of the structure.
For example:
If we want to store all the details of an employee such as employee id(integer),
name(string),department code(integer),and salary as one entity, we can do this using structure
as below:
Struct employee
{
Int empid;
Char name[35];
Int deptcode;
Float salary;
};

(b)
//arrayproduct.cpp
#include<iostream.h>
#include<conio.h>
Struct product
{
Int productcode;
Char description;
Float unitprice;
Int qtyinhand;
}

P[100];
Void main()
{
Int I;
Clrscr();
For(i=0;i<100;i++)
{
Cout<<”Enter Product code”;
Cin>>p[i].productcode;
Cout<<”Enter Product description”;
Cin>>p[i].description;
Cout<<”Enter unit price”;
Cin>>p[i].unitprice;
Cout<<”Enter qty in hand”;
Cin>>p[i].qtyinhand;
}Getch();}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
4. What is the purpose of exception handling? How do you infer from the phrase,
“Throwing an exception”?.

Ans:4
Purpose of Exception Handling:
Exception handling is a programming language construct to handle the occurrence of
exceptions, special conditions that change the normal flow of program execution. We use
exception handling to control your application and improved error recovery. The purpose of
exception handling are to simplify the creation of large, reliable programs using less code than
currently possible, with more confidence that our application doesn’t have an unhandled error.

Throwing an exception:
If we encounter an exceptional situation in our code-that is, one where we don’t have enough
information in the current context to decide what to do-we can send information about the error
into a larger context by creating an object containing that information and “throwing” it out of our
current context. This is called “Throwing an exception.”

Example:
Throw myerror(“something bad happened.”);

Myerror is an ordinary class, which takes a char as its argument. We can use any type when we
throw, but often we will use special types created just for throwing exceptions.
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
Set-2

1. Write a program which accepts a number from the user and generates prime
numbers till that number.

Ans:1
//primegen.cpp
#include<iostream.h>
Void main()
{int number;
Int prime;
Cout<<”Enter a number”;
Cin>>number;
For(int j=2;i<=number;j++)
{
Prime=0;
For(int j=2;j<=i/2;j++)
{
If((i%j)==0)
{
Prime=1;
Break;
}
}if(prime==0)
Cout<<i<<endl’
}
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
2. Implement a class stack which simulates the operations of the stack allowing LIFO
operations. Also implement push and pop operations for the stack.
Ans:2
//stack.cpp
#include<iostream.h>
#define size 100
Class stack
{
Int stck[size];
Int top;
Public:stack(){top=0;
Cout<<”stack initialised”<<endl;
~stack()
{
Cout<<”stack destroyed”<<endl;
}
Void push(int i);
Int pop();
};
Void stack::push(int i)
{
If(top==size){
Cout<<”stack is full”;
Return;
}
Stck[top]=I;
Top++;
}
Int stack::pop()
{
If (top==0)
{
Cout<<”stack underflow”;
Return 0;
}
Top--;
Return stckp[top];
}
Void main()
{
Stack a,b;
a.push(1);
b.push(2);
a.push(3);
b.push(4);
cout<<a.pop()<<””;
cout<<a.pop()<<””;
cout<<b.pop()<<””;
cout<<b.pop()<<endl;
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)

3. What are allocators? Describe the sequence container adapters.

Ans:3
Allocators:
Allocators allocate raw memory, and return it. They do not create or destroy objects. Allocators
are very “low level” features in the STL, and are designed to encapsulate memory allocation and
deallocation. This allows for efficient storage by use of different schemes for particular container
classes. The default allocator, alloc, is thread-safe and has good performance characteristics.

Sequence container adapters:


A container is an object that contains other objects.
The sequence container store data in linear sequence.
The sequence container adapters are used to change the “user interface” to other STL
sequences containers or to user written containers if they satisfy the access functions
requirements.
Example:
If we wanted to implement a stack of items, we might at first decide to base our stack class on
the list container-let’s call it ListStack-and define public member functions for
push(),pop(),empty(),and top(). However, we later decide that another container like a vector
might be better suited to the task. We would then have to define a new stack class, with the
same public interface, but based on the vector, e.g. VectorStack, so that other programmers
could choose a list or a vector based queue. It is obvious tha the number of names for what is
essentially the same thing start to mushroom.

4. Describe the extensibility mechanisms of UML.


Ans:4
Extensibility Mechanisms:
The extensibility mechanisms allow you to customize and extend the UML by adding new
building blocks, creating new properties, and specifying new semantics in order to make the
language suitable for you specific problem domain. There are three common extensibility
mechanisms that are defined by the UML: stereotypes, tagged values, and constraints.
STEREOTYPES:
Stereotypes allow extending the vocabulary of the UML so that we can create new model
elements, derived from existing ones. But that have specific properties that are suitable for our
problem domain. They are used for classifying or marking the UML building blocks in order to
introduce new building blocks that speak the language of our domain and that look like primitive,
or basic, model elements.
Stereotypes also allow to introduce new graphical symbols for providing visual cues to the
models that speak the vocabulary of our specific domain.
TAGGED VALUES:
Tagged values are properties for specifying keyword-value pairs of model elements, where the
keywords are attributes. They allow us to extend the properties of a UML building block so that
we create new information in the specification of that element. Tagged value can be defined for
existing model elements, or for individual stereotypes, so that everything with that stereotype
has that tagged value.
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
Constraints are properties for specifying semantics and/or conditions that must be held true at
all times for the elements of a model. They allow us to extend the semantics of a UML building
block by adding new rules, or, modifying existing ones.

You might also like