OOP PT-2 QUESTION BANK
CHAPTER 4: POINTER AND POLYMORPHISM
Q 1)Differentiate between run time and compile time polymorphism.
Ans)
Compile time Polymorphism Run time Polymorphism
It means that an object is bound to its function It means that selection of appropriate function is
call at compile time i.e. linking of function call done at runtime i.e. linking of function call to its
to its definition at compile time. definition at run time.
Functions to be called are known well before Function to be called is unknown until
appropriate selection is made.
This does not require use of pointers to objects This requires use of pointers to object
Function calls are faster Function calls execution are slower
It is also referred as early binding or static It is also referred as late binding or dynamic binding.
binding.
e.g. overloaded function call e.g. virtual function
It is implemented by function overloading It is implemented by virtual functions.
or operator overloading
Q 2) Give meaning of following statements:
int *ptr, a = 5;
ptr = & a;
cout<< * ptr;
cout<< (* ptr) + 1;
int *ptr, a = 5;
Ans)
int *ptr, a = 5;
Declare pointer variable ptr and variable a with initial value 5.
ptr = & a;
initialize pointer variable with address of variable a (store address of each variable a in ptr)
cout<< * ptr;
Displays value of a i.e. value at address stored inside ptr. It displays value 5.
cout<< (* ptr) + 1;
Displays value by adding 1 to the value at address stored inside ptr. It displays value 6
Q 3)Write any six rules of operator overloading.
Ans)
Rules for overloading operators:
1.Only existing operators can be overloaded. New operators cannot be created.
2.The overloaded operator must have at least one operand that is of user defined data type.
3.We can’t change the basic meaning of an operator. That is to say, we can’t redefine the plus(+)
operator to subtract one value from other.
4.Overloaded operators follow the syntax rules of the original operators. They can’t be
overridden.
5.There are some operators that can’t be overloaded.
6.We can’t use friend functions to overload certain operators. However, member function scan
be used to overload them.
7.Unary operators overloaded by means of member function take no explicit arguments and
return no explicit values, but, those overloaded by means of the friend function, take one
reference argument (the object of the relevant class).
8.Binary operators overloaded through a member function, take one explicit argument and those
which are overloaded through a friend function take two explicit arguments.
9.When using binary operators overloaded through a member function, the left hand operand
must be an object of the relevant class.
10.Binary arithmetic operators such as +,-,* and / must explicitly returna value. They must not
attempt to change their own arguments.
Q 4)Write a program in C++ to overload unary ‘_’ operator to negate values of data
members of class.
Ans)
#include<iostream.h>
#include<conio.h>
class minus
{
int a,b,c;
public:
void getdata()
{
cout<<"Enter the values:"<<endl;
cout<<"a= ";
cin>>a;
cout<<"b= ";
cin>>b;
cout<<"c= ";
cin>>c;
}
void putdata()
{
cout<<"a= "<<a<<endl;
cout<<"b= "<<b<<endl;
cout<<"c= "<<c<<endl;
}
void operator-()
{
a=-a;
b=-b;
c=-c;
}
};
void main()
{
clrscr();
minus m;
m.getdata();
cout<<"\nbefore negation:"<<endl;
m.putdata();
-m;
cout<<"\nafter negation:"<<endl;
m.putdata();
getch();
}
Q 5)With suitable example, describe effect of ++ and - - operators used with pointer in
pointer arithmetic.
Ans)
++ Operator: - It is referred as increment operator that increments the value of variable. If ++
operator is used with pointer variable, then pointer variable points to next memory address
that means pointer increment with respect to size of the data type used to declare pointer ++
variable.
-- Operator: - It is referred as decrement operator that decrements the value of variable. If --
operator is used with pointer variable, then pointer variable points to previous memory
address that means pointer decrement with respect to size of the data type used to declare
pointer variable.
Program:
#include<iostream.h>
#include<conio.h>
void main( )
{
int num[5]={56,75,22,18,90};
int *ptr, i;
ptr=&num[0];
cout<<”value of ptr::”<<*ptr<<endl;
ptr++;
cout<<”value of ptr++::”<<*ptr<<endl;
ptr--;
cout<<”value of ptr--::”<<*ptr<<endl;
getch()
}
Q 6)Write a C++ program to interchange the values of two int, float and char using
function overloading
Ans)
class swapping
{
private:
int p,q;
double a,b;
char m,n;
public:
void swap(int p1, int q1)
{
p=p1;
q=q1;
cout<<"\n***before swap:***"<<endl;
cout<<"first integer number:"<<p<<endl;
cout<<"second integer number:"<<q<<endl;
int temp;
temp=p;
p=q;
q=temp;
cout<<"\n***after swap:***"<<endl;
cout<<"first integer number:"<<p<<endl;
cout<<"second integer number:"<<q<<endl;
}
void swap(double a1, double b1)
{
a=a1;
b=b1;
cout<<"\n***before swap:***"<<endl;
cout<<"first float number:"<<a<<endl;
cout<<"second float number:"<<b<<endl;
double temp;
temp=a;
a=b;
b=temp;
cout<<"\n***after swap:***"<<endl;
cout<<"first float number:"<<a<<endl;
cout<<"second float number:"<<b<<endl;
}
void swap(char m1, char n1)
{
m=m1;
n=n1;
cout<<"\n***before swap:***"<<endl;
cout<<"first char:"<<m<<endl;
cout<<"second char:"<<n<<endl;
char temp;
temp=m;
m=n;
n=temp;
cout<<"\n***after swap:***"<<endl;
cout<<"first char:"<<m<<endl;
cout<<"second char:"<<n<<endl;
}
};
void main()
{
swapping s;
clrscr();
s.swap(6,8);
s.swap(62.32,88.45);
s.swap('x','y');
getch();
}
Q 7)Write a C++ program to compare two strings using '==' operator overloading
Ans)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class str_cmp
{
private:
char str[20];
public:
void getdata()
{
cout<<"enter string:"<<endl;
cin>>str;
}
void operator==(str_cmp s3)
{
if(strcmp(str,s3.str)==0)
cout<<"string are same"<<endl;
else
cout<<"string are not same"<<endl;
}
};
void main()
{
str_cmp s1,s2;
clrscr();
s1.getdata();
s2.getdata();
s1==s2;
getch();
}
Q 8)Write a C++ program to overload binary operator ‘+’ to concatenate two strings.
Ans)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class opov
{
char str1[10]; public:
void getdata()
{
cout<<"\nEnter a strings"; cin>>str1;
}
void operator +(opov o)
{
cout<<strcat(str1,o.str1);
}
};
void main()
{
opov o1,o2; clrscr();
o1.getdata();
o2.getdata(); o1+o2;
getch();
}
Q 9)Write a C++ program to declare a class "Book" containing data members
book_name,author_name and price. Accept this information for one object of the class
using pointer to that object*/
Ans)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Book
{
private:
char book_name[20];
char author[20];
float price;
public:
void accept()
{
cout<<"enter details of book"<<endl;
cout<<"enter book name:";
cin>>book_name;
cout<<"enter author name:";
cin>>author;
cout<<"enter price:";
cin>>price;
}
void display()
{
cout<<"book name:"<<book_name<<endl;
cout<<"author name:"<<author<<endl;
cout<<"price:"<<price;
}
};
void main()
{
Book b,*bptr;
bptr=&b;
clrscr();
bptr->accept();
bptr->display();
getch();
}
Q 10) Write a C++ program to declare a class "polygon" having data members width and
height. Derive classes "rectangle" and "triangle" from "polygon" having area() as a
member function. Calculate area of triangle and rectangle using pointer to derived class
object
Ans)
#include<iostream.h>
#include<conio.h>
class polygon
{
public:
float width,height;
public:
void get()
{
cout<<"enter width of polygon:";
cin>>width;
cout<<"enter height of polygon:";
cin>>height;
}
virtual void area()
{
}
};
class rectangle:public polygon
{
private:
float ar_rect;
public:
void area()
{
ar_rect=width*height;
}
void display()
{
cout<<"area of rectangle is:"<<ar_rect<<endl;
}
};
class triangle:public polygon
{
private:
float ar_tr;
public:
void area()
{
ar_tr=(0.5*(width*height));
}
void display()
{
cout<<"area of triangle is:"<<ar_tr<<endl;
}
};
void main()
{
polygon p,*pptr;
rectangle r,*rptr;
triangle t,*tptr;
clrscr();
pptr=&r;
pptr->get();
pptr->area();
rptr=&r;
rptr->display();
pptr=&t;
pptr->get();
pptr->area();
tptr=&t;
tptr->display();
getch();
}
Q11)Write a C++ program to declare a class "Box" having data members
height,width and breadth. Accept this information for one object using
pointer to that object. Display this area and volume of that object
ANS)
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
class box
{
int a,area,volume;
public:
void getdata()
{
cout<<"\n Enter height: ";
cin>>a;
}
void calculate()
{
area=6*(a*a);
volume=(a*a*a);
}
void display()
{
cout<<"\n Area: "<<area;
cout<<"\n Volume: "<<volume;
}
};
void main()
{
clrscr();
box b,*ptr;
ptr=&b;
ptr->getdata();
(*ptr).calculate();
ptr->display();
getch();
}
Enter height: 2
Area: 24
Volume: 8
Q12) Write a C++ program to overload unary operator (++)increment and (--)decrement
ANS)
#include<iostream.h>
#include<conio.h>
class op_ov
{
int num;
public:
void getdata()
{
cout<<"enter number:";
cin>>num;
}
void operator++()
{
num=num+5;
cout<<"number after increment is:"<<num<<endl;
}
void operator--()
{
num=num-10;
cout<<"number after decrement is:"<<num<<endl;
}
};
void main()
{
op_ov o;
clrscr();
o.getdata();
++o;
o.getdata();
--o;
getch();
}
/*OUTPUT
enter number:50
number after increment is:55
enter number:80
number after decrement is:70
*/
Q 13) What are the rules for virtual function?
ANS)
Rules for virtual function:
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived class versions
must be identical.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. While a base pointer can point to any type of the derived object, the reverse is not true.
9. When a base pointer points to a derived class, incrementing or decrementing it will not make it
to point to the next object of the derived class.
10. If a virtual function is defined in the base class, it need not be necessarily redefined in the
derived class.
CHAPTER 5: FILE OPERATIONS
Q 1)Write the use of ios : : in and ios : : out.
Ans)
ios::in - It is used as file opening mode to specify open file reading only.
ios::out- It is used as file opening mode to specify open file writing only.
Q 2)Write a C++ program to count number of spaces in text file.
Ans)
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ifstream file;
int s=0;
char ch;
clrscr();
file.open("abc.txt");
while(file)
{
file.get(ch);
if(ch==' ')
{
s++;
}
}
cout<<"\nNumber of spaces in text file are:"<<s; getch();
}
Q 3)Write a C++ program to append data from abc.txt to xyz.txt file.
Ans)
Assuming input file as abc.txt with contents "World" and output file named as xyz.txt with
contents "Hello" have been already created.
#include <iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("abc.txt",ios::in);
ofstream fout;
fout.open("xyz.txt", ios::app);
if (!fin)
{
cout<< "file not found";
}
else
{
fout<<fin.rdbuf();
}
Cout<<”file appended successfully”;
Getch();
return 0;
}
Q 4)Write a C++ program to write ‘Welcome to poly’ in a file. Then read the data from file
and display it on screen.
Ans)
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char str[25] = "Welcome to poly",ch;
clrscr();
ofstream fout;
fout.open("output.txt");
fout<<str;
fout.close();
ifstream fin;
fin.open("output.txt");
while (!fin.eof())
{
fin.getline(str, 25);
cout<<str<<endl;
}
fin.close();
getch();
}
Q 5)Write a C++ program to merge two files
Ans)
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
ifstream ifs1, ifs2;
ofstream ofs;
char ch, fname1[20], fname2[20], fname3[30];
cout<<"Enter first file name(with extension like file1.txt):";
cin>>fname1;
cout<<"Enter second file name(with extension like file2.txt):";
cin>>fname2;
cout<<"Enter name of file (with extension like file3.txt) which will store the contents of the two
files (fname1 and fname1) : ";
cin>>fname3;
ifs1.open(fname1);
ifs2.open(fname2);
if(!ifs1 || ifs2==NULL)
{
cout<<"Error Message:no such file present\n";
cout<<"Press any key to exit...";
}
else
{
ofs.open(fname3);
ifs1.get(ch);
while(ifs1.eof()==0) //while(!ifs1.eof())
{
ofs.put(ch);
ifs1.get(ch);
}
ifs2.get(ch);
while(ifs2.eof()==0)
{
ofs.put(ch);
ifs2.get(ch);
}
cout<<"The two files were merged into "<<fname3<<" file successfully..!!";
}
ifs1.close();
ifs2.close();
ofs.close();
getch();
}
Q 6)Write a program to count the number of lines in file.
ANS)
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream file;
char ch;
int n=0;
clrscr();
file.open("abc.txt");
while(file)
{
file.get(ch);
if(ch=='\n')
n++;
}
cout<<"\n Number of lines in a file are:"<<n;
file.close();
getch();
}
Q7)Explain function for manipulation of file pointers with example
ANS)
1)seekg(): It places the get file pointer to the specified position in input mode of file.
e.g:
file.seekg(p,ios::beg); or
2)seekp(): It places the put file pointer to the specified position in output mode of file.
e.g:
file.seekp(-p,ios::end), or
3)tellg(): This function returns the current working position of the file pointer in the input mode.
e.g:
int p=file.tellg();
4)tellp(): This function returns the current working position of the file pointer in the output
mode.
e.g:
int p=file.tellp();