Intro to C++ - Part 2

Last week we went over the basics of configuring your IDE and compiling your first program. I have class this evening, but I wanted to point you to a resource that I will be developing as I go through this class. The CfCode C++ Blog Series is basically a listing of all posts in the series, as well as an example C++ program with what I hope are good comments throughout of what is going on. In today's iteration of this example document, I go over declaring constant variables, using cin and cout to accept input and provide output respectively. I define several data types, and show you how to evaluate expressions and place the result onto the screen. This example document also introduces static_cast to force a result to a specific data type. You can always find the latest version of my example document at http://www.cfcode.net/blog/page.cfm/c-series, or you can browse back through prior posts like this one to see the code as it grows. More tonight after class!

<span class='cc_comment'>/*
   This is an example simple application
   It has no meaning other than to provide
   examples of functions and 'how to' info
*/
</span>


<span class='cc_comment'>/*
   Pre-processor directives include libraries
   of functions to your C++ program. By default
   you can add, subtract, and do a few basic functions.
   For example, adding the cmath include here allows the
   use of power, sine, absolute, and other geeky math bits
*/
</span>
#include <span class='cc_value'>"stdafx.h"</span>
#include <iostream><span class='cc_comment'> // This include allows using cout, cin, and more</span>#include <string><span class='cc_comment'>   // The book didnt say, but I assume this allows the use of the string data type</span>#include <cmath><span class='cc_comment'>   // Math functions</span>
<span class='cc_comment'>/*
   This specifies that commands without a
   specified namespace::cmd are from 'std'
   namespace
*/
</span>
using namespace std;

<span class='cc_comment'>/*
   constants are often written with all upper
   case characters. Constants are declared
   and then never change due to program execution.
   Examples could be the value of pi, a chart of
   tax rates, or other fixed values.
*/
</span>
const double PI = 3.1415927;
const int ADMIN_ID = 1;
const char BLANK = ' ';
const char WILDCARD = '*';

<span class='cc_comment'>/*
   variables are basically the same as constants,
   but they are not necessarily initialized with
   a value, are designed to be changed throughout
   program execution, and can be initialized many
   at once. These are 'simple' variables, as they
   can only contain 1 value at a time.

   You may initialize these variables as you declare
   them if you follow the name with an = value. Initializing
   the variables will make the code take a bit longer,
   but will protect you from accidently using a variable
   before being initialized, which could read a memory
   space that was leftover from earlier in the code
   or even from a different program completely!
*/
</span>
double amountDue = 0, originalInvoice = 0, itemsPurchased = 0;
int counter = 8, iterations = 0;
string fName, lName, address, city = <span class='cc_value'>"Grand Rapids"</span>, zip;<span class='cc_comment'> // notice a few un-initialized variables here</span>
<span class='cc_comment'>/*
   the 'main' method is the default method
   that is executed when the program first
   is run.
*/
</span>
int main()
{
<span class='cc_comment'>   // cout - print a result out to the console</span>   cout << <span class='cc_value'>"This is a literal string of output text"</span> << endl;
   cout << <span class='cc_value'>"The results of 2(5 + 6) = "</span> << 2 * (5+6) << endl;

<span class='cc_comment'>   // static_cast will allow the casting of an</span><span class='cc_comment'>   // expression result to a specified data type</span>   <span class='cc_comment'>/*
       ---- data types ----
Please Note: I have been informed that these values may differ based on
compiler and platform, so please use them for 'ballpark' ideas only!
Check out http://en.wikipedia.org/wiki/Limits.h for great details on data types
   */
</span>
   cout << <span class='cc_value'>"The following lines evaluate 5.5 + 5.25:"</span>
       << endl;
   cout << <span class='cc_value'>"Static cast as int using static_cast<int> "</span>
       << static_cast<int>( 5.5 + 5.25 )
       << endl;
   cout << <span class='cc_value'>"Static cast as double using static_cast<double> "</span>
       << static_cast<double>( 5.5 + 5.25 )
       << endl;

   /*
      This block will assign some values to the variables
      we initialized above before the main method of the
      program. *NOTE* Case Matters! fName <> fname!!
   */
   amountDue = 125.50;
   originalInvoice = 500.75;
   itemsPurchased = 3;

   <span class='cc_comment'>/*
      cin is a standard input method to get data, most
      likely from the keyboard. This is called a stream
      extraction operator. You can get more than one
      value per cin operation. Note the direction of the
      << or >> characters

      ---- escape characters, note the \n in this output ----
      \n = new line, same as endl
      \t = tab
      \b = backspace
      \r = return
      \\ = backslash
      \' = single quote
      \<span class='cc_value'>" = double quote
   */
</span>
   cout << "
</span>Please enter your first, and then your last name,\nseparated by a space:\n<span class='cc_value'>";
   cin >> fName >> lName;
   cout << endl;

<span class='cc_comment'>   // Example of using some of these variables we just assigned values too</span>   cout << "
</span>Hello <span class='cc_value'>"
       << fName + "
</span> <span class='cc_value'>" + lName
       << "
</span>, you have paid <span class='cc_value'>"
       << originalInvoice - amountDue
       << "
</span> and your balance due is "
       << amountDue
       << endl;

<span class='cc_comment'>   // increment and decrement operations will increase</span><span class='cc_comment'>   // or decrease a value by one, often used for loops</span><span class='cc_comment'>   // or counters in a program. Where you place the ++</span><span class='cc_comment'>   // or -- characters determine if the increase or </span><span class='cc_comment'>   // decrease happens before or after evaluation.</span>   ++iterations;
   ++iterations;
   iterations--;

<span class='cc_comment'>   // address has not been initialized, lets try and</span><span class='cc_comment'>   // see what it has in its memory space? Probably</span><span class='cc_comment'>   // nothing, but you never know, so initialized em!</span>   cout << address << endl;


<span class='cc_comment'>   // return will exit this method and, if specified,</span><span class='cc_comment'>   // return a value to the calling function</span>   return 0;
}

0 comments