MB0053-Winter Drive-Assignment-2011 - International Business Management - Set 1
MC0066 Winter Drive 2011
-
Upload
prakash-mandal -
Category
Documents
-
view
218 -
download
0
Transcript of MC0066 Winter Drive 2011
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 1/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
Winter 2011
Master of Computer Application (MCA) – Semester 2MC0066 – OOPS using C++ – 4 Credits
(Book ID: B0681 & B0715)Assignment Set – 1 (40 Marks)
Answer all Questions Each Question carries TEN Marks
Book ID: B 0681
1. 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 tomodify 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 inC++. 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
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 2/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
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 executed, 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()
{
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();
}
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 ornot.
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 3/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
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;
}
If the variable in the switch statement is equal to value1 then statement1 is executed, if it is equalto value2 then statement2 is executed, if it is value3 then statement3 is executed. If the variable
value is not in any of the cases listed then the default case statement or statement4 is executed.The default case specification is optional, however keeping it is a good practice. It can also be
used for displaying any error message. Each case can have any number of statements. Howeverevery case should have a break statement as the last statement. Break statement takes the control
out of the switch statement. The absence of the break statement can cause execution of
statements in the next case. No break is necessary for the last case. In the above example, defaultcase does not contain a break statement.
The following program implements the switch statement
position.cpp
# include<iostream.h>
void main()
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 4/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
{ 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;
}
Conditional Operator
Conditional operator (?:) is a handy operator which acts like a shortcut for if else statement. If you had to compare two variables a and b and then depending on which is larger, you wanted to
store that variable in another variable called large. You would do this using if else statement in
the following way:
if (a>b)
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 5/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
large=a;
else
large=b;
The above can be done using conditional operator in the following way:
large= (a>b) ? a : b ;
Iteration statements in C++
Iteration or loops are important statements in C++ which helps to accomplish repeatitiveexecution 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
statement1 and statement2 are 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 loopso 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 iniatialised to avoid
the variable taking junk value and create bugs in the program.
The following program implements the while loop
// average1.cpp
# include <iostream.h>
void main()
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 6/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
{
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);
}
The above program accepts five numbers from the user and finds the average of the five
numbers. In the above while loop, the variable n is initialized to zero and this variable keeps
track of the count of numbers input from the user. It is incremented every time a number is input
from the user. The number accepted from the user is added to the value stored in the sumvariable. When n becomes 5 the while loop condition becomes false and the average of the
numbers is printed. Please note that there is no semicolon after while and condition expression.
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 at least once. However, in while loop, since the condition is checked before, thestatements inside the loop will not be executed if the loop condition is false.
Syntax:
do
{
Statement1;
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 7/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
Statement2
} while (condition expression);
In the above example the statement1 and statement2 are executed and the condition is checked. If
the condition is true then the statements are executed again and if the condition is false then thecontrol is transferred to the next statement after the do..while statement.
Please note that there is no semicolon after do, but there is a semicolon after the conditionexpression in while part. The average1.cpp program is rewritten using do..while loop as follows
// average2.cpp
# 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);
}
The decision on whether to use while or do..while statement depends on whether the statementsinside the loop have to be executed atleast once or not. If it has to be executed atleast once, then
the do..while statement should be used.
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 8/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
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 iterationare all included in one statement.
Syntax:
for(initialization statement; loop termination condition; statement to increment/decrement theloop variable)
{
Statement1;
Statement2;
}
In the above example, initiation statement is executed first and then the termination is checked. If the condition is true, statement1 and statement2 will be executed. Then the third statement in the
for loop is executed which modifies the loop control variable.
Please note that there is no semicolon after the for statement.
The following program demonstrates the use of for loop to find the factorial of a number.
// factorial.cpp
# include <iostream.h>
void main()
{
int number,fact=1;
cout<< “enter a number”;
cin>>number;
for(int i=number;i>1;i–)
fact=fact*i;
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 9/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
cout<<”Factorial of <<number<<“is”<<fact;
}
2. Write your own C++ functions for the following problems:
Sort a book list in a library based on the discipline
Print the sorted output on the console
Ans:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class CBook {
private:
string strTitle;
string strAuthor;
string strDiscipline;
public:CBook(const string& strPTitle, const string& strPAuthor, const string& strPDiscipline) :
strTitle(strPTitle), strAuthor(strPAuthor), strDiscipline(strPDiscipline)
{
}
const string& get_title() const {return strTitle;}
const string& get_author() const {return strAuthor;}
const string& get_discipline() const {return strDiscipline;}
static bool compare_disciplineDSC( const CBook& s1, const CBook& s2 ) {
return s1 . get_discipline() . compare(s2 . get_discipline()) > 0 ? true : false;
}
static bool compare_disciplineASC( const CBook& s1, const CBook& s2) {
return s1 . get_discipline() . compare(s2 . get_discipline() ) < 0 ? true : false;
}
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 10/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
friend int sort_discipline(const CBook &a, const CBook &b)
{
return a.strDiscipline.compare(b.strDiscipline) < 0 ? 1 : 0;
}
friend ostream& operator<<(ostream& os, const CBook& book)
{
os << "\n Title: " << book.strTitle <<
"\n Author: " << book.strAuthor <<
"\nDiscipline: " << book.strDiscipline;
return os;
}
};
class CBookList : public vector<CBook>
{public:
friend ostream& operator<<(ostream& os, const CBookList& bl)
{
copy(bl.begin(), bl.end(), ostream_iterator<CBook>(os, "\n"));
return os;
}
};
int main()
{CBookList a;
string strA[3];
a.clear();
for (int i = 0; i < 10; i++) {
strA[0] = " Title"; strA[0] += 'A' + i;
strA[1] = " Author"; strA[1] += 'A' + i;
strA[2] = "Discipline"; strA[2] += 'A' + i;
a.push_back(CBook(strA[0], strA[1], strA[2]));
}
cout << "Creation Order\n" << a << endl;
sort(a.begin(), a.end(), CBook::compare_disciplineDSC);
cout << "\nSorted descending\n" << a << endl;
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 11/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
return 0;
}
OutPut:
Creation Order
Title: TitleA
Author: AuthorA
Discipline: DisciplineA
Title: TitleB
Author: AuthorB
Discipline: DisciplineB
Title: TitleC
Author: AuthorC
Discipline: DisciplineC
Title: TitleD
Author: AuthorD
Discipline: DisciplineD
Title: TitleE
Author: AuthorE
Discipline: DisciplineE
Title: TitleF
Author: AuthorF
Discipline: DisciplineF
Title: TitleG
Author: AuthorG
Discipline: DisciplineG
Title: TitleH
Author: AuthorH
Discipline: DisciplineH
Title: TitleI
Author: AuthorI
Discipline: DisciplineI
Title: TitleJ
Author: AuthorJ
Discipline: DisciplineJ
Sorted descending
Title: TitleJ
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 12/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
Author: AuthorJ
Discipline: DisciplineJ
Title: TitleI
Author: AuthorI
Discipline: DisciplineI
Title: TitleH
Author: AuthorH
Discipline: DisciplineH
Title: TitleG
Author: AuthorG
Discipline: DisciplineG
Title: TitleF
Author: AuthorF
Discipline: DisciplineF
Title: TitleE
Author: AuthorEDiscipline: DisciplineE
Title: TitleD
Author: AuthorD
Discipline: DisciplineD
Title: TitleC
Author: AuthorC
Discipline: DisciplineC
Title: TitleB
Author: AuthorB
Discipline: DisciplineB
Title: TitleA
Author: AuthorA
Discipline: DisciplineA
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 13/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
Book ID: B0715
3. Explain the concepts and applications of multiple inheritance and virtual
functions in C++.
Ans:
Multiple Inheritance
We 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. Anarrow 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.
A direct base class cannot appear in the base list of a derived class more than once:
class B1 { /* … */ }; // direct base class
class D : public B1, private B1 { /* … */ }; // error
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 14/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
However, a derived class can inherit an indirect base class more than once, as shown in the
following example:
class L { /* … */ }; // indirect base class
class B2 : public L { /* … */ };
class B3 : public L { /* … */ };
class D : public B2, public B3 { /* … */ }; // valid
In the above example, class D inherits the indirect base class L once through class B2 and once
through class B3. However, this may lead to ambiguities because two subobjects of class L exist,
and both are accessible through class D. We can avoid this ambiguity by referring to class L usinga qualified class name. For example:
B2::L
or
B3::L.
We can also avoid this ambiguity by using the base specifier virtual to declare a base class.
Virtual Functions
By default, C++ matches a function call with the correct function definition at compile time. This
is called static binding. We can specify that the compiler match a function call with the correct
function definition at run time; this is called dynamic binding. We declare a function with the
keyword virtual if we want the compiler to use dynamic binding for that specific function.
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 15/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
A virtual function is a member function we may redefine for other derived classes, and can
ensure that the compiler will call the redefined virtual function for an object of the correspondingderived class, even if we call that function with a pointer or reference to a base class of the
object.
A class that declares or inherits a virtual function is called a polymorphic class.
We redefine a virtual member function, like any member function, in any derived class. Suppose
we declare a virtual function named f in a class A, and we derive directly or indirectly from A a
class named B. If we declare a function named f in class B with the same name and same
parameter list as A::f, then B::f is also virtual (regardless whether or not we declare B::f with
the virtual keyword) and it overrides A::f. However, if the parameter lists of A::f and B::f
are different, A::f and B::f are considered different, B::f does not override A::f, and B::f is
not virtual (unless we have declared it with the virtual keyword). Instead B::f hides A::f. The
following example demonstrates this:
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout << "Class A" << endl; }
};
struct B: A {
void f(int) { cout << "Class B" << endl; }
};
struct C: B {
void f() { cout << "Class C" << endl; }
};
int main() {
B b; C c;
A* pa1 = &b;
A* pa2 = &c;
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 16/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
// b.f();
pa1->f();
pa2->f();
}
The following is the output of the above example:
Class A
Class C
The function B::f is not virtual. It hides A::f. Thus the compiler will not allow the function call
b.f(). The function C::f is virtual; it overrides A::f even though A::f is not visible in C.
If we declare a base class destructor as virtual, a derived class destructor will override that base
class destructor, even though destructors are not inherited.
The return type of an overriding virtual function may differ from the return type of the
overridden virtual function. This overriding function would then be called a covariant virtual
function. Suppose that B::f overrides the virtual function A::f. The return types of A::f and
B::f may differ if all the following conditions are met:
· The function B::f returns a reference or pointer to a class of type T, and A::f returns a pointer
or a reference to an unambiguous direct or indirect base class of T.
· The const or volatile qualification of the pointer or reference returned by B::f has the same or
less const or volatile qualification of the pointer or reference returned by A::f.
· The return type of B::f must be complete at the point of declaration of B::f, or it can be of
type B.
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. We can declare a virtual function to be a friend of another class.
If a function is declared virtual in its base class, we can still access it directly using the scope
resolution (::) operator. In this case, the virtual function call mechanism is suppressed and thefunction implementation defined in the base class is used. In addition, if we 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.
8/2/2019 MC0066 Winter Drive 2011
http://slidepdf.com/reader/full/mc0066-winter-drive-2011 17/17
MC0066 – OOPS using C++ Roll No. XXXXXXXXX
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 relevantprogramming examples.
Ans:
Remaining answers are available in the full assignments (in MS-WORD format).
For full assignments Contact us:
Prakash: 9686515230
Email: [email protected] / [email protected]
Website: www.assignmentsclub.com
Note: Sample papers are available in Portable Docum ent Format (.pd f) with a
watermark of our Website. Full assignments will be in MS-WORD (.doc) format
without any watermark... Contact us for Full assignments...