Recent Comments

C++

C++ is a general purpose programming language in wide use. Object-oriented languages such as C++ and Java leverage three principles called encapsulation, inheritance, and polymorphism to make programming more connected to concepts that people are familiar with.

This example of the temperature conversion program provides a very brief introduction to these key object-oriented concepts.

Download software: A list of C++ compilers can be found here: http://www.stroustrup.com/compilers.html. The gnu g++ or minGW compilers are good ones to start with.

Cost: Free

Instructions to setup software: Follow the installation instructions for your compiler.

Hardware requirements: PC, Mac

Operating systems supported: Win, Mac, Linux

Difficulty level: 5 out of 5


TRY IT!

1. Create the following 10 files in the editor of your choice. Each class is in a separate set of header “.h” and source “.cpp” files.  The class name is used as the file name.

Temp.h


#ifndef _INCL_GUARD_TEMP
#define _INCL_GUARD_TEMP

class Temp
{
protected:
	double temperature;  // temperature


public:
	Temp(void);
	virtual ~Temp(void);
	Temp(const double &in);
	double getT();
	void setT(double in);
	double convert(double in);
// Program constants
     static double LOW_TEMP_F_WARNING;  
     static double HIGH_TEMP_F_WARNING;

};

#endif

Temp.cpp

#include "Temp.h"

double Temp::LOW_TEMP_F_WARNING=0.;  // Program constants
double Temp::HIGH_TEMP_F_WARNING=100.;

Temp::Temp(void)  // default constructor
{
}

Temp::~Temp(void) // default destructor
{
}

Temp::Temp(const double &in) // copy constructor
{
  temperature=in;
}

double Temp::getT()  // getter
{
	return temperature;
}

void Temp::setT(double in) // setter
{
	temperature=in;
}

CTemp.h

#ifndef _INCL_GUARD_CTEMP
#define _INCL_GUARD_CTEMP

#include "Temp.h"
#include "FTemp.h"


class CTemp :
	public Temp
{
public:
	CTemp(const double &in);
	CTemp(void);
	~CTemp(void);
	CTemp &operator =(FTemp &);
	double convert(double in);
};

#endif

CTemp.cpp

#include "CTemp.h"


CTemp::CTemp(void)
{
}

CTemp::~CTemp(void)
{
}


CTemp::CTemp(const double &inval)
{
  temperature = inval;

}
CTemp &CTemp::operator =(FTemp &inx)
{
	this->temperature = convert(inx.getT());

	return *this;
}

double CTemp::convert(double in){

	return (in-32.)/1.8;
}

FTemp.h


#ifndef _INCL_GUARD_FTEMP
#define _INCL_GUARD_FTEMP


#include "Temp.h"


class FTemp :
	public Temp
{
public:
	FTemp(const double &in);
	FTemp(void);
	~FTemp(void);
	double convert(const double &in);

};


#endif

FTemp.cpp


#include "FTemp.h"


FTemp::FTemp(void)
{
}


FTemp::~FTemp(void)
{
}


FTemp::FTemp(const double &inval)
{
  temperature = inval;
}


double FTemp::convert(const double &in){

	return in*1.8+32.;
}

MyTemperatureConverter.h

ifndef _INCL_GUARD_MYTEMPERATURECONVERTER
#define _INCL_GUARD_MYTEMPERATURECONVERTER


class MyTemperatureConverter
{
protected:
	
	static const int MAX_LOOP=5;

public:
	MyTemperatureConverter(void);
	 ~MyTemperatureConverter(void);
	
	void run(void);

};
#endif

MyTemperatureConverter.cpp

#include <iostream>  // This includes the file stdio.h into your code so it knows what the functions such as printf() mean.


#include "CTemp.h"
#include "FTemp.h"
#include "MyTemperatureConverter.h"


MyTemperatureConverter::MyTemperatureConverter(void)
{
}


MyTemperatureConverter::~MyTemperatureConverter(void)
{
}


void MyTemperatureConverter::run(void)
{

  FTemp temp_f; // Declaration of variables that the program will use
  CTemp temp_c;


  int i; 
  double input;

  for(i=0; i<MAX_LOOP; i++){   // loop

    std:: cout << std::endl << "Enter the temperature in degrees F : ";     // Input the temperature to convert 
    std::cin >> input;     // Reads the user input

	temp_f.setT(input);

	// This is an example of function overloading. The "=" operator has been specialized for the CTemp object
	temp_c=temp_f;
    

    
    std::cout << "The temperature in Celsius (C) is " << temp_c.getT() << std::endl; // Output the Celsius result

  
    if(temp_f.getT() > Temp::HIGH_TEMP_F_WARNING){ // Check for high temperature 
      std::cout << "Remember to hydrate" << std::endl;
    }
    
    if(temp_f.getT() < Temp::LOW_TEMP_F_WARNING ){ // Check for low temperature
      std::cout << "Remember to pack long underwear"<< std::endl;
    }
  }
}

ftc.cpp

// f2c.cpp 

#include "MyTemperatureConverter.h"


int main(int argc, char* argv[])
{
  // create an instance of the "MyTemperatureConverter" object
  MyTemperatureConverter mytemp; 

  mytemp.run();  // call the "mytemp" object "run" method


  return(0); // exits the program
}

Makefile

CC=g++


f2c:	Temp.o CTemp.o  f2c.o  FTemp.o  MyTemperatureConverter.o  
	$(CC) -o f2c Temp.o CTemp.o  f2c.o  FTemp.o  MyTemperatureConverter.o  

clean:
	rm -f *.o *~ core f2c


2. Compile the program. Follow the specific instructions of your compiler. With the “gnu” compiler, you would type the command:

g++ -o f2c Temp.cpp CTemp.cpp FTemp.cpp MyTemperatureConverter.cpp f2c.cpp

3. Run the program.


ABOUT THE PROGRAM — A WALK THROUGH

To fully understand the program, the reader should probably review the tutorials listed in the “Learn More” section.

To design, document and understand a C++ program, software programmers often use a Unified Modeling Language (UML) class diagram. The image below shows a UML class diagram for this temperature conversion program. The diagram shows the relationship of classes through a visual representation. The layout of graphics mirrors the connections between classes. UML is a standard for documenting Object-Oriented software and is not specific to any programming language. UML contains a number of different diagrams and views in order to capture different aspects of a software design.

umlclassdiagram


TRY THIS

1. Change the temperatures used in the decisions –- change the lower temperature from 60 to 30 degrees, for example. Make sure you change it in two places! Save the file and refresh the browser (or restart the web page), and enter new numbers –- did the answers change at the new temperature?

2. Create a new temperature range from 30 to 60 degrees and have it display -– “Bring hat and gloves!”

3. Change the wording of the phrases.

4. Add another text input -– ask for the wind speed, for example.

5. Add some conditional statements that evaluate the wind chill factor.

6. Add some text to display the wind chill result.

7. Change the base class to store temperature as degrees Kelvin. Change the CTemp and FTemp classes to internally store temperate as degrees Kelvin in the base class.

8. Write the “=” operator in class FTemp that internally converts Celsius to Fahrenheit.


LEARN MORE

There are some good tutorials on the web.

http://www.cplusplus.com/doc/tutorial/

http://www.learncpp.com/

http://www.cprogramming.com/tutorial/c++-tutorial.html

5 Comments on C++

  1. I might work on that

  2. Arnold Swartzingager // March 27, 2015 at 7:27 pm // Reply

    none of these programs worked with the compiler i used

  3. Uhh… How do you get the compiler to run? I am using the MinGW compiler, or I am trying to at least. Also, how do you compile it?

  4. In the Try This section, the note about changing the lower temperature in two places seems incorrect – it’s stored in a constant.

  5. RyZ Encrypted // October 30, 2013 at 12:39 am // Reply

    This is an extremly hard langauge im only between the age of 8-14 but i found it easy but fpr begginners do npt start of with this. Python is the easiest even tho its a 2 out of 5 for me it was way to simple

Leave a Comment

Please don't use your real name.