Putting things together

Putting things together

Requirements:
Linux Distribution
g++
any text editor

My Setup:
Debian 10
g++ version 6.3.0
pluma

I wasnt sure of what exactly to go over next, so this tutorial will be a brief
tutorial of my thought process to create something. In my C tutorials, we
created a temperature converter between Fahrenheit and Celsius . Today we will
be recreating it in multiple ways. We will go over an object-oriented approach,
and a functional approach, the procedural approach is the one we had already
created. So lets begin!

Ok so for our first example will be the functional example. First I would like
to say i am not EXTREMELY confident in functional programming, I am more use to
procedural, however what I know about functional programming is that it doesnt
like to change states, or contain mutable data. So I started off by defining a
couple things. These include getting the temp and storing it in a constant,
defining what when to determine if the temperature we are converting is in
Fahrenheit or Celsius. If the second argument to the program is a ‘c’ , we know
it is a Fahrenheit temperature needing to be converted into a Celsius
temperature. If the second argument is a ‘f’, then we know it is a Celsius
temperature needing to be converted into a Fahrenheit temperature. Next i define
the conversion in 2 functions, toFahrenheit(const double) and toCelsius(const
double). Last I just added a printUsage function like my C Program’s, except
I used std::cout rather then printf.

[sourcecode language=’cpp’]
#include <iostream>
#include <string>

const double temp(const char * var);
const bool isFahrenheit(const char arg);
const bool isCelsius(const char arg);
const double toFahrenheit(const double c);
const double toCelsius(const double f);
void printUsage();

int main(int argc, char * argv[]){

const double degree = temp(argv[2]);
const char convert = argv[1][0];

if(isFahrenheit(convert))
std::cout << toCelsius(degree) << std::endl;
else if(isCelsius(convert))
std::cout << toFahrenheit(degree) << std::endl;
else
printUsage();

return 0;
}

const double temp(const char * var){
std::string::size_type sst;
return std::stod(var,&sst);
}

const bool isFahrenheit(const char arg){
return arg == ‘c’;
}

const bool isCelsius(const char arg){
return arg == ‘f’;
}

const double toFahrenheit(const double c){
return (c * 1.8f) + 32;
}
const double toCelsius(const double f){
return (f – 32) * 5/9 ;
}

void printUsage(){
std::cout <<“Usage: temp [c/f] value” << std::endl;
}

[/sourcecode]
Ok so now that we have our functional example, lets go over an object-oriented
approach. In my head, I will consider temperature as a base class and the type
of temperature as a derived class. Within Temperature i defined a toFahrenheit
and a toCelsius virtual function, and then extended the functions once i got to
the derived classes. I make use of the string header file for the std::stod
function, which takes in a string and outputs the double value of the string.
I also create a temporary string in order to hold it, since the argument comes
in form of a char array. Lets look at it:

[sourcecode language=’cpp’]
#include <iostream>
#include <string>

class Temperature{

protected:
double m_degrees;

public:
Temperature(double degrees): m_degrees(degrees){}
Temperature(const Temperature & copy): m_degrees(copy.m_degrees) {}
Temperature(Temperature && move) : m_degrees(std::move(move.m_degrees)){}
~Temperature(){}

Temperature & operator=(const Temperature & copy){
this->m_degrees = copy.m_degrees;
return *this;
}

Temperature & operator=(Temperature && move){
this->m_degrees = std::move(move.m_degrees);
return *this;
}

//Marking these as virtual
virtual double toCelsius() = 0;
virtual double toFahrenheit() = 0;

};

class Celsius : public Temperature {

public:
Celsius(double degrees):Temperature(degrees){}
~Celsius(){}

double toCelsius(){ return m_degrees;}
double toFahrenheit(){
return ( 1.8f * m_degrees) + 32.0 ;
}

};

class Fahrenheit : public Temperature {

public:
Fahrenheit(double degrees):Temperature(degrees){}
~Fahrenheit(){}

double toCelsius(){
return ( m_degrees – 32.0 ) * (5.0 / 9.0);
}
double toFahrenheit(){ return m_degrees;}
};

void printUsage();

int main(int argc, char * argv[]){

Temperature * temp;
std::string::size_type sz;
if(argc < 3 || argc > 3){
printUsage();
return -1;
}

switch(argv[1][0]){
case ‘c’:
temp = new Fahrenheit(std::stod(std::string(argv[2]),&sz));
std::cout << temp->toCelsius() <<std::endl;
break;
case ‘f’:
temp = new Celsius(std::stod(std::string(argv[2]),&sz));
std::cout << temp->toFahrenheit() <<std::endl;
break;
default:
std::cout << “Error, incorrect syntax” << std::endl;
printUsage();
return -2;
break;
}
delete temp;
return 0;

}

void printUsage(){
std::cout <<“Usage: temp [c/f] value” << std::endl;
}
[/sourcecode]

 

Leave a Reply