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
Program
#include<iostream.h>
#include<conio.h>
class complexNumber
{
private:
double real;
double imaginary;
public:
complexNumber(double r=0.0,double i=0.0)
{
real=r;
imaginary=i;
}
void set_value(double r, double i)
{
real =r;
imaginary=i;
}
void show_value()
{
if(imaginary >=0)
cout<<real<<"+"<<imaginary<<"i"<<endl;
else
cout<<real<<"-"<<-imaginary<<"i"<<endl;
}
};
void main()
{ clrscr();
complexNumber c1(3,-4);
cout<<"COMPLEX NUMBER C1:";
c1.show_value();
c1.set_value(5,2);
cout<<"updated compex number c1:";
c1.show_value();
getch();
}
OUTPUT:
b. Add, subtract and multiply two complex numbers
Program
#include<iostream.h>
#include<conio.h>
class complexNumber
{
private:
double real;
double imaginary;
public:
complexNumber(double r=0.0,double i=0.0)
{
real=r;
imaginary=i;
}
double get_real()const
{
return real;
}
double get_imaginary()const
{
return imaginary;
}
complexNumber add(const complexNumber&other)const
{
double r_real=real+[Link];
double r_imaginary=imaginary+[Link];
return complexNumber(r_real,r_imaginary);
}
complexNumber sub(const complexNumber&other)const
{double r_real=[Link];
double r_imaginary=[Link];
return complexNumber(r_real,r_imaginary);
}
complexNumber multiply(const complexNumber&other)const
{
double r_real=real*[Link]-imaginary*[Link];
double r_imaginary=real*[Link]+imaginary*[Link];
return complexNumber(r_real,r_imaginary);
}
void show_value()const
{if (imaginary>=0)
cout<<real<<"+"<<imaginary<<"i"<<endl;
else
cout<<real<<"-"<<-imaginary<<"i"<<endl;
}
};
void main()
{
clrscr();
complexNumber c1(3,-4);
complexNumber c2(5,2);
cout<<"complex number c1:";
c1.show_value();
cout<<"complex number c2:";
c2.show_value();
complexNumber sum =[Link](c2);
complexNumber sub=[Link](c2);
complexNumber product=[Link](c2);
cout<<"\n sum of c1 and c2;";
sum.show_value();
cout<<"difference of c1 and c2:";
sub.show_value();
cout<<"product of c1 and c2:";
product.show_value();
getch();
}
OUTPUT:
c. Multiplying the complex number with a scalar value
Program:
#include<iostream.h>
#include<conio.h>
class complexnumber
{
private:
double real;
double imaginary;
public:
complexnumber(double r=0.0,double i=0.0)
{
real=r;
imaginary=i;
}
double get_real()const
{
return real;
}
double get_imaginary()const
{
return imaginary;
}
complexnumber multiply_scalar(double scalar)const
{
double result_real=real*scalar;
double result_imaginary=imaginary*scalar;
return complexnumber(result_real,result_imaginary);
}
void show_value()const
{
if(imaginary>=0)
cout<<real<<"+"<<imaginary<<"i"<<endl;
else
cout<<real<<"-"<<-imaginary<<"i"<<endl;
}
};
void main()
{
clrscr();
complexnumber c1(3,-4);
cout<<"complex number c1:";
c1.show_value();
double scalar=2;
complexnumber result=c1.multiply_scalar(scalar);
cout<<"c1 multiplied by scalar"<<scalar<<":";
result.show_value();
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
Program:
#include<iostream.h>
#include<conio.h>
class point
{
private:
double x;
double y;
public:
point(double xcoord=0.0,double ycoord=0.0)
{
x=xcoord;
y=ycoord;
}
void set_value(double xcoord,double ycoord)
{
x=xcoord;
y=ycoord;
}
double get_x()const
{
return x;
}
double get_y()const
{
return y;
}
void show_value()const
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
};
void main()
{
clrscr();
point p1(3.5,2.8);
cout<<"point p1:";
p1.show_value();
p1.set_value(5.2,-1.7);
cout<<"updated point p1:";
p1.show_value();
getch();
}
OUTPUT:
b. Find the distance between two points
Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class point
{
private:
double x;
double y;
public:
point(double xcoord=0.0,double ycoord=0.0)
{
x=xcoord;
y=ycoord;
}
void set_value(double xcoord,double ycoord)
{
x=xcoord;
y=ycoord;
}
double get_x()const
{
return x;
}
double get_y()const
{
return y;
}
double distance_to(const point & other)const
{
double dx=x-other.x;
double dy=y-other.y;
return sqrt(dx*dx+dy*dy);
}
};
void main()
{
clrscr();
point p1(3.0,4.0);
point p2(6.0,8.0);
cout<<"point p1:
("<<p1.get_x()<<","<<p1.get_y()<<")"<<endl; cout<<"point
p2:("<<p2.get_x()<<","<<p2.get_y()<<")"<<endl; double
distance=p1.distance_to(p2);
cout<<"distance between p1 ans p2:"<<distance<<endl;
getch();
}
OUTPUT:
c. Check whether two points are equal or not
Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class point
{
private:
double x;
double y;
public:
point(double xcoord,double ycoord)
{
x=xcoord;
y=ycoord;
}
int isEqual(const point &other)const
{
return(x==other.x && y==other.y);
}
double get_x()
{
return x;
}
double get_y()
{
return y;
}
};
void main()
{
clrscr();
point p1(3.5,4.2);
point p2(3.5,4.2);
point p3(1.0,2.4);
if ([Link](p2))
{
cout<<"p1 is equal to p2"<<endl;
}
else
{
cout<<"p1 is not equal to p2"<<endl;
}
if ([Link] (p3))
{
cout<<"p1 is equal to p3"<<endl;
}
else
{
cout<<"p1 is not equal to p3"<<endl;
}
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
Program:
#include<iostream.h>
#include<conio.h>
class Harmonicprogression
{
private:
double start;
double increment;
int numterm;
public:
Harmonicprogression(double S,double inc,int term)
{
start=S;
increment=inc;
numterm=term;
}
void generateHP()
{
double currentterm=start;
cout<<"Harmonic progression with"<<numterm<<"terms:"<<endl;
for(int i=0;i<numterm;++i)
{
cout<<currentterm<<" ";
currentterm+=increment;
}
cout<<endl;
}
};
void main()
{
clrscr();
Harmonicprogression ob(1.4,2.5,10);
[Link]();
getch();
}
OUTPUT:
b. Calculate the sum of the HP to n terms and to infinity
Program:
#include<iostream.h>
#include<conio.h>
class harmonicprogression
{
private:
double start;
double increment;
public:
harmonicprogression(double s=1.0,double
inc=1.0):start(s),increment(inc){}
double sumToN(int n)
{
double sum=0.0;
double currentterm=start;
for(int i=0; i<n; ++i)
{
sum+=1.0/currentterm;
currentterm+=increment;
}
return sum;
}
double sumToInfinity()
{
double sum=0.0;
double currentterm=start;
while(1)
{
sum+=1.0/currentterm;
currentterm+=increment;
if(currentterm>1.0+10)
break;
}
return sum;
}
};
void main()
{
clrscr();
harmonicprogression hp(1.0,1.0);
int n=10;
double sumToN=[Link](n);
cout<<"sum of harmonic progression to"<<n<<"term:"<<sumToN<<endl;
double sumToInf=[Link]();
cout<<"sum of harmonic progression to infinity:"<<sumToInf<<endl;
getch();
}
OUTPUT:
c. Generate the nth term of the HP
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class NthTermHp
{
public:
double GenerateHP(double n)
{
if(n<=0)
{
exit(0);
}
return 1.0/n;
}
};
void main()
{
clrscr();
double n;
NthTermHp ob;
cout<<"Enter the nth term";
cin>>n;
double term=[Link](n);
cout<<"the"<<n<<"th term of the Harmonic Progression is "<<term;
getch();
}
OUTPUT:
d. Generate the corresponding Arithmetic Progression. (Design
and implement a class that encapsulates an AP, and allow
the HP class to use its facilities by implementing friend
functions
#include<iostream.h>
#include<conio.h>
class arithmeticprogression;
class harmonicprogression
{
private:
int firstterm;
int commondifference;
public:
harmonicprogression(int first,int diff)
{
firstterm=first;
commondifference=diff;
}
friend void useap(harmonicprogression hp,arithmeticprogression ap);
};
class arithmeticprogression
{
private:
int firstterm;
int commondifference;
public:
arithmeticprogression(int first,int diff)
{
firstterm=first;
commondifference=diff;
}
void generateap(int term)
{
cout<<"arithmetic progression generated:"<<endl;
int currentterm=firstterm;
for(int i=0;i<term;++i)
{
cout<<currentterm<<" ";
currentterm+=commondifference;
}
cout<<endl;
}
friend void useap(harmonicprogression hp,arithmeticprogression ap)
{
[Link]([Link]);
}
};
int main()
{
clrscr();
harmonicprogression hp(1,2);
arithmeticprogression
ap(2,3); useap(hp,ap);
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.
#include<iostream.h>
#include<string.h>
#include<conio.h>
class solid
{
private:
float length;
float breadth;
float height;
char type[10];
public:
void setdimension(float l,float b,float h,const char*t)
{
length=l;
breadth=b;
height=h;
strcpy(type,t);
}
void display()
{
cout<<"length: "<<length<<endl;
cout<<"breadth: "<<breadth<<endl;
cout<<"height: "<<height<<endl;
cout<<"type: "<<type<<endl;
}
};
int main()
{
clrscr();
solid s;
[Link](5.5,6.6,7.7,"cube");
[Link]();
getch();
}
OUTPUT:
b. Use functions to calculate volume and surface area for different
solids.
#include<iostream.h>
#include<string.h>
#include<conio.h>
class solid
{
private:
float length;
float breadth;
float height;
char type[10];
public:
void setdimensions(float l,float b,float h,char*t)
{
length=l;
breadth=b;
height=h;
strcpy(type,t);
}
void display()
{
cout<<"length:"<<length<<endl;
cout<<"breadth:"<<breadth<<endl;
cout<<"height:"<<height<<endl;
cout<<"type:"<<type<<endl;
}
float volume()
{
if(strcmp(type,"cube")==0)
{
return length*length*length;
}
else if(strcmp(type,"cuboid")==0)
{
return length*breadth*height;
}
else
{
return 0;
}
}
float surfacearea()
{
if(strcmp(type,"cube")==0)
{
return 6*length*length;
}
else if(strcmp(type,"cuboid")==0)
{
return 2*(length*breadth+breadth*height+height*length);
}
else
{
return 0;
}
}
};
int main()
{ clrscr();
solid s;
[Link](5.5,5.5,5.5,"cube");
[Link]();
cout<<"volume:"<<[Link]()<<endl;
cout<<"surfacearea:"<<[Link]()<<endl;
solid s2;
[Link](5.5,6.6,7.7,"cuboid");
[Link]();
cout<<"volume:"<<[Link]()<<endl;
cout<<"surfacearea:"<<[Link]()<<endl;
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 time objects
c. Adding a given duration to a time
d. Conversion of the time object to seconds
#include<iostream.h>
#include<conio.h>
#include<math.h>
class time
{
private:
int hours;
int minutes;
int seconds;
public:
time(int h=0,int m=0,int s=0)
{
hours=h;
minutes=m;
seconds=s;
}
void setTime(int h,int m,int s)
{
hours=h;
minutes=m;
seconds=s;
}
void showtime()
{
cout<<"time:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
time diff(time t2)
{
int totalsec1=hours*3600+minutes*60+seconds;
int totalsec2=[Link]*3600+[Link]*60+[Link];
int diffsec=abs(totalsec1-totalsec2);
int h=diffsec/3600;
int m=(diffsec%3600)/60;
int s=(diffsec%3600)%60;
return time(h,m,s);
}
void addDuration(int h,int m,int s)
{
hours+=h;
minutes+=m;
seconds+=s;
if(seconds>=60)
{
minutes+=seconds/60;
seconds%=60;
}
if(minutes>=60)
{
hours+=minutes/60;
minutes%=60;
}
}
long toSeconds()
{
return hours*3600+minutes*60+seconds;
}
};
void main()
{ clrscr();
time t1,t2(10,30,45),t3;
[Link](9,15,30);
[Link]();
[Link]();
t3=[Link](t2);
cout<<"difference between t1 and t2:";
[Link]();
[Link](1,10,15);
cout<<"t2 after adding duration:";
[Link]();
cout<<"t1 in seconds:"<<[Link]()<<endl;
getch(); }
OUTPUT:
6. Design a 3x3 matrix class and demonstrate the following:
a. Addition and multiplication of two matrices using operator
overloading
b. Maintaining a count of the number of matrix object created
#include<iostream.h>
#include<conio.h>
class matrix
{
private:
int mat[3][3];
static int count;
public:
matrix()
{
for (int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
mat[i][j]=0;
}
}
count++;
}
void setmatrix(int values[3][3])
{
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
mat[i][j]=values[i][j];
}
}
}
void displaymatrix()
{
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
matrix operator+(matrix&m)
{
matrix result;
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
[Link][i][j]=mat[i][j]+[Link][i][j];
}
}
return result;
}
matrix operator*(matrix&m)
{
matrix result;
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
[Link][i][j]=0;
for(int k=0;k<3;++k)
{
[Link][i][j]+=mat[i][j]*[Link][k][j];
}
}
}
return result;
}
static int getcount()
{
return count;
}
};
int matrix::count=0;
void main()
{ clrscr();
matrix m1,m2,result;
int values1[3][3]=
{
{1,2,3},
{4,5,6},
{7,8,9}
};
[Link](values1);
int values2[3][3]=
{
{9,8,7},
{6,5,4},
{3,2,1}
};
[Link](values2);
cout<<"matrixm1:"<<endl;
[Link]();
cout<<"matrixm2:"<<endl;
[Link]();
result=m1+m2;
cout<<"result of addition(m1+m2):"<<endl;
[Link]();
result=m1*m2;
cout<<"result of multiplication(m1*m2):"<<endl;
[Link]();
cout<<"number of matrix objects created:"<<matrix::getcount()<<endl;
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 strings
c. Find the length of the string
d. Reversing a string
e. Comparing two strings
#include<iostream.h>
#include<conio.h>
#include<string.h>
class cstring
{
private:
char str[100];
public:
cstring()
{
str[0]='\0';
}
cstring(const char*s)
{
strcpy(str,s);
}
cstring concatenate(const cstring&s2)
{
cstring result;
strcpy([Link],str);
strcat([Link],[Link]);
return result;
}
int length()
{
return strlen(str);
}
void reverse()
{
int len=strlen(str);
for(int i=0;i<len/2;++i)
{
char temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
}
int compare(const cstring&s2)
{
return strcmp(str,[Link]);
}
void display()
{
cout<<"string:"<<str<<endl;
}
};
void main()
{ clrscr();
cstring s1("hello");
cstring s2("world");
cstring s3,s4(s1);
cout<<"initial strings:"<<endl;
[Link]();
[Link]();
[Link]();
[Link]();
s3=[Link](s2);
cout<<"after concatenation:"<<endl;
[Link]();
cout<<"length of s3:"<<[Link]()<<endl;
cout<<"after reversing:"<<endl;
[Link]();
[Link]();
cout<<"comparison of s1 and s4:"<<[Link](s4)<<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 as member
functions:
a. Copy Constructor
b. Destructor
c. Concatenate two strings
d. Find the length of the string
e. Reversing a string
f. Comparing two strings
#include<iostream.h>
#include<conio.h>
#include<string.h>
class cstring
{
private:
char str[100];
public:
cstring()
{
str[0]='\0';
}
cstring(const char*s)
{
strcpy(str,s);
}
cstring concatenate(const cstring&s2)
{
cstring result;
strcpy([Link],str);
strcat([Link],[Link]);
return result;
}
int length()
{
return strlen(str);
}
void reverse()
{
int len=strlen(str);
for(int i=0;i<len/2;++i)
{
char temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
}
int compare(const cstring&s2)
{
return strcmp(str,[Link]);
}
void display()
{
cout<<"string:"<<str<<endl;
}
};
void main()
{ clrscr();
cstring s1("hello");
cstring s2("world");
cstring s3,s4(s1);
cout<<"initial strings:"<<endl;
[Link]();
[Link]();
[Link]();
[Link]();
s3=[Link](s2);
cout<<"after concatenation:"<<endl;
[Link]();
cout<<"length of s3:"<<[Link]()<<endl;
cout<<"after reversing:"<<endl;
[Link]();
[Link]();
cout<<"comparison of s1 and s4:"<<[Link](s4)<<endl;
getch();
}
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.
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Shape
{
public:
virtual float area()const=0;
virtual ~Shape(){}
};
class Triangle:public Shape
{
private:
float base,height;
public:
Triangle(float b,float h):base(b),height(h){}
float area()const //override
{
return 0.5*base*height;
}
};
class Rectangle:public Shape
{
private:
float length,breadth;
public:
Rectangle(float l, float b):length(l),breadth(b){}
float area()const //overrride
{
return length*breadth;
}
};
class Circle:public Shape
{
private:
float radius;
public:
Circle(float r):radius(r){}
float area()const //override
{
return M_PI*radius*radius;
}
};
void main()
{
clrscr();
Triangle T(4,5);
Rectangle R(3,6);
Circle C(5);
cout<<"Area of Triangle:"<<[Link]()<<endl;
cout<<"Area of Rectangle:"<<[Link]()<<endl;
cout<<"Area of Circle:"<<[Link]()<<endl;
getch();
}
Output:
[Link] 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 for array-bound
violations
#include <iostream>
#include <stdexcept> // For std::out_of_range
#include <algorithm> // For std::sort
// Template class for a single-dimensional array
template <typename T>
class Array {
private:
T* data; // Pointer to hold the array
size_t size; // Size of the array
public:
// Constructor to initialize the array with a given size
Array(size_t size) : size(size) {
data = new T[size];
}
// Destructor to clean up the dynamically allocated array
~Array()
{ delete[]
data;
}
// Accessor function to set or get an array element, with bounds checking
T& operator[](size_t index) {
if (index >= size) {
throw std::out_of_range("Array index out of bounds");
}
return data[index];
}
// Function to get the size of the array
size_t getSize() const {
return size;
}
// Function to sort the array elements
void sortArray() {
std::sort(data, data + size);
}
// Function to display the array
void display() const {
for (size_t i = 0; i < size; ++i)
{ std::cout << data[i] << " ";
}
std::cout << std::endl;
}
};
// Test the Array class
int main() {
try {
Array<int> arr(5); // Create an array of size 5
// Initialize the array
arr[0] = 4;
arr[1] = 2;
arr[2] = 5;
arr[3] = 1;
arr[4] = 3;
std::cout << "Original array: ";
[Link]();
// Sort the array
[Link]();
std::cout << "Sorted array: ";
[Link]();
// Test out-of-bounds access (uncomment to test exception)
// arr[5] = 10; // This will throw an exception
} catch (const std::exception& e)
{ std::cerr << [Link]() << std::endl;
}
return 0;
}
Output:
[Link] the use of the vector STL container.
a. Implement a telephone directory using files
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm> // for std::sort
// Struct representing a contact with name and phone number
struct Contact {
std::string name;
std::string phone;
// Comparison operator to sort contacts by
name bool operator<(const Contact& other)
const {
return name < [Link];
}
};
// Class for managing the telephone directory
class TelephoneDirectory {
private:
std::vector<Contact> contacts; // Vector to hold all contacts
const std::string filename = "[Link]"; // File to store the contacts
public:
// Function to add a new contact
void addContact(const std::string& name, const std::string& phone)
{ Contact newContact{name, phone};
contacts.push_back(newContact);
std::cout << "Contact added: " << name << " - " << phone <<
std::endl;
}
// Function to display all contacts
void displayContacts() const {
if ([Link]()) {
std::cout << "No contacts in the directory." << std::endl;
} else {
std::cout << "Telephone Directory:" << std::endl;
for (const auto& contact : contacts) {
std::cout << "Name: " << [Link] << ", Phone: " <<
[Link] << std::endl;
}
}
}
// Function to save contacts to a file
void saveToFile() {
std::ofstream outFile(filename);
if (!outFile) {
std::cerr << "Error opening file for writing!" << std::endl;
return;
}
for (const auto& contact : contacts) {
outFile << [Link] << "\n" << [Link] << "\n";
}
[Link]();
std::cout << "Contacts saved to file." << std::endl;
}
// Function to load contacts from a file
void loadFromFile() {
std::ifstream inFile(filename);
if (!inFile) {
std::cerr << "Error opening file for reading!" << std::endl;
return;
}
[Link](); // Clear existing contacts
std::string name, phone;
while (getline(inFile, name) && getline(inFile, phone)) {
contacts.push_back(Contact{name, phone});
}
[Link]();
std::cout << "Contacts loaded from file." << std::endl;
}
// Function to sort contacts by name
void sortContacts() {
std::sort([Link](), [Link]());
std::cout << "Contacts sorted by name." << std::endl;
}
};
// Main function to interact with the directory
int main() {
TelephoneDirectory directory;
[Link](); // Load any existing contacts from the file
int choice;
std::string name, phone;
do {
std::cout << "\n1. Add Contact\n2. Display Contacts\n3. Save
Contacts\n4. Sort Contacts\n5. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter Name: ";
std::[Link](); // Clear the input buffer
std::getline(std::cin, name);
std::cout << "Enter Phone: ";
std::getline(std::cin, phone);
[Link](name, phone);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
std::cout << "Exiting..." << std::endl;
break;
default:
std::cout << "Invalid choice!" << std::endl;
break;
}
} while (choice != 5);
return 0;
}
Output: