As we move into more advanced graphics topics, we will require you to write programs in C++. Luckily, you will not need to use any advanced language features (unless you want to) and all of the tools required are available for free. Everything you learned in CS 367 still applies. Java and C++ have many similarities, but also a few key differences.
We will use Microsoft Visual Studio as our IDE for C++ programs. The latest version is Visual Studio 2013, and the CSL computers have version 2010 installed. I have not experienced any compatability issues between the two versions, but that does not mean they are not there.
Getting Visual Studio 2013 for free:
As a student, you have access to Microsoft’s Dreamspark program, which lets you download many different pieces of software for free (including full versions of Windows.) Here’s how to get Visual Studio 2013:
- From the Dreamspark student homepage, click “More Software Through Your School”
- Search for “University of Wisconsin – Madison” and look for the Computer Science Dreamspark store
- Enter your CS login and password to get access to the store.
- Visual Studio 2013 should be listed under the “New” section of the store
- From there you can download any version of Visual Studio 2013, including the Ultimate edition. The “Professional” version is just fine for our needs, and it is what the CSL uses.
Both of these options will download a Microsoft installer program used to actually download and install Visual Studio.
A Basic C++ Project
Once Visual Studio is installed, we can make new C++ projects (or “solutions” as Microsoft calls them.)
- From the start page, click “New Project”
- Select “Win32 Console Application” from the C++ menu. Make sure to check the “Empty Project” box so there is no unnecessary code added.
- To add a new C++ source file, right click the Source Files folder, click add new item, and select C++ source.
- From here you can code anything you want, but we’ll start with Hello World.
Now we can run the code.
- Clicking the little play button at the top will compile and run the project (or use the keyboard shortcut F5.) If all goes well, the console will appear and disappear right away.
- In order to actually see your output, launch the program without debugging using the option from the “DEBUG” menu (or the keyboard shortcut Ctrl-F5).
- If Ctrl-F5 does not work, you will have to change the project settings to explicitly make it a console application:
- Right click the project and go to Properties.
- Navigate to Configuration Properties -> Linker -> System
- Set the SubSystem to “Console”
That’s all for now! If you have specific questions about C++, ask them on Piazza. We will add more advanced C++ tutorials soon.
The C family of programming languages are rivaled only by Java in their popularity. In 2014, the C family (that is C, C++ and C#) had a combined market share of 27.3% to Java’s 19.3% Considering this, you will almost certainly learn one or the other in your college or university, and many software professionals have had successful careers specialising in the C family.
The oldest of the languages, C, was designed by Dennis Ritchie in 1972. It has been a mainstay of operating system and low level application programming for decades. It is what is known as an imperative language, which means a program is made up of functions that execute in a linear progression, with each one changing some state of the program. For example, a line of code may add a number to another in memory, display a certain value on screen, or write a value to a file on disk. Programs are structured with “if” statements which fork the execution path of a program into multiple directions, or loops, which repeat operations until a condition is met. Complex data structures can be implemented, but in a more crude form than in C++ or C#, and the key factor is that the functions always take and manipulate these data structures, rather than exist as a part of them.
C++ moves us away from this paradigm into the Object-Oriented Programming (OOP) world. In OOP, the data structures we once knew are objects capable of their own actions. Commonly encountered examples in university tutorials are things such as a car, which would have methods (the actions) such as driving or changing gears. Instead of having a central program that operates on data structures, the data structures themselves gain some level of independence through what’s known as ‘encapsulation’. This allows program objects to hide their internal workings and instead provide publically accessible interfaces – rather than setting the location on the aforementioned car, we must call the drive function to direct it where to go. This allows the object to keep a consistent internal state and only allow other objects in the system to have as much knowledge about each other as is needed to perform their function, and never enough to break the functionality of another object. Understanding this key aspect which differentiates C++ from C is crucial in becoming confident and competent with C++ and more modern programming languages as a whole.
C# is the last member of the traditional C family of languages (although Apple/iOS-focused Objective C has its place too) and is usually only considered in association with Microsoft’s .Net framework. Syntactically C# can be viewed as just a cleaner version of C++, and although it introduces a lot of modern programming language quirks, it is less of a departure from C++ than C++ was from C. The .Net framework, on the other hand, provides easy to use tools to build Windows applications of all types, including desktop applications, web applications, web services, Windows services and Microsoft Office plugins. This is essentially what drives the market share in C#. The language has been improved significantly to modernise C++ to rival Java, as well. Memory management is no longer the responsibility of the programmer, as the application is run within what’s known as managed execution, which resembles Java’s virtual machine. This attempts to handle one of the most common problems with C and C++ applications, which was improper memory usage leading to applications that become slower and more resource-heavy during their lifecycle.
This is but an overview of one of the largest and most feature-rich programming languages in the field of computer programming, but it touches on some of the most important fundamentals you will have to learn on your journey to becoming a software developer. Indeed, you will encounter most of these concepts again and again regardless of which language you learn, so even if you don’t plan to make a career out of C programming, knowing these core concepts will stand you in good stead.
Your first C# program
It is difficult for a developer to consider writing C# code to any real degree without dipping into the .NET Framework or Mono extensively. As a result, most tutorials and examples you encounter take this coupling for granted and do not really differentiate between the two, and this will be no exception. The following code is for use with .NET 4.6 (the latest version of the framework at the time of writing) but should work on Mono or older versions without too many problems.
First let’s turn our attention to the inevitable “Hello World” application. First off we can make a new solution (this code is targeted at Visual Studio, the dominant IDE for C#), selecting the Console Application template. This gives us a single code file, “Program.cs”, and an application config file called App.config, which we will disregard for now. By default, the following code is given to us:
Anyone familiar with writing C console applications will recognize what they are seeing here. The program class is designed to be the startup class for our application (from the project and solution properties) and the Main() method is our entry point. At execution time, code will come in here, execute to the end of Main() function, and then quit. If you run this application by hitting ‘start’, you will see a console window appear and disappear just as quickly, which means our empty application has executed without error. The first thing we can do is add the following line to our application so that when it has completed, the application waits for us to press a key before exiting. We will leave this line at the end of our application for the duration of this tutorial. Add it, and try running your application again.
Now add the following line above this one, to create our ‘Hello World’ application.
When you run this, you will now see your greeting as text. Let’s examine what’s happening here with both lines of code. Console is a static class provided to us from the System namespace, which we can see at the top of our file (“using System;”). Static classes do not require an instantiated object to execute their functions, and are instead executed solely from any code module that has access to the namespace of your static class. The staticmethods we are using here are ‘WriteLine’ and ‘ReadKey’, which are fairly self-explanatory. WriteLine is passed one argument, which is our text string, “Hello World!” When we run that line of code, the application prints our text to the console, followed by a new-line. The program then waits (also referred to as ‘blocking for input’) at the ‘Console.ReadKey()’ line until the user pushes a key and the program can continue to the end, exiting. As you can see, these two lines actually perform a large number of low-level operations without us ever having to think about them, which is the strength of high level languages from a programming perspective.
Let’s go ahead and customize our input by reading from the console. Above our WriteLine method, we can add another method – ReadLine. ReadLine is like ReadKey, but instead we wait for a user to type several characters and press return, at which point the text they have typed will be returned and we can store it in a variable. This is done with the equals (=) operator. In the following example, we combine variable declaration and assignment into one line after prompting for input. The output of the ReadLine method is subsequently stored in the userName variable which is of type string (as in a sequence of characters, or text). Now, our program looks like this:
However, this program is identical to the following:
This is because declaring a variable and later assigning it is the same as doing both actions on the same line. In fact, the second example is a bad practice, as you may forget to assign your variable to some value before you use it, which will cause your program to crash.
The other part of this example that has changed is the ‘WriteLine’ method, which now is outputting "Hello, " + username. In this instance, the + operation usually associated with adding numbers is combining two strings. As a result, when executed, the value inside ‘username’ is appended to the end of “Hello, “ to create a new string. Try running this program now and experimenting with the output.
One brief word on variable declarations before we move on: You may see other tutorials and subsequent ones in this series use ‘var’ instead of ‘string’ (or any other type like ‘int’). This is because modern versions of C# can save the programmer the job of declaring a variable type and instead let the compiler do that work. As a result, the compiler has access to information about the ‘Console.ReadLine()’ method and can correctly determine that it will return a string, which it substitutes for ‘var’ at compile time. As a result, this is equally valid, and an even better practice:
Having built this basic application you have gained a brief insight into input/output methods and the structure of an application. Once you understand such procedural code and basic variables, it is time to move on to understand object oriented programming in order to make the most of the power of a high level language like C#.
To fulfill our tutoring mission of online education, our college homework help and online tutoring centers are standing by 24/7, ready to assist college students who need homework help with all aspects of C-family programming. Our computer science tutors can help with all your projects, large or small, and we challenge you to find better online C-family programming tutoring anywhere.
Please let us know the date by which you need help from your tutor or the date and time you wish to have an online tutoring session.
Normal response time: Our most experienced, most successful tutors are provided for maximum expertise and reliability.
Fast response time: Used only for emergencies when speed is the single most important factor.
We require your email address so that we can send you an email alert when the tutor responds to your message.
You will get a negotiable price quote with no obligation.