Mar 182011
 

Function overloading and using templates are extremely useful when trying to not write duplicate methods with the same parameters, but only different types.

//main.cpp
#include <iostream>

using namespace std;

void get2Values  (int &data1, int &data2);
void get2Values  (double &data1, double &data2);

void print2Values  (const int &data1,const int &data2);
void print2Values (const double &data1,const double &data2);

template <class dataType>
void  swapC (dataType &a,  dataType &b);

int main ()
{
   int first, second;
   double dFirst, dSecond;

    get2Values(first, second);
    get2Values(dFirst, dSecond);

    print2Values(first, second);
    print2Values(dFirst, dSecond);

    cout << endl << "--Swap--" ;

    swapC<int>(first, second);
    swapC<double>(dFirst, dSecond);

    print2Values(first, second);
    print2Values(dFirst, dSecond);

	cout << endl;
	cin.get();
	return 0;
}

void  get2Values (double &data1, double &data2)
{
	cout << "Enter Double 1 ---> ";
	cin >> data1;
	cout << "Enter Double 2 ---> ";
	cin >> data2;
}

void  get2Values (int &data1, int &data2)
{
	cout << "Enter int 1 ---> ";
	cin >> data1;
	cout << "Enter int 2 ---> ";
	cin >> data2;
}

void  print2Values (const double &data1,const double &data2)
{
	cout << endl << "Double 1 ---> " << data1;
	cout << endl << "Double 2 ---> " << data2;
}
void  print2Values (const int &data1,const int &data2)
{
	cout << endl << "Int 1 ---> " << data1;
	cout << endl << "Int 2 ---> " << data2;
}

template <class dataType>
void  swapC (dataType &a,  dataType &b)
{
	dataType temp = a;  a = b;  b = temp;
}

The output should be:

Enter int 1 ---> 1
Enter int 2 ---> 2
Enter Double 1 ---> 3.21
Enter Double 2 ---> 8.125647

Int 1 ---> 1
Int 2 ---> 2
Double 1 ---> 3.21
Double 2 ---> 8.12565
--Swap--
Int 1 ---> 2
Int 2 ---> 1
Double 1 ---> 8.12565
Double 2 ---> 3.21

The template function allows you to pass in any value, saving you from creating the two methods above unique to double and int.

Mar 162011
 

I admit this is a terrible example I wrote up a few months ago. You should obviously have a temp variable so you aren’t casting all the time. But the point of the lab was to test user input, in which it performs just fine and is clean.

//main.cpp
#include <iostream>

using namespace std;

int main ()
{
	double purchaseAmount, cashPaid, temp;
	int change;

	cout << "Enter the purchase price: ";
	cin >> purchaseAmount;
	cout << "Enter the cash tendered: ";
	cin >> cashPaid;

	temp = cashPaid - purchaseAmount;
	change = int (temp * 100);

	cout << "Your change: " << change << " cents" << endl;
	int numQuarters = int(change/25);
	cout << "Quarters: " << numQuarters << endl;
	int numDimes  = int((change-numQuarters*25)/10);
	cout << "Dimes: " << numDimes << endl;
	int numNickels = int((change-numQuarters*25-numDimes*10)/5);
	cout << "Nickels: " << numNickels << endl;
	int numPennies = int((change-numQuarters*25-numDimes*10-numNickels*5)/1);
	cout << "Pennies: " << numPennies << endl;

	cin.get();
	return 0;
}

The output should be:

Enter the purchase price: 10.25  //10.25 was inputted
Enter the cash tendered: 20.21  //20.21 was inputted
Your change: 996 cents
Quarters: 39
Dimes: 2
Nickels: 0
Pennies: 1

And yes, I have noticed the divide by one. Its left in there to keep the standard so its easier to see the pattern of calculating the individual change.

Mar 152011
 

Data Types and Sizes

Just a basic look into a few different (basic) data types that C++ operators. Compared to Actionscript, ints are the same (non-decimal numbers). Unsinged integers (or uints in AS3) have the same positive, non-decimal values. Floats, doubles, long doubles are like Numbers in AS3, holding decimal values at increasing precision. Chars aren’t available in AS3, but are the same as single length strings.

//main.cpp

#include <iostream>

using namespace std;

int main ()
{
	int myInt = 1;
	long myLong = 2;
	unsigned int myUnsignedInt = 3;
	char myChar = 'D';
	float myFloat = 5.0;
	double myDouble = 6.00;
	long double myLongDouble = 7.0000000000;
	bool myBool = false;
	int myCharASCII = int(myChar);
	int myRandomInt = 98;

	cout < < "The value of myInt = " << myInt << "          The byte size of myInt is " << sizeof (myInt) << " bytes." << endl;
	cout << "The value of myLong = " << myLong << "         The byte size of myLong is " << sizeof (myLong) << " bytes." << endl;
	cout << "The value of myUnsignedInt = " << myUnsignedInt << "  The byte size of myUnsignedInt is " << sizeof (myUnsignedInt) << " bytes." << endl;
	cout << "The value of myChar = " << myChar << "         The byte size of myChar is " << sizeof (myChar) << " bytes." << endl;
	cout << "The value of myFloat = " << myFloat << "        The byte size of myFloat is " << sizeof (myFloat) << " bytes." << endl;
	cout << "The value of myDouble = " << myDouble << "       The byte size of myDouble is " << sizeof (myDouble) << " bytes." << endl;
	cout << "The value of myLongDouble = " << myLongDouble << "   The byte size of myLongDouble is " << sizeof (myLongDouble) << " bytes." << endl;
	cout << "The value of myBool = " << myBool << "         The byte size of myBool is " << sizeof (myBool) << " bytes." << endl<< endl;
	cout << "Letter = " << myChar << "                  The ASCII position = " << myCharASCII << endl;
	cout << "The ASCII position = " << myRandomInt << "     Letter = " << char(myRandomInt) << endl << endl;

	cin.get();
	return 0;
}

The output should be:
Continue reading »

Mar 142011
 

I’m not a pro. I’m not experienced with this, besides half a year of working with the language. I’d love some comments and improvements on any and all code I put online. I currently work with the really old Bloodshed Software’s Dev-C++ (Version 4.9.8.0) with its Mingw compiler.

I’ll assume that you’ve already gotten the program set up and have a blank project open.

//main.cpp

#include <iostream>

using namespace std;

int main ()
{
	cout << "Name - Mr. Cake" << endl;
	cout << "Grade in School - 9000th" << endl << endl;
	cout << "Period      Course" << endl << endl;
	cout << "     1      Math" << endl;
	cout << "     2      Computer Science" << endl;
	cout << "     3      Science" << endl;
	cout << "     4      History" << endl;
	cout << "     5      Language Arts" << endl;
	cout << "     6      Sports" << endl << endl;
	cout << "Computer at home - Windows 7" << endl;

	cin.get();
	return 0;
}

The output should be:

Name - Mr. Cake
Grade in School - 9000th

Period      Course

     1      Math
     2      Computer Science
     3      Science
     4      History
     5      Language Arts
     6      Sports

Computer at home - Windows 7