Professional Documents
Culture Documents
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);
}
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.
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;
}
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();
}
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();
}