Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lebanese University Programming with C++

Faculty of Engineering Semester V


__________________________________________________________________________________

Name: Ali Makki ID:6478 Date:1/12/2024

LAB 4. Overloading Opearators

Problem 1: Write a class Temperature that has two members double degrees and char scale .

1.1. Write a header file Temperature.h with the following functions:


Temperature();//degrees=0.0, scale = ‘C’
Temperature(double initDegrees, char initScale);//if the scale is lower
//case , convert it to upper case. Check validity of the degrees for the
//corresponding scale(degrees are greater or equal to the absolute zero)
double getDegrees() const;
char getScale() const;
Temperature toFahrenheit();//convert degree to Fahrenheit scale
Temperature toCelsius();//convert degree to Celsius scale
void print();//print the degree and scale of the object
bool operator<(const Temperature & rightOperand) const;
bool operator==(const Temperature & rightOperand) const;
where the last two functions express operator overloading.
1.2. Initialize the scales’ degrees to Absolute zero by the constant
const double MIN_FAHRENHEIT = –459.67, MIN_CELSIUS = –273.15;

Converting formulas:
Celsius(‫﮿‬C) = (Fahrenheit – 32)/1.8
Fahrenheit (oF) = (Celsius *1.8) + 32

Use header file cctype for the functions: islower(), toupper()

1.3. Write the class implementation and store as Temperature.cpp file.

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();
}

You might also like