Sei sulla pagina 1di 9

Winter 2011

Master of Computer Application (MCA) Semester 2 MC0066 OOPS using C++


1. With the help of suitable programming examples, explain Selection control statements and Iteration statements in C++. Ans: 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; } e.g : # include <iostream.h> # include <conio.h> void main() { int num; cout<<Please enter a number<<endl;

cin>>num; if ((num%2) == 0) cout<<num << is a even number; else cout<<num << is a odd number; getch(); } 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; } e.g: include<iostream.h> void main()

{ char pos; int x=15, y=15; cout << you are currently located at <<x<< <<y<<endl; cout>>please choose the letter to move l for left, r for right, u for up and d for down <<endl; cin>>pos; switch (pos) { case l: x; break; case r: x++; break; case u: y++; break; case d: y; break; default: cout<<You selected a wrong option; } cout<< you are now located at <<x<< <<y; } 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; } e.g: # include <iostream.h> void main() { int n=0,a; int sum=0; cout<< enter five numbers; while (n<5) { cin>>a; sum=sum+a; n++; } cout<<Average of the numbers is<<(sum/n); } 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. Syntax:

do { Statement1; Statement2 } while (condition expression); e.g. include <iostream.h> void main() { int n=0,a; int sum=0; cout<< enter five numbers; do { cin>>a; sum=sum+a; n++; }while (n<5); cout<<Average of the numbers is<<(sum/n); }
For loop

The for loop is one of the popular control statement as it is compact and clear in specification. The loop initialization, loop termination condition statement and statement for the next iteration are all included in one statement. Syntax:

for(initialization statement;loop termination condition;statement to increment/decrement the loop variable) { Statement1; Statement2; } e.g. # include <iostream.h> void main() { int a; int sum=0; cout<< enter five numbers; for(int n=0;n<5;n++) { cin>>a; sum=sum+a; } cout<<Average of the numbers is<<(sum/n); }
2. Write your own C++ functions for the following problems: o Sort a book list in a library based on the discipline o Print the sorted output on the console Ans:

3. Explain the concepts and applications of multiple inheritance and virtual functions in C++. Ans: Multiple Inheritance: 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 parent names have to be specified while defining the class. We will discuss multiple inheritance in detail in the next unit. In the above example we have created a class employee and a student. Manager class is defined from both of these classes. This is useful in instances when you want to create an employee whose educational qualifications need not be stored such as a worker. Virtual inheritance: It Is a topic of object-oriented programming. It is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes a common direct base for the derived class and any other class that derives from it. In other words, if class A is virtually derived from class V, and class B is derived (directly or indirectly) from A, then V becomes a direct base class of class B and any other class derived from A. The best-known language that implements this feature is C++. This feature is most useful for multiple inheritance, as it makes the virtual base a common sub object for the deriving class and all classes that are derived from it. This can be used to avoid the problem of ambiguous hierarchy composition (known as the "diamond problem") by clarifying ambiguity over which ancestor class to use, as from the perspective of the deriving class (B in the example above) the virtual base (V) acts as though it were the direct base class of B, not a class derived indirectly through its base (A). It is used when inheritance represents restriction of a set rather than composition of parts. In C++, a base class intended to be common throughout the hierarchy is denoted as virtual with the virtual keyword. 4. Describe the theoretical concepts of Binary files and Manipulators with relevant programming examples. Ans: Binary Files in C++ A binary file is a file of any length that holds bytes with values in the range 0 to 0xff. (0 to 255). These bytes have no other meaning. In a text file a value of 13 means carriage return, 10 means line feed, 26 means end of file. Software reading or writing

text files has to deal with line ends. In Linux these are just separated by line feeds but Windows uses carriage returns and line feeds. In modern terms we can call a binary file a stream of bytes and more modern languages tend to work with streams rather than files. The important part is the data rather than where it came from! This example shows that you can write text to a binary file. Example: RandomAccess ra( filename) ; if ( ra.OpenWrite() ) { if (!ra.Write( mytext )) cout << "Failed to write to file " << filename << endl; ra.Close() ; } else cout << "Failed to open " << filename << " fior writing " << endl; This uses the class RandomAccess to open a binary file for writing, then writes a string into it. The RandomAccess class uses a FILE to do the main work. It's opened in "wb " mode (refer to the C tutorial for more information on that) and then writes the text to the file. It's actually writing sequentially though it could be made to write anywhere in the file. However, because the "wb" mode creates the file (or deletes all content of an existing file) there isn't much point. If you try to move the file pointer to a place in the file that doesn't exist, the results aren't defined. It might create a file to fill in the gaps or it might just not work. That depends upon the operating system so if you want to write software that is portable don't use it!

Manipulators in C++ Programs There is another manipulator setw in <b>C++ programs</b> to display values properly. This manipulator is available in iomanip.h header file. Values are by default displayed from the upper left corner (This is called left justified display). If we want to display out values with specified space, then we have to use setw manipulator. setw manipulator takes an integer value and will create imaginary field according to the

integer value. Each box will contain one character and the values will be displayed in right justified order. Lets see the working of setw manipulator with <b>C++ program</b>. #include <iostream.h> #include <conio.h> #include <iomanip.h> void main () { clrscr (); cout<< setw (10)<<"12,235"<< endl; cout<< setw (10)<<"1,000"<< endl; cout<< setw (10)<< "2,35,000"; getch (); }

Potrebbero piacerti anche