Programming

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 Celisus (C) is " << temp_c.getT() << std::endl; // Output the Celcius 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

Comments about “C++”

  1. n00b says:

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

  2. RyZ Encrypted says:

    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

Write a comment about “C++”

Nickname:

Type your comment:

  • 1. Safety. Do the following:
      a. Show your counselor your current, up-to-date Cyber Chip.
      b. Discuss first aid and prevention for the types of injuries or illnesses that could occur during programming activities, including repetitive stress injuries and eyestrain.

    2. History. Do the following:
      a. Give a brief history of programming, including at least three milestones related to the advancement or development of programming.
      b. Describe the evolution of programming methods and how they have improved over time.

    3. General knowledge. Do the following:
      a. Create a list of 10 popular programming languages in use today and describe which industry or industries they are primarily used in and why.
      b. Describe three different programmed devices you rely on every day.

    4. Intellectual property. Do the following:
      a. Explain how software patents and copyrights protect a programmer.
      b. Describe the difference between licensing and owning software.
      c. Describe the differences between freeware, open source, and commercial software, and why it is important to respect the terms of use of each.

    5. Projects. Do the following:
      a. With your counselor’s approval, choose a sample program. Then, as a minimum, modify the code or add a function or subprogram to it. Debug and demonstrate the modified program to your counselor.
      b. With your counselor’s approval, choose a second programming language and development environment, different from those used for requirement 5a and in a different industry from 5a. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      c. With your counselor’s approval, choose a third programming language and development environment, different from those used for requirements 5a and 5b and in a different industry from 5a or 5b. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      d. Explain how the programs you wrote for requirements 5a, 5b, and 5c process inputs, how they make decisions based on those inputs, and how they provide outputs based on the decision making.

    6. Careers. Find out about three career opportunities in programming. Pick one and find out the education, training, and experience required. Discuss this with your counselor and explain why this career might be of interest to you.

  • Earning the Cyber Chip can help you learn how to stay safe while you are online and using social networks or the latest electronic gadgets.

    Topics include cell phone use, texting, blogging, gaming, cyberbullying, and identity theft. Find out more about the Cyber Chip at www.scouting.org/cyberchip.