Вы находитесь на странице: 1из 8

MASTER

OF

COMPUTER APPLICATION (MCA) SEMESTER 2 MC0066 OOPS USING C++ ASSIGNMENT SET 1

Book ID: B0681 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.
// Fibonacci series upto n term i. e. 0 #include <iostream.h> #include <stdlib.h> class fibo { private: int n, a, b, c; public: void getnterm(); void showseries(); fibo() { a = 0; b = 1; c = 0; } }; void fibo :: getnterm() { cout << "\t\tFibonacci Series:" << endl << endl; cout << "Enter nth term: "; cin >> n; if (n <= 0) { 1 2 3 5 8 .. n

cout << "Invalid input "; exit (0); } } void fibo :: showseries() { cout << "\n Series: "; cout << a << "\t" << b << "\t"; for (int p = 3; p <= n; p++) { c = a + b; cout << c << "\t"; a = b; b = c; } } int main(void) { fibo ob; ob.getnterm(); ob.showseries(); return 1; }

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.
// Palindrome checking i.e. input string = reverse of input string #include <iostream.h> #include <string.h> #include <conio.h> class palin { private: char s[50], r[50], c; public: void getstr();

void revstr(); void showresult(); palin() { c=0; } }; void palin::getstr() { cout << "\t\t Palindrome checking: " << endl << endl; cout << "Enter any string: "; cin >> s; } void palin :: revstr() { for(int p = strlen(s) - 1; p >= 0; p--) { r[c++] = s[p]; } r[c] = '\0'; } void palin :: showresult() { cout << "\nOriginal string = " << s << endl; cout << "\nReverse string = " << r << endl; if (strcmpi(s, r) == 0) cout << "\n So It's Palindrom "; else cout << "\n So It's not Palindrom "; } int main(void) { palin ob; ob.getstr(); ob.revstr(); ob.showresult(); return 1; }

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.
Structure in C++ As we know that grouping data of same type can be stored together with arrays. But different types of data can not be grouped and stored in array. Data that are of different types but are logically related can also be grouped together. This can be done through structures. Structures are a group of dissimilar data that are related with each other. 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 shown below: struct employee { int empid; char name[35]; int deptcode; float salary; }; Structure is a feature in C++ that enables us to define a userdefined datatype. Once we specify the definition of the structure, we can create variables of that structure. In the above definition, a structure is defined using the keyword struct followed by the name of the structure (employee). All the data items that need to be defined are defined by specifying the datatype and name of the variable. Once the definition is done, variables of type employee can be created. For example, the following statement creates a variable e of type employee. employee e; // recod single employee employee e[100]; // record 100 employee

// Use of structure demonstration #include <iostream.h> #include <string.h> #include <conio.h> struct product { int productcode; char description[50];

double unitprice; int qtyinhand; }p[100]; class store { int i; public: void getrecord(); void showrecord(); store() { i = 0; } }; void store :: getrecord() { cout << "\t\tGeneral Store" << endl << endl; while(i <= 1) { cout << "Enter productcode: "; cin >> p[i].productcode; cout << "Enter description: "; cin >> p[i].description; cout << "Enter unitprice: "; cin >> p[i].unitprice; cout << "Enter quantity in hand: "; cin >> p[i].qtyinhand; i++; } } void store :: showrecord() { i = 0; cout << "\nProduct code Description Unitprice QIH\n"; while(i <= 1) { cout << p[i].productcode << " " << p[i].description << " "; cout << p[i].unitprice << " " << p[i].qtyinhand << endl; i++; } } int main(void) {

store ob; ob.getrecord(); ob.showrecord(); return 1; }

Book ID: B0715 4. What is the purpose of exception handling? How do you infer from the phrase Throwing and exception.
An exception is an error or an unexpected event. The exception handler is x set of codes that executes when an exception occurs. The purpose of exception handling mechanism is to provide means to detect and report on "exceptional circumstance" so that appropriate action can be taken. Exception handling provides a way of transferring control and information to an unspecified caller that has expressed willingness to handle exceptions of a given type. Exceptions of arbitrary types can be thrown and caught and the set of exceptions a function may throw can be specified. The termination model of exception handling is provided. Exception handling can be used to support notions of error handling and fault tolerant computing. Wherever an exception occurs, the error handling code. i. ii. iii. iv. Find the problem (Hit the exception) Inform that an error has occurred (throw the exception) Receive the error information(catch the exception) Take connective action (Handle the exception).

Throwing an Exception In C++, a throw statement is used to signal that an exception or error case has occurred (think of throwing a penalty flag in football by referee). Signaling that an exception has occurred is also commonly called raising an exception. To use a throw statement, simply use the throw keyword, followed by a value of any data type you wish to use to signal that an error has occurred. Typically, this value will be an error code, a description of the problem, or a custom exception class. Here are some examples: throw -1; // throw a literal integer value

throw ENUM_INVALID_INDEX; // throw a literal char* string

// throw an enum value

throw "Can not take square root of negative number"; throw dX; // throw a double variable that was previously defined throw MyException("Fatal Error"); // Throw an object of class MyException Each of these statements acts a signal that some kind of problem that needs to be handled has occurred Example // for sqrt() function #include "math.h" using namespace std; int main() { cout << "Enter a number: "; double dX; cin >> dX; try { // If the user entered a negative number, this is an error condition if (dX < 0.0) throw "Can not take sqrt of negative number"; // throw exception of type char * // Otherwise, print the answer cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl; } catch (char* strException) { cerr << "Error: " << strException << endl; } } // catch exceptions of type char* // Look for exceptions that occur within try block and route to attached catch block(s)

In this code, the user is asked to enter a number. If they enter a positive number, the if statement does not execute, no exception is thrown, and the square root of the number is printed. Because no exception is thrown in this case, the code inside the catch block never executes. The result is something like this: Enter a number: 9 The sqrt of 9 is 3 If the user enters a negative number, we throw an exception of type char*. Because were within a try block and a matching exception handler is found, control immediately transfers to the char* exception handler. The result is: Enter a number: -4 Error: Can not take sqrt of negative number

Вам также может понравиться