1.
Write a class to represent a complex number which has member functions to do the
following
a. Set and show the value of the complex number
b. Add, subtract and multiply two complex numbers
c. Multiplying the complex number with a scalar value
Code:
#include<iostream.h>
#include<conio.h>
class Complex
{
private :
intreal,imag,scalarvalue;
public :
void getvalue()
{
cout<<"\nGivethe real number :";
cin>>real;
cout<<"Give the imagnary number :";
cin>>imag;
}
void addition(Complex c1,Complex c2)
{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
cout<<"Addition of two complex is :"<<real<<"+"<<imag<<"i";
}
void subraction(Complex c1,Complex c2)
{
real=c1.real-c2.real;
imag=c1.imag-c2.imag;
cout<<"\nSubraction of two complex is :"<<real<<"+"<<imag<<"i";
}
void multiply(Complex c1,Complex c2)
{
real=c1.real*c2.real-c1.imag*c2.imag;
imag=c1.real*c2.imag+c2.real*c1.imag;
cout<<"\nMultiplication of two complex is :"<<real <<"+"<<imag<<"i";
}
void multiplywithscalar(Complex c1)
{
cout<<"\nGive the scalar value:";
cin>>scalarvalue;
real=c1.real*scalarvalue;
mag=c1.imag*scalarvalue;
cout<<"Multiplication with scalarvalueis :"<<real<<"+"<<imag<<"i";
}
};
void main()
{
clrscr();
Complex C,C1,C2;
cout<<"Give the First real and imag number";
C1.getvalue();
cout<<"Give the Second real and imag number ";
C2.getvalue();
C.addition(C1,C2);
C.subraction(C1,C2);
C.multiply(C1,C2);
C.multiplywithscalar(C1);
getch();
}
Output :
2. Write a point class that represents a 2-d point in a plane. Write member functions to
a. Set and show the value of a point
b. Find the distance between two points
c. Check whether two points are equal or not
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Point
{
private :
int x1,y1,x2,y2;
float dist;
public :
void get()
{
cout<<"\nEnter X1 value :";
cin>>x1;
cout<<"Give the y1 value :";
cin>>y1;
cout<<"\nEnter X2 value :";
cin>>x2;
cout<<"Give the y2 value :";
cin>>y2;
}
void display(Point p1)
{
cout<<"x1,y1= ("<<p1.x1<<","<<p1.y1<<")";
cout<<"\nx2,y2= ("<<p1.x2<<","<<p1.y2<<")";
}
void distance(Point p1)
{
dist=sqrt(pow(p1.x2-p1.x1,2)+(pow(p1.y2-p1.y1,2)));
cout<<"\nDistance between two point is :"<<dist;
}
void equalornotequal(Point p2)
{
if(p2.x1==p2.x2&&p2.y1==p2.y2)
{
cout<<"\nTwo points are equal";
}
else
{
cout<<"\nGiven point are not equal";
}
}
};
void main()
{
clrscr();
Point P,P1,P2;
cout<<"Give the points for distance :";
P1.get();
P.display(P1);
P.distance(P1);
cout<<"\nGive
nGive check the points for equal or not equal :";
P2.get();
P.display(P2);
P.equalornotequal(P2);
getch();
}
Output:
3. Design and implement a class that represents a Harmonic progression(hp). Implement
functions to do the following
a. Generate the Hp up to a specified number of terms
b. Calculate the sum of HP to n terms and to infinity
Code:
#include<iostream.h>
#include<conio.h>
class Generate
{
private :
int a1,d,n;
float findseries(int a1,int d, int n);
float an,term,sumval;
public:
void getdata()
{
cout<<"Enter a1 term :";
cin>>a1;
cout<<"Enter difference d :";
cin>>d;
cout<<"Enter number of terms n :";
cin>>n;
cout<<"Harmonic progression : ";
}
void display();
void findseries();
};
void Generate::display()
{
for(float y=1;y<=n;y++)
{
an=a1+(y-1)*d;
cout<<"1/"<<an<<" ,";
}
}
void Generate::findseries()
{
for(float y=1;y<=n;y++)
{
term=(1.0)/(float)(a1+(y-1)*d);
sumval+=term;
}
cout<<"\nThe sum of hp is "<<sumval;
}
void main()
{
clrscr();
Generate G;
G.getdata();
G.display();
G.findseries();
getch();
}
Output:
4.Design and implement a class to represent a Solid object
a. Apart from data members to represent dimensions,use a data member to specify the type
of solid.
b. Use functions to calculate volume and surface area for different solids.
Code:
#include<iostream.h>
#include<conio.h>
class Solidobject
{
private:
float pi,vol,surarea;
public:
void volume(float r)
{
pi=3.14159;
vol = (float(4) / float(3)) * pi * r * r * r;
cout << "Volume Of Sphere :" << vol << endl;
}
void surface_area(float r)
{
surarea = 4 * pi * r * r;
cout << "Surface Area Of Sphere :" << surarea << endl;
}
};
void main()
{
clrscr();
int radius = 5;
Solidobject S;
S.volume(radius);
S.surface_area(radius);
getch();
}
Output:
5. Design a class representing time in hh:mm:ss. Write functions to
a. Set and show the time
b. Find the difference between two the objects
c. Adding a given duration to a time
d. Conversion of the time object to seconds
Code:
#include<iostream.h>
#include<conio.h>
class Time
{
private:
inthour,minute,second,diffhour,diffsec,diffmin;
inthrs,min,sec;
public:
void get()
{
cout<<"\nEnterhour :";
cin>>hour;
cout<<"Enter minute :";
cin>>minute;
cout<<"Enter second:";
cin>>second;
}
void difference(Time t1,Time t2)
{
if(t2.second>t1.second)
{
t1.minute--;
t1.second+=60;
}
diffsec=t1.second-t2.second;
if(t2.minute>t1.minute)
{
t1.hour--;
t1.minute+=60;
}
diffmin=t1.minute-t2.minute;
diffhour=t1.hour-t2.hour;
cout<<"\nTime Difference is :"<<diffhour<<":"<<diffmin<<":"<<diffsec;
}
void duration(Time t1,Time t2)
{
second=t1.second+t2.second;
minute=t1.minute+t2.minute+(second/60);
hour=t1.hour+t2.hour+(minute/60);
minute=minute%60;
second=second%60;
cout<<"\nTotal time is :"<<hour<<":"<<minute<<":"<<second;
}
void conversion()
{
cout<<"\nEnter hours:";
cin>>hrs;
min=hrs*60;
sec=min*60;
cout<<hrs<<"hours="<<min<<"minutes="<<sec<<"seconds";
}
};
void main()
{
clrscr();
Time T,T1,T2;
cout<<"Enter time period 1";
T1.get();
cout<<"\nEnter time period 2";
T2.get();
T.difference(T1,T2);
cout<<"\n\nTwo time duration adding";
T.duration(T1,T2);
cout<<"\n\nTime is conversion in to Seconds";
T.conversion();
getch();
}
Output:
6. Design a 3x3 matrix class and demonstrate the following:
a. Addition and multiplication of two matrices using operator overloading
code:
#include<iostream.h>
#include<conio.h>
class Matrix
{
private:
int a[3][3];
public:
void accept();
void display();
void operator +(Matrix x);
void operator *(Matrix x);
};
void Matrix::accept()
{
cout<<"\n Enter Matrix Element (3 X 3) : \n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<" ";
cin>>a[i][j];
}
}
}
void Matrix::display()
{
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
void Matrix::operator +(Matrix x)
{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]+x.a[i][j];
}
}
cout<<"\n Addition of Matrix : \n\n";
for(int k=0;k<3;k++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<mat[k][j]<<"\t";
}
cout<<"\n";
}
}
void Matrix::operator *(Matrix x)
{
int mul[3][3],i,j,k;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mul[i][j]=0;
for(k=0;k<3;k++)
{
mul[i][j]=mul[i][j]+a[i][k]*x.a[k][j];
}
}
}
cout<<"Multiplication of two matrix is\n";
for(int l=0;l<3;l++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<mul[l][j]<<"\t";
}
cout<<"\n";
}
}
void main()
{ clrscr();
Matrix M,N;
M.accept();
N.accept();
cout<<"\n First Matrix : \n\n";
M.display();
cout<<"\n Second Matrix : \n\n";
M.display();
M+N;
M*N;
getch();
}
Output:
7. Design a class called cString to represent a string data type. Create a data member in the
Class to represent a string using an array of size 100. Write the following functionality as
Member functions:
a. Copy Constructor
b. Concatenate two string
c. Find the length of the string
d. Reversing a string
e. Comparing two strings
Code:
#include <iostream.h>
#include<conio.h>
class cString
{
private:
char str[100];
public:
cString()
{
str[0] = '\0';
cString(const char* input);
cString(const cString& other);
cString concatenate(const cString& other) const;
void reverse();
int compare(const cString& other) const;
int length() const;
void display() const
{
cout << str << endl;
}
};
int cString:: length() const
{
int len = 0;
while (str[len] != '\0')
{
len++;
}
return len;
}
int cString ::compare(const cString& other) const
{
int i = 0;
while (str[i] != '\0' && other.str[i] != '\0')
{
if (str[i] != other.str[i])
{
return 0;
}
i++;
}
return str[i] == other.str[i];
}
void cString:: reverse()
{
int len = length();
for (int i = 0; i < len / 2; ++i)
{
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
cString::cString(const char* input)
{
int i = 0;
while (input[i] != '\0' && i < 99)
{
str[i] = input[i];
i++;
}
str[i] = '\0';
}
void cString::cString(const cString& other)
{
int i = 0;
while (other.str[i] != '\0' && i < 99)
{
str[i] = other.str[i];
i++;
}
str[i] = '\0';
}
cString cString:: concatenate(const cString& other) const
{
cString result;
int i = 0;
while (str[i] != '\0' && i < 99)
{
result.str[i] = str[i];
i++;
}
int j = 0;
while (other.str[j] != '\0' && i < 99)
{
result.str[i] = other.str[j];
i++;
j++;
}
result.str[i] = '\0';
return result;
}
void main()
{
clrscr();
cString str1("Hello");
cString str2("World");
cout << "String 1: ";
str1.display();
cout << "String 2: ";
str2.display();
cString concatenated = str1.concatenate(str2);
cout << "Concatenated: ";
concatenated.display();
cout << "Length of String 1: " << str1.length() << endl;
cout << "Length of String 2: " << str2.length() << endl;
str1.reverse();
cout << "Reversed String 1: ";
str1.display();
if (str1.compare(str2))
{
cout << "String 1 and String 2 are equal." <<endl;
}
else
{
cout << "String 1 and String 2 are not equal." << endl;
}
getch();
}
Output:
8.Design a class called cString to represent a string data type. Create a data member in the
class to represent a string whose size is dynamically allocated. Write the following functionality
as member functions:
a. Copy Constructor
b. Concatenate two string
c. Find the length of the string
d. Reversing a string
e. Comparing two strings
Code:
#include <iostream.h>
#include<string.h>
#include<conio.h>
class cString
{
private:
char* str;
size_t length;
public:
cString()
{
str = new char[1];
str[0] = '\0';
length = 0;
}
cString(const char* input)
{
length = strlen(input);
str = new char[length + 1];
strcpy(str, input);
}
cString(const cString& other)
{
length = other.length;
str = new char[length + 1];
strcpy(str, other.str);
}
~cString()
{
delete[] str;
}
cString concatenate(const cString& other) const
{
cString result;
result.length = length + other.length;
result.str = new char[result.length + 1];
strcpy(result.str, str);
strcat(result.str, other.str);
return result;
}
size_t getLength() const
{
return length;
}
int compare(const cString& other) const
{
return strcmp(str, other.str) == 0;
}
void display() const
{
cout << str << endl;
}
int lengt() const;
void reverse();
};
void cString::reverse()
{
int len = lengt();
for (int i = 0; i < len / 2; ++i) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int cString:: lengt() const {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
int main()
{ clrscr();
cString str1("Hello");
cString str2("World");
cout << "String 1: ";
str1.display();
cout << "String 2: ";
str2.display();
cString concatenated = str1.concatenate(str2);
cout << "Concatenated: ";
concatenated.display();
cout << "Length of String 1: " << str1.getLength() << endl;
cout << "Length of String 2: " << str2.getLength() << endl;
str1.reverse();
cout << "Reversed String 1: ";
str1.display();
if (str1.compare(str2)) {
cout << "String 1 and String
tring 2 are equal." << endl;
} else {
cout << "String 1 and String 2 are not equal." << endl;
}
getch();
return 0;
}
Output:
9. Create a class to represent a 2-d shape and derive classes to represent a triangle, rectangle
and circle. Write a program using run-time polymorphism to compute the area of the figures.
Code:
#include <iostream.h>
#include<conio.h>
class Shape
{
public:
virtual void calculate()
{
cout<< "Area of your Shape ";
}
virtual ~Shape()
{
cout<< "Shape Destuctor Call\n";
}
};
class Rectangle : public Shape
{
public:
int width, height;
float area;
void calculate()
{
cout<< "Enter Width of Rectangle: ";
cin>> width;
cout<< "Enter Height of Rectangle: ";
cin>> height;
area = height * width;
cout<< "Area of Rectangle: " << area << "\n";
}
virtual ~Rectangle()
{
cout<< "Rectangle Destuctor Call\n";
}
};
class Triangle : public Shape
{
public:
intbase,height;
float half,area;
void calculate()
{
half=0.5;
cout<< "Enter one base your of Triangle: ";
cin>> base;
cout<<"Enter height of your Triangle;";
cin>>height;
area = half*base*height;
cout<< "Area of Triangle: " << area << "\n";
}
virtual ~Triangle()
{
cout<< "Triangle Destuctor Call\n";
}
};
class Circle : public Shape
{
public:
int radius;
float pi,area;
void calculate()
{
pi=3.14;
cout<< "Enter one radius your of Circle: ";
cin>> radius;
area = pi*radius*radius;
cout<< "Area of Circle: " << area << "\n";
}
virtual ~Circle()
{
cout<< "Circle Destuctor Call\n";
}
};
void main()
{
clrscr();
Shape* S;
Rectangle R;
S = &R;
S->calculate();
Triangle T;
S = &T;
S->calculate();
Circle C;
S=&C;
S->calculate();
getch();
}
Output :
10. Define a class template representing a single-dimensional array. Implement a function to
sort the array elements. Include a mechanism to detect and throw an exception array-bound
violations.
Code:
#include <iostream>
#include <stdexcept>
template <class Element, class size>
void sort(Element a[], size n) {
if (n <= 0) {
throw std::invalid_argument("Array size must be greater than zero.");
}
Element e;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
e = a[i];
a[i] = a[j];
a[j] = e;
}
}
}
std::cout << "\nSorted elements are:";
for (int i = 0; i < n; i++) {
std::cout << " " << a[i];
}
}
int main() {
int x[10] = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
std::cout << "Unsorted elements are:";
int n = 10;
for (int i = 0; i < n; i++) {
std::cout << " " << x[i];
}
try {
sort(x, n);
} catch (const std::exception& e) {
std::cerr << "\nError: " << e.what() << std::endl;
}
return 0;
}
Output: