You are on page 1of 8

What is function overloading? Write a c++ program to implement a function overloading.

Answer
More than one user defined functions can have same name and perform different operations this feature of
c++ is known as function overloading. Every overloaded function should however have a different prototype.
To find area of square, rectangle and circle
#include< iostream.h>
#include< conio.h>
int area(int);
int area(int,int);
float area(float);
void main()
{
clrscr();
cout< < " Area Of Square: "< < area(4);
cout< < " Area Of Rectangle: "< < area(4,4);
cout< < " Area Of Circle: "< < area(3.2);
getch();
}
int area(int a)
{
return (a*a);
}
int area(int a,int b)
{
return(a*b);
}
float area(float r)
{
return(3.14 * r * r);
}

Explain about the constructors and Destructors with suitable example


Answer
Constructors and Destructors:
Constructors are member functions of a class which have a same name as a class name. Constructors are
called automatically whenever an object class is created. Whereas destructors are also the member functions
with the same name as class, they are invoked automatically whenever object life expires , therefore it is
used to return memory back to the system if the memory was dynamically allocated. Generally the
destructor function is needed only when constructor has allocated dynamic memory. Destructors are
differentiated by prefix tilde (~) from constructors.
Constructor and destructors are usually defined as public members of their class and may never possess a
return value. Constructs can be overloaded whereas destructors cannot be overloaded.
Example : constructor
class myclass
{
private: int a; int b;
public:
myclass()
{a=10;
b=10;
}
int add(void)
{return a+b;
}
};
void main(void)
{myclass a;
cout<<a.add();
}
Example Destructor
#include<iostream.h>
class myclass
{public:
~myclass()
{cout<<"destructed\n";
}
};
void main(void)
{myclass obj;
cout<<"inside main\n
}
Explain about polymorphism. Explain its significance
Answer
Polymorphism means the ability to take more than one form. Defined as feature in c++ where operator or
function behaves differently depending upon what they are operating on. The behavior depends on the data
types used in the operation. Polymorphism is extensively used in implementing Inheritance.
The operator + will be adding two numbers when used with integer variables. However when used with user
defined string class, + operator may concatenate two strings. Similarly same functions with same function
name can perform different actions depending upon which object calls the function. Operator overloading is
a kind of polymorphism.
In C++, polymorphism enables the same program code calling different functions of different classes

Example:
In below codewWe are using a common code as following so that you can draw several of these shapes with
same code and the shape to be drawn is decided during runtime:
Shape *ptr[100]
For (int j=0;j<n;j++)
Ptr[j]->draw();
As in the above code if ptr pointing to rectangle then rectangle is drawn and if it points to circle then circle is
drawn.

What is an inheritance? Explain different types of inheritance


Answer
Inheritance allows one data type to acquire characteristics and behavior of other data types this feature of
inheritance allows easy modification of existing code and also programmer can reuse code without modifying
the original one which saves the debugging and programming time and effort. It can be defined as process of
creating a new class called derived class from the existing or base class. Thus the child class has all the
functionality of the parent class and has additional features of its own. Inheritance from a base class may be
declared as public, protected, or private.
Types of Inheritance:
Inheritance are of five types as follows
1) Single inheritance
It has only one base class and one derived class
2) Multilevel inheritance
In multilevel inheritance another class is derived from the derived class
3) Multiple Inheritance
There are two base classes and one derived class which is derived from both two base classes.
4) Hierarchical inheritance
In this there are several classes derived from a single base class.
5) Hybrid inheritance
Combination of any above mentioned inheritance types.

Write a c++ program to implement the relational operator overloading for the distance
class
Answer
#include <iostream>
using namespace std;
class Distance
{private:
int feet;
// 0 to infinite
int inches;
// 0 to 12
public:
// required constructors
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// method to display distance
void displayDistance()
{cout << "F: " << feet << " I:" << inches <<endl;
}

// overloaded minus (-) operator


Distance operator- ()
{feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
// overloaded < operator
bool operator <(const Distance& d)
{if(feet < d.feet)
{return true;
}
if(feet == d.feet && inches < d.inches)
{return true;
}
return false;
}
};
int main()
{Distance D1(11, 10), D2(5, 11);
if( D1 < D2 )
{cout << "D1 is less than D2 " << endl;
}
else
{cout << "D2 is less than D1 " << endl;
}
return 0;
}

Create a class String which stores a string value. Overload ++ operator which converts
the characters of the string to uppercase (toupper() library function of ctype.h can be
used).
Answer
# include<iostream.h>
# include<ctype.h>
# include<string.h>
# include<conio.h>
class string
{ char str[25];
public:
string()
{ strcpy(str, );}
string(char ch[])
{ strcpy(str, ch);}
void display()
{ cout<<str;}
string operator ++()
{string temp;
int i;
for(i=0;str[i]!=\0;i++)
temp.str[i]=toupper(str[i]);
temp.str[i]=\0;
return temp;
}

};
void main()
{ clrscr();
string s1=hello, s2;
s2=s1++;
s2.display();
getch();
}

What is a virtual function? Explain it with an example


Answer
A virtual function is a member function that is declared within a base class and redefined by a derived class.
To create virtual function, precede the functions declaration in the base class with the keyword virtual.
When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its
own needs.
Virtual means existing in effect but not in reality. Virtual functions are primarily used in inheritance.
Class A
{
int a;
public:
A()
{
a = 1;
}
virtual void show()
{
cout <<a;
}
};
Class B: public A
{
int b;
public:
B()
{
b = 2;
}
virtual void show()
{
cout <<b;
}
};
int main()
{
A *pA;
B oB;
pA = &oB;
pA->show();
return 0;
}
Output is 2 since pA points to object of B and show() is virtual in base class A.

Explain different access specifiers in a class


Answer
Access specifiers control access to class members. Or access specifiers in C++ determine the scope of the
class members.
A common set of access specifiers that c++ supports are as follows:
Private:
Restricts the access to the class itself therefore none of the external function can access the private data and
member functions. Therefore if a class member is private, it can be used only by the members and friends of
class.
Public:
Public means that any code can access the member by its name. Therefore If a class member is public, it can
be used anywhere without the access restrictions.
Protected:
The protected access specifier restricts access to member functions of the same Class, or those of derived
classes.

Define a STUDENT class with USN, Name, and Marks in 3 tests of subject. Declare an array of 10 STUDENT
objects. Using appropriate functions, find the average of two better marks for each student. Print the USN,
Name and the average marks of all the student
Answer
#include<iostream.h>
#include<conio.h>
class student
{
private:
char usn[10];
float avg;
char name[30];
int test[3];
public:
void get_stud_details()
{
cout<<"Enter the USN number: ";
cin>>usn;
cout<<"Enter the name of the student: ";
cin>>name;
cout<<"Enter the marks of three test: \n";
for(int i=0;i<3;i++)
{
cin>> test[i];
}
}
void net_avg()
{
int m,j,k;
avg=0;
for( j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(test[j]>test[k])

{
m=test[j];
test[j]=test[k];
test[k]=m;
}
}
}
avg=(test[1]+test[2])/2.0;
}
void display()
{
cout<<"|******Student details are******|"<<endl;
cout<<"Student USN:"<<usn<<endl;
cout<<"Student name:"<<name<<endl;
cout<<"The best of two from the three marks"<<endl;
for( int i=1;i<3;i++)
cout<<test[i]<<endl;
cout<<"\nAverage:"<<avg<<endl;
}
};
void main()
{
int n;
student s[100];
clrscr();
cout<<"Enter the number of students:";
cin>>n;
for(int i=0;i<n;i++)
{
s[i].get_stud_details();
s[i].net_avg();
}
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
Write a C++ program to create a template function for quick sort and demonstrate sorting of integers
Answer
#include<iostream.h>
#include<conio.h>
template<class T>
int partition( T a[],int low,int high )
{T num=a[low];
int i=low+1;
int j=high;
T temp;
while( 1 )
{while( i<high && num>a[i] )

i++;
while( num<a[j] )
j--;
if( i<j )
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}else
{temp=a[low];
a[low]=a[j];
a[j]=temp;
return(j);
}
}
}
template<class T>
void Quick(T a[],int low,int high )
{int j;
if( low<high )
{j=partition(a,low,high);
Quick(a,low,j-1);
Quick(a,j+1,high );
}
}
void main()
{int N;
clrscr();
cout<<"Enter array size : ";
cin>>N;
int *p=new int[N];
cout<<"\nEnter "<<N<<" int no's..\n";
for(int i=0; i<N; i++ )
{cin>>p[i];
}
cout<<"\nArray before sorting is..\n";
for(i=0; i<N; i++ )
cout<<p[i]<<"\n";
Quick( p,0,N-1 );
cout<<"\nArray after sorting is..\n";
for(i=0; i<N; i++ )
cout<<p[i]<<"\n";
getch();
}

You might also like