Introduction to C++, and NetRun

Dr. Lawlor, CS 202, CS, UAF

C++ is a pretty darn interesting language.  It's a 1990's-era extension of plain C, the original 1970's language used by the original UNIX kernel.  So C++ spans all the way from hardcore low-level features like pointers and bit manipulation, all the way up to high-level features like classes and templates.  Out in the real world people also use other languages, in particular Microsoft's C#, Sun's cross-platform Java, and a variety of interpreted languages (Perl, Python, Ruby, etc), but here at UAF, C++ is pretty universal.

C++ definitely has some pretty serious warts:
There's also a fairly universal problem in C++ that the compiler accepts a surprising variety of statements.  I've been writing C++ professionally for a dozen years or so, and I still occasionally come across pieces of code that look like they shouldn't compile at all, but the compiler thinks they're fine!

Style: Just because you CAN, doesn't mean you SHOULD

For example, C++ lets you add two integers like so:
int x=2, y=10;
cout<<"x="<<x<<" and y="<<y<<"\n";

(Try this in NetRun now!)

Surprisingly, because "+y" compiles (it just means positive y), this also compiles:
x=x + +y;

(Try this in NetRun now!)

And of course, "++y" means to pre-increment y:
x=x + ++y;

(Try this in NetRun now!)

Or, you can have a positive pre-increment on y:
x=x + + ++y;

(Try this in NetRun now!)

You can also post-increment x, although now we need to assign to a different variable (here, z):
int z=x++ + + ++y;

(Try this in NetRun now!)

This compiles perfectly, with no warnings, but frankly it looks like utter gibberish.  C++ has quite a few constructs that can be abused this way, sometimes unintentionally, resulting in code that is very difficult to understand or modify.

That's bad.

In this course, I'll try to illustrate some of the ways that C++ can go bad, and how to fix them.  Generally, you should try to make your code as clear as possible--the exact opposite of what's shown above!

NetRun: Compile and Run C++ code from a Web Browser

NetRun is my little web server application to accept C++ code from your web browser, and run that code on my server.  We'll be doing most of the homeworks in NetRun, because it provides you and me with instant grading feedback, and has some useful builtin options like timing and profiling.

Anybody with an "" email address is welcome to sign up to get a NetRun account.  It will email you your password, which you use to log into NetRun.

You can then log in to NetRun.  Your named runs are saved indefinitely on my server, but I don't normally look at these unless you ask, so feel free to try things out--don't worry about cluttering up the server!

Here's the basic NetRun interface:

Run name: <- Make up your own name here to save the code UAF CS NetRun
Code to run: <- Type or paste in arbitrary C++ code in the area below.  This is mode "Inside a function".
return 42;

 ^ Check here to provide text to cin

 ^ Check here to switch languages

Executing Run: netrun/ ./code.exe
  v This is the output of your program.
Program complete. Return 42 (0x2a)
   ^ This means your program returned cleanly.  It prints the return value in decimal and hexadecimal.

Homeworks will appear below your Saved files.  There are also options to download the complete source code, or all of your saved NetRun files.