C++ Program
C++ Program
Overload
all the four Arithmetic operators so that they operate on the object FLOAT
#include<iostream.h>
#include<conio.h>
class FLOAT
float n;
public:
FLOAT(){}
void getdata()
cin>>n;
void putdata()
cout<<"\n"<<n;
FLOAT operator+(FLOAT);
FLOAT operator*(FLOAT);
FLOAT operator-(FLOAT);
FLOAT operator/(FLOAT);
};
FLOAT FLOAT::operator+(FLOAT a)
FLOAT t;
t.n=n+a.n;
return t;
}
FLOAT FLOAT::operator*(FLOAT b)
FLOAT t;
t.n=n*b.n;
return t;
FLOAT FLOAT::operator-(FLOAT b)
FLOAT t;
t.n=n-b.n;
return t;
FLOAT FLOAT::operator/(FLOAT b)
FLOAT t;
t.n=n/b.n;
return t;
main()
clrscr();
FLOAT a,b,c;
a.getdata();
b.getdata();
c=a+b;
c=a*b;
c.putdata();
c=a-b;
c.putdata();
c=a/b;
c.putdata();
getch();
Write a C++ program to create a class STRING. Write a Member Function to initialize,
get and display stings. Overload the operators ++ and == to concatenate two Strings and
to compare two strings respectively.
#include<iostream>
#include<string.h>
class String
{
public:
char str[20];
public:
void get()
{
cout<<"\n Enter String : ";
cin>>str;
}
void display()
{
cout<<str;
}
String operator+(String x) //Concatenating String
{
String s;
strcat(str,x.str);
strcpy(s.str,str);
return s;
}
};
int main()
{
String s1, s2, s3;
s1.get();
s2.get();
cout<<"\n ----------------------------------------------";
cout<<"\n\n First String is : ";
s1.display (); //Displaying First String
cout<<"\n\n Second String is : ";
s2.display (); //Displaying Second String
cout<<"\n ----------------------------------------------";
s3=s1+s2; //String is concatenated. Overloaded '+' operator
cout<<"\n\n Concatenated String is : ";
s3.display_string();
return 0;
}
Write a C++ program to create a class SHAPE which consists of two VIRTUAL
FUNCTIONS Calculate_Area() and Calculate_Perimeter() to calculate area and
perimeter of various figures. Derive three classes SQUARE, RECTANGLE, TRIANGE
from class Shape and Calculate Area and Perimeter of each class separately and display
the result.
#include <iostream>
class Shape
protected:
double x, y;
public:
void set_dim(double i, double j=0)
x = i;
y = j;
};
public:
void show_area(void)
cout << "Triangle with height "<< x << " and base " << y<< " has an area of "<< x *
0.5 * y << ".\n”;
};
public:
void show_area(void)
cout << "Square with dimensions "<< x << "x" << y<< " has an area of "<< x * y
<< ".\n";
}
};
public:
void show_area(void)
};
main(void)
Shape *p;
triangle t;
square s;
circle c;
p = &t;
p->set_dim(10.0, 5.0);
p->show_area();
p = &s;
p->set_dim(10.0, 5.0);
p->show_area();
p = &c;
p->set_dim(9.0);
p->show_area();
return 0;
}
Write a C++ program using Function Overloading to read two Matrices of different
Data Types such as integers and floating point numbers. Find out the sum of the above
two matrices separately and display the sum of these arrays individually
#include<iostream>
class matrix
int m,n;
public:
void get()
cin>>m;
cin>>n;
void sum()
cin>>a[i][j];
}}
void sum(void)
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<" ";
};
int main()
matrix cp;
matrix cp1;
cp.get();
cp.sum();
cp.sum(void);
cp1.sum();
cp1.sum(void);
return 0;
}
Write a C++ program to create a class to implement the data structure STACK. Write a
constructor to initialize the TOP of the STACK. Write a member function PUSH() to
insert an element and member function POP() to delete an element. Check for overflow
and underflow conditions
# include<iostream>
class Stack
{
int top;
public:
int a[10]; //Maximum size of Stack
Stack()
{
top = -1;
}
void push(int x);
int pop();
void isEmpty();
};
void Stack::push(int x)
{
if(top >= 10)
{
cout << "Stack Overflow \n";
}
else
{
a[++top] = x;
cout << "Element Inserted \n";
}
}
int Stack::pop()
{
if(top < 0)
{
cout << "Stack Underflow \n";
return 0;
}
else
{
int d = a[top--];
return d;
}
}
void Stack::isEmpty()
{
if(top < 0)
{
cout << "Stack is empty \n";
}
else
{
cout << "Stack is not empty \n";
}
}
int main() {
Stack s1;
s1.push(10);
s1.push(100);
}