Object Oriented Programming with C++
Polymorphism
Evaluation
PART II
Answer to all the questions (2 Marks):
1. What is function overloading?
Answer: The ability of the function to process the message or data in
more than one form is called as function overloading.
2. List the operators that cannot be overloaded.
Answer: Operator that are not overloaded are follows
(i) scope operator::
(ii) sizeof
(iii) member selector.
(iv) member pointer selector.*
(v) ternary operator ?:
3. class add{int x; public: add(int)}; Write an outline definition for the constructor.
Answer: add :: add(int y)
{
y=x;
}
4. Does the return type of a function help in overloading a function?
Answer: No.
The return type of a function do not help in overloading a function.
5. What is the use of overloading a function?
Answer: Function overloading used in implementing polymorphism and also
reduces the number of comparisons in a program and makes the program to execute
faster. It also helps the programmer by reducing the number of function names
to be remembered.
PART III
Answer to all the questions (3 Marks):
1. What are the rules for function overloading?
Answer: Rules for
function overloading :
(i) The overloaded function must differ in the number of its
arguments or data types.
(ii) The return type of overloaded functions are not considered
for overloading same data type.
(iii) The default arguments of overloaded functions are not
considered as part of the parameter list in function overloading.
2. How does a compiler decide as to which function should be invoked when there are many functions? Give an example.
Answer: When an overloaded function called the compiler determines the
most appropriate definition to use, by comparing the argument types used to
call the function with the parameter types specified in the definitions.
Example : float area (float radius);
float area (float half, float base, float height);
float area (float length, float breadth);
3. What is operator overloading? Give some example of operators which can be overloaded.
Answer: The mechanism of giving special meaning to an operator is known
as operator overloading
Example : operators can be overloaded
+,++,−,− −, +=, −=, *, <,>
4. Discuss the benefit of constructor overloading ?
Answer: (i) Constructor overloading provides flexibility of creating
multiple type of objects for a class.
(ii) If there are multiple constructors present, argument to the
constructor should also be passed while creating an object.
5. class sale ( int cost, discount ;public: sale(sale &); Write a non inline definition for constructor specified;
Answer:
Sale :: sale (sale &s)
{
cost = s.cost;
discount = s.discount;
}
PART IV
Answer to all the questions (5 Marks):
1. What are the rules for operator overloading?
Answer: (i) Precedence and Associativity of an operator cannot be
changed.
(ii) No new operators can be created, only existing operators
can be overloaded.
(iii) Cannot redefine the meaning of an operator’s procedure.
You cannot change how integers are added. Only additional functions can be to
an operator
(iv) Overloaded operators cannot have default arguments.
(v) When binary operators are overloaded, the left hand object must
be an object of the relevant class.
2. Answer the question (i) to (v) after going through the following class.
classBook
{
intBookCode ; char Bookname[20];float fees;
public:
Book( ) //Function 1
{
fees=1000;
BookCode=1;
strcpy (Bookname,"C++");
}
void display(float C) //Function 2
{
cout<<BookCode<<":"<<Bookname<<":"<<fees<<endl;
}
~Book( ) //Function 3
{
cout<<"End of Book Object"<<endl;
}
Book (intSC,char S[ ],float F) ; //Function 4
};
(i) In the above program, what are Function 1 and Function 4 combined together referred as?
(ii) Which concept is illustrated by Function3? When is this function called/ invoked?
(iii) What is the use of Function3?
(iv) Write the statements in main to invoke function1 and function2
(v) Write the definition for Function 4 .
Answer:
(i) Function 1 - non parameterized constructor definition
Function 4 - Parameterized non-line constructor declaration
(ii) Destructor - When instance of a class goes out of scope.
This function gets invoked/called.
(iii) It destroys the memory allocated by the constructor
(iv) int main ()
{
Book BI;
Bl.Book();
Bl.display(10.5);
}
(v) Book :: Book(int Sc, CharS[], float F)
{
Bookcode = sc;
strcpy(Bookname,s);
fees= F;
}
3. Write the output of the following program
include<iostream>
using namespace std;
class Seminar
{
int Time;
public:
Seminar()
{
Time=30;cout<<"Seminar starts now"<<endl;
}
void Lecture()
{
cout<<"Lectures in the seminar on"<<endl;
}
Seminar(int Duration)
{
Time=Duration;cout<<"Welcome to Seminar "<<endl;
}
Seminar(Seminar &D)
{
Time=D.Time;cout<<"Recap of Previous Seminar Content "<<endl;
}
~Seminar()
{
cout<<"Vote of thanks"<<endl;
}
};
int main()
{
Seminar s1,s2(2),s3(s2);
s1.Lecture();
return 0;
}
Answer:
Output:
Seminar Starts now
Welcome to Seminar
Recap of Previous Seminar Content
Lectures in the Seminar on
Vote of thanks
4. Debug the following program
#include<iostream>
using namespace std;
class String
{
public:
charstr[20];
public:
void accept_string
{
cout<<"\n Enter String : ";
cin>>str;
}
display_string()
{
cout<<str;
}
String operator *(String x) //Concatenating String
{
String s;
strcat(str,str);
strcpy(s.str,str);
goto s;
}
}
int main()
{
String str1, str2, str3;
str1.accept_string();
str2.accept_string();
cout<<"\n\n First String is : ";
str1=display_string();
cout<<"\n\n Second String is : ";
str2.display_string();
str3=str1+str2;
cout>>"\n\n Concatenated String is : ";
str3.display_string();
return 0;
}
Answer:
5. Answer the questions based on the following program
#include<iostream>
#include<string.h>
using namespace std;
class comp {
public:
chars[10];
void getstring(char str[10])
{
strcpy(s,str);
}
void operator==(comp);
};
void comp::operator==(comp ob)
{
if(strcmp(s,ob.s)==0)
cout<<"\nStrings are Equal";
else
cout<<"\nStrings are not Equal";
}
int main()
{
comp ob, ob1;
char string1[10], string2[10];
cout<<"Enter First String:";
cin>>string1;
ob.getstring(string1);
cout<<"\nEnter Second String:";
cin>>string2;
ob1.getstring(string2);
ob==ob1;
return 0;
}
(i) Mention the objects which will have the scope till the end of the program.
(ii) Name the object which gets destroyed in between the program
(iii)Name the operator which is over loaded and write the statement that invokes it.
(iv) Write out the prototype of the overloaded member function
(v)What types of operands are used for the overloaded operator?
(vi) Which constructor will get executed? Write the output of the program
Answer:
(i) ob, ob1;
(ii) ob
(iii) ==, ob==obl;
(iv) void operator ==(comp);
(v) string
(vi) default constructor will get executed
Output:
Enter First String : TEXT
Enter Second String : BOOK
Strings are not Equal
CASE STUDY
Suppose you have a Kitty Bank with an initial amount of Rs500 and you have to add some more amount to it. Create a class 'Deposit' with a data member named 'amount' with an initial value of Rs500. Now make three constructors of this class as follows:
1. without any parameter - no amount will be added to the Kitty Bank
2. having a parameter which is the amount that will be added to the Kitty Bank
3. whenever amount is added an additional equaly amount will be deposited automatically Create an object of the 'Deposit’ and display the final amount in the Kitty Bank.
Answer:
#include<iostream>
Class Deposit
{
int amount;
Deposit ( )
{
amount = 0;
}
Deposit (int d)
}
amount = dP;
Deposit (Deposit @ d)
}
amount = d.amount;
}
void display ()
{
amount = amount + 500;
cout <<
"Amout" << amout;
}
};
int (main C)
Deposit D1, D2(D1), D3(2000);
DI .display ( );
D2. display ( );
D3. display ( );
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.