Professional Documents
Culture Documents
LAB 4. Overloading Operators 2024
LAB 4. Overloading Operators 2024
Problem 1: Write a class Temperature that has two members double degrees and char scale .
Converting formulas:
Celsius(﮿C) = (Fahrenheit – 32)/1.8
Fahrenheit (oF) = (Celsius *1.8) + 32
1.4. Write a main() function when you create two objects of Temperature class and test the methods
operator<()and operator==().
1
Problem 2. Overload operator+ for class Temperature so that expressions like a temp + 30.5
can be used to increase a Temperature value by 30.5 degrees in the same scale.
Solution:
Header:
#pragma once
class Temperature
{
private:
double dgree;
char scale;
const double MIN_FAHRENHEIT = -459.67, MIN_CELSIUS = -273.15;
public:
Temperature();
Temperature(double, char);
double get_degree() const;
char get_scale() const;
Temperature toFahernheit();
Temperature toCelsius();
void print();
bool operator<( Temperature& rightOperand) const;
bool operator==(Temperature& rightOperand) const;
void operator+(double );
};
Implementation:
#include "Temperature.h"
#include <iostream>
#include<cctype>
using namespace std;
Temperature::Temperature()
{
this->dgree = 0;
this->scale = 'C';
}
Temperature::Temperature(double a, char c)
{
if (islower(c))
{
toupper(c);
}
if (c == 'C')
{
if (a >= this->MIN_CELSIUS)
this->dgree = a;
}
else
{
if (c == 'F')
{
if (a >= this->MIN_FAHRENHEIT)
this->dgree = a;
}
else
{
cout << "Error in Scale" << endl;
}
}
2
this->scale = c;
}
double Temperature::get_degree()const
{
return this->dgree;
}
Temperature Temperature::toFahernheit()
{
if (this->scale != 'F')
{
this->scale = 'F';
this->dgree = (this->dgree * 1.8) + 32;
}
Temperature a(this->dgree,this->scale);
return a;
}
Temperature Temperature::toCelsius()
{
if (this->scale != 'C')
{
this->scale = 'C';
this->dgree = (this->dgree -32) /1.8;
}
Temperature a(this->dgree, this->scale);
return a;
}
void Temperature::print()
{
cout << "Temperature: " << this->dgree <<" "<< this->scale << endl;
}
bool Temperature:: operator<( Temperature& rightOperand) const
{
if (this->scale == rightOperand.scale)
{
if (this->dgree < rightOperand.dgree)
return true;
else
return false;
}
else
{
if (rightOperand.scale == 'F')
{
rightOperand.toCelsius();
if (this->dgree < rightOperand.dgree)
return true;
else
return false;
}
else
{
rightOperand.toFahernheit();
if (this->dgree < rightOperand.dgree)
return true;
else
return false;
}
}
}
3
bool Temperature:: operator==(Temperature& rightOperand) const
{
if (this->scale == rightOperand.scale)
{
if (this->dgree == rightOperand.dgree)
return true;
else
return false;
}
else
{
if (rightOperand.scale == 'F')
{
rightOperand.toCelsius();
if (this->dgree == rightOperand.dgree)
return true;
else
return false;
}
else
{
rightOperand.toFahernheit();
if (this->dgree == rightOperand.dgree)
return true;
else
return false;
}
}
}
void Temperature::operator+(double a)
{
this->dgree += a;
}
Main():
#include"Temperature.h"
#include <iostream>
using namespace std;
int main()
{
Temperature a(30, 'C');
Temperature b(20, 'C');
Temperature c;
bool x;
x = a < b;
cout << x<<endl;
a + 10;
x = (a == b);
cout << x<<endl;
a.print();
}