String Operations
Answer:
#include <iostream.h>
#include <conio.h>
class Rectangle {
private:
float length;
float width;
public:
void setlength(float);
void setwidth(float);
float perimeter();
float area();
void show();
int sameArea(Rectangle);
};
void Rectangle::setlength(float len) {
length = len;
}
void Rectangle::setwidth(float wid) {
width = wid;
}
float Rectangle::perimeter() {
return (2 * length + 2 * width);
}
float Rectangle::area() {
return length * width;
}
void Rectangle::show() {
cout << "Length: " << length << " Width: " << width;
}
int Rectangle::sameArea(Rectangle other) {
float areaf = length * width;
float areas = other.length * other.width;
if (areaf == areas)
return 1;
return 0;
}
int main() {
Rectangle first, second;
first.setlength(5);
first.setwidth(2.5);
second.setlength(5);
second.setwidth(18.9);
cout << "First rectangle: ";
first.show();
cout << endl << "Area: " << first.area() << " Perimeter: " << first.perimeter() << endl << endl;
cout << "Second rectangle: ";
second.show();
cout << endl << "Area: " << second.area() << " Perimeter: " << second.perimeter() << endl << endl;
if (first.sameArea(second))
cout << "Rectangles have the same area\n";
else
cout << "Rectangles do not have the same area\n";
first.setlength(15);
first.setwidth(6.3);
cout << "First rectangle: ";
first.show();
cout << endl << "Area: " << first.area() << " Perimeter: " << first.perimeter() << endl << endl;
cout << "Second rectangle: ";
second.show();
cout << endl << "Area: " << second.area() << " Perimeter: " << second.perimeter() << endl << endl;
if (first.sameArea(second))
cout << "Rectangles have the same area\n";
else
cout << "Rectangles do not have the same area\n";
getch();
return 0;
}
Answer:
#include <iostream.h>
#include <conio.h>
class complex {
private:
float x;
float y;
public:
void set(float real, float img) {
x = real; y = img;
}
complex sum(complex);
void disp();
};
complex complex::sum(complex C) {
complex t;
t.x = x + C.x;
t.y = y + C.y;
return t;
}
void complex::disp() {
cout << x << " + j" << y << endl;
}
int main() {
complex C1, C2, C3;
C1.set(2.5, 7.1);
C2.set(4.2, 5.5);
C3 = C1.sum(C2);
cout << "\n complex Number 1 = "; C1.disp();
cout << "\n complex Number 2 = "; C2.disp();
cout << "\n complex Number 3 = "; C3.disp();
getch();
return 0;
}
Answer:
#include <iostream.h>
#include <conio.h>
class Distance {
private:
int feet;
float inches;
public:
void setdist(int ft, float in) {
feet = ft; inches = in;
}
Distance add(Distance);
void disp();
};
Distance Distance::add(Distance D) {
Distance t;
t.inches = inches + D.inches;
t.feet = 0;
if (t.inches >= 12.0) {
t.inches -= 12.0;
t.feet++;
}
t.feet += feet + D.feet;
return t;
}
void Distance::disp() {
cout << feet << "\'" << inches << "\" ";
}
int main() {
Distance d1, d2, d3;
d1.setdist(10, 7.1);
d2.setdist(23, 5.5);
d3 = d1.add(d2);
cout << "\n distance 1 = "; d1.disp();
cout << "\n distance 2 = "; d2.disp();
cout << "\n distance 3 = "; d3.disp();
getch();
return 0;
}
Answer:
#include <iostream.h>
#include <conio.h>
class time {
private:
int hours;
int minutes;
public:
void settime(int h, int m) {
hours = h; minutes = m;
}
time sum(time);
void showtime();
};
time time::sum(time TM) {
time t;
t.minutes = minutes + TM.minutes;
t.hours = t.minutes / 60;
t.minutes = t.minutes % 60;
t.hours += hours + TM.hours;
return t;
}
void time::showtime() {
cout << hours << " hours and " << minutes << " minutes" << endl;
}
int main() {
time T1, T2, T3;
T1.settime(2, 45);
T2.settime(3, 30);
T3 = T1.sum(T2);
cout << "\n Time 1 : "; T1.showtime();
cout << "\n Time 2 : "; T2.showtime();
cout << "\n Time 3 : "; T3.showtime();
getch();
return 0;
}