Sie sind auf Seite 1von 5

Summer 2012 Master of Computer Application (MCA) Semester 2 MC0066 OOPS using C++ 4 Credits (Book ID: B0681

681 & B0715) Assignment Set 1 (40 Marks)

Answer all Questions 1.

Each Question carries TEN Marks

With the help of suitable programming examples, explain Selection control statements and Iteration statements in C++. Ans:Selection control statements in C++ There are basically two types of control statements in c++ which allows the programmer to modify the regular sequential execution of statements.They are selection and iteration statements. The selection statements allow to choose a set of statements for execution depending on a condition. If statement and switch statement are two statements which allow selection in c++. There is also an operator known as conditional operator which enables selection. If statement Syntax : if (expression or condition) { statement 1; statement 2; } else { statement 3; statement 4; } The expression or condition is any expression built using relational operators which either yields true or false condition. If no relational operators are used for comparison, then the expression will be evaluated and zero is taken as false and non zero value is taken as true. If the condition is true, statement1 and statement2 is executed otherwise statement 3 and statement 4 is executed. Else part in the if statement is optional. If there is no else part, then the next statement after the if statement is exceuted, if the condition is false. If there is only one statement to be executed in the if part or in the else part, braces can be omitted. Following example program implements the if statement. // evenodd.cpp # include <iostream.h> # include <conio.h> void main() { intnum; cout<<Please enter a number<<endl; cin>>num; if ((num%2) == 0) cout<<num<< is a even number; 1

else cout<<num<< is a odd number; getch(); } The above program accepts a number from the user and divides it by 2 and if the remainder (remainder is obtained by modulus operator) is zero, it displays the number is even, otherwise as odd. We make use of the relational operator == to compare whether remainder is equal to zero or not.

Switch statement Nested ifs can be confusing if the if statement is deeply nested. One alternative to nested if is the switch statement which can be used to increase clarity in case of checking the different values of the same variable and execute statements accordingly. Syntax : Switch (variablename) { case value1: statement1; break; case value2: statement2; break; case value3: statement3; break; default: statement4; } Iteration statements in C++ Iteration or loops are important statements in c++ which helps to accomplish repeatitive execution of programming statements. There are three loop statements in C++ : while loop, do while loop and for loop While loop Syntax: while (condition expression) { Statement1; Statement 2; } Do..while loop The do while loop is same as while loop except that the condition is checked after the execution of statements in the do..while loop. Hence in do..while loop, statements inside the loop are executed atleast once. However, in while loop, since the condition is checked before, the statements inside the loop will not be executed if the loop condition is false. Do..while Statement Please note that there is no semicolon after do, but there is a semicolon after the condition expression in while part. 2. Write your own C++ functions for the following problems: Sort a book list in a library based on the discipline 2

Print the sorted output on the console //seqsearch.cpp # include<iostream.h> void main() { int a[100], num; int i, flag=0; cout<<please enter the numbers for the array; for( i=0;i<100;i++) { cin>>a[i]; } cout<<Enter the number to be searched; cin>>num; for(i=0;i<100;i++) { if (a[i]==num) { cout<<num<< is found; flag=1; break; }} if (flag==0) cout<<num<< not found; }

3.

Explain the concepts and applications of multiple inheritance and virtual functions in C++. Multiple inheritance allows you to create a derived class that inherits properties from more than one base class. Because a derived class inherits members from all its base classes, ambiguities can result. For example, if two base classes have a member with the same name, the derived class cannot implicitly differentiate between the two members. Note that, when you are using multiple inheritance, the access to names of base classes may be ambiguous. See Multiple inheritance section for more detailed information. You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. In the following example, classes A, B, and C are direct base classes for the derived class X: class A { /* */ }; class B { /* */ }; class C { /* */ }; class X : public A, private B, public C { /* */ }; The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow: The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors. Virtual Functions 3

By default, C++ matches a function call with the correct function definition at compile time. This is called static binding. You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding. You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function. A virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object. A virtual function cannot be global or static because, by definition, a virtual function is a member function of a base class and relies on a specific object to determine which implementation of the function is called. You can declare a virtual function to be a friend of another class. If a function is declared virtual in its base class, you can still access it directly using the scope resolution (::) operator. In this case, the virtual function call mechanism is suppressed and the function implementation defined in the base class is used. In addition, if you do not override a virtual member function in a derived class, a call to that function uses the function implementation defined in the base class. A virtual function must be one of the following: Defined Declared pure Defined and declared pure A base class containing one or more pure virtual member functions is called an abstract class.

4. Describe the theoretical concepts of Binary files and Manipulators with relevant programming examples. Binary Files Although, the files with formatted text (all that were talked about so far) are very useful, sometimes you may need to work with unformatted files, i.e., binary files. They have the same look as your program itself, and it is much different from what comes after using the << and >> operators. The functions that give you the possibility to write/read unformatted files are get() and put(). To read a byte, you can use get() and to write a byte, use put(). get() and put() both take one parameter, a char variable or character. If you want to read/write whole blocks of data, then you can use the read() and write() functions. Their prototypes are: istream&read(char *buf, streamsizenum); ostream&write(const char *buf, streamsizenum); For the read() function, buf should be an array of chars, where the read block of data will be put. For the write() function, buf is an array of chars, where is the data you want to save in the file. For the both functions, num is a number, that defines the amount of data (in symbols) to be read/written. If you reach the end of the file, before you have read num symbols, you can see how many symbols were read by calling the function gcount(). This function returns the number of read symbols for the last unformatted input operation. And, before going to the code examples, we must take note that if you want to open a file for binary read/write, you should pass ios::binary as an open mode. Manipulators 4

A manipulator is an identifier that can be inserted into an output stream or extracted from an input stream in order to produce a desired effect. For example, endl is a commonly-used manipulator which inserts a newline into an output stream and flushes it. Therefore, cout<< 10 <<endl; has the same effect as: cout<< 10 << n; In general, most formatting operations are more easily expressed using manipulators than using setf. For example, cout<<oct<< 10 <<endl; is an easier way of saying: cout.setf(ios::oct, ios::basefield); cout<< 10 <<endl; Some manipulators also take parameters. For example, the setw manipulator is used to set the field width of the next IO object: cout<<setw(8) << 10; // sets the width of 10 to 8 characters The manipulators available have already been summarized in Section 2.2.3. Let us now look at some of the examples. Example 1: A simple output manipulator #include <iostream> #include <iomanip> using namespace std; ostream&sethex(ostream&stream) { stream.setf(ios::showbase); stream.setf(ios::hex, ios::basefield); return stream; } int main() { cout<< 256 << " " <<sethex<< 256; return 0; } Output: 256 0100"

Das könnte Ihnen auch gefallen