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

Name : Junaid Ahmed Siddiqui Roll Number : 571124436 Centre Code: 00229 Master of Computer Application (MCA) Semester

r 2 MC0067 OOPS using C++ Assignment Set 1 Answer all Questions Each Question carries FIVE Marks Book ID: B0681 & B0715

1.With the help of suitable programming examples, Explain Selection Control Statements and Iteration Statements in C++. ANS: Selection Control Statement The simplest and most common selection structure is the if statement which is written in a statement of the form: If (Boolean - expression ) statement; The if statement tests for a particular condition (expressed as a Boolean expression) and only executes the following statement(s) if the condition is true. An example follows of a fragment of a program which tests if the denominator is not zero before attempting to calculate fraction. If(total != 0) fraction = counter/total; The IF/ELSE selection control statement Often it is desirable for a program to take one branch if the condition is true and another if it is false. This can be done by using an if/else selection statement: if( boolean-expression ) statement-1; else

statement-2; Again, if a sequence of statements is to be executed, this is done by making a compound statement by using braces to enclose the sequence: if( boolean-expression ) { statements; } else { statements; } Again, if a sequence of statements is to be executed, this is done by making a compound statement by using braces to enclose the sequence: if( boolean-expression ) { statements; } else { statements; } ELSE IF multiple selection statement Occasionally a decision has to be made on the value of a variable which has more than two possibilites. This can be done by placing if statements within other if-else constructions. This is commonly known as nesting and a different style of indentation is used to make the multiple-selection functionality much clearer. This is given below: if( boolean-expression-1 )

statement-1; else if( boolean-expression-2 ) statement-2; else statement-N; For compound statement blocks, braces must be used. SWITCH selection control statement Instead of using multiple if/else statements C++ also provides a special control structure, switch. For a variable x the switch(x) statement tests whether x is equal to the constant values x1, x2, x3, etc. and takes appropriate action. The default option is the action to be taken if the variable does not have any of the values listed. switch( x ) { case x1: statements1; break; case x2: statements2; break; case x3: statements3; break; default: statements4; break; } The WHILE repetition control statement

Repetition control statements allow the programmer to specify actions which are to be repeated while some condition is true. In the while repetition control structure: while( boolean-expression ) { statements; } Increment and decrement operators Increasing and decreasing the value of an integer variable is a commonly used method for counting the number of times a loop is executed. C++ provides a special operator ++ to increase the value of a variable by 1. The following are equivalent ways of incrementing a counter variable by 1. count = count + 1; count++; The operator -- decreases the value of a variable by 1. The following are both decrementing the counter variable by 1. count = count - 1; count--; The FOR repetition control statement Often in programs we know how many times we will need to repeat a loop. A while loop could be used for this purpose by setting up a starting condition; checking that a condition is true and then incrementing or decrementing a counter within the body of the loop. For example we can adapt the while loop in AddWhilePositive.cc so that it executes the loop N times and hence sums the N numbers typed in at the keyboard. int i=0; // initialise counter

while(i<N) // test whether counter is still less than N { cin >> number; total = total + number; i++; } // increment counter

Iteration Statement Iteration or loops are important statements in C++ which helps to accomplish repetitive 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;} In the above example, condition expression is evaluated and if the condition is true, then the statement1and statement2 is executed. After execution, the condition is checked again. If true, the statements inside the while loop are executed again. This continues until the loop condition becomes false. Hence the variable used in the loop condition should be modified inside the loop so that the loop termination condition is reached. Otherwise the loop will be executed infinitely and the program will hang when executed. Also the loop variable should be initialized to avoid the variable taking junk value and create bugs in the program. #include <iostream> using namespace std; int main () { int Entry; int Counter; cout << "Enter a positive integer: "; cin >> Entry; Counter = 1; while(Counter <= Entry) { cout << "Iteration " << Counter << " of " << Entry<< "\n"; Counter = Counter + 1; } return 0; } 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 (); }

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