Home

Introduction to C++ Projects

   

Introduction to Programming

 

Introduction to Computers Instructions

 

A computer is an electronic device that receives instructions to perform one or more assignments. An assignment can be as simple as displaying a character or as complex as predicting the weather. To process the assignments, the instructions must be given in a language the computer can understand. The language the computer uses is called a machine language.

While our English language is made of various letters and symbols, the language of the computer is made of two characters: 1 and 0. Using different combinations of 1s and 0s, a person can formulate any type of instruction. Here is an example:

1000110010100000

This instruction is asking the computer to add two numbers. You may be saying, "So what? This is easy". Maybe so. Imagine you have to give instructions to open two files, merge them, and send the newly merged file to a network drive.

Because a machine language speaks directly to the hardware of the device, it is referred to as lower-level language.

Just like people speak different languages, there is not one single machine language: A machine language that one computer understands is not the same machine language that another computer understands. Put it another way: The instructions given to one type of computer to perform an assignment are not the same instructions given to another type of computer to perform the same operation. This complicates matters.

To make it a little easier to create instructions for a computer, a language named Assembly was created. Assembly instructions are given to a program named an assembler. Its job is to takes instructions written in human readable words and convert them into machine language. Assembly instructions are a little easier to understand. Here is an example from the above line:

add A, B

To perform its assignments, an assembler uses the memory of the computer to load, store, and move instructions, values, and other items. This means that, to write instructions in Assembly, you must be familiar with the memory or the type of memory the computer uses. Because Assembly instructions are given directly to the computer memory, its language also qualifies as lower-level.

Since there are different types of computers, there are also various kinds of memory. Consequently, there are multiple ways to store instructions. Because of the hassle of studying memory and its many details, assembly instructions can be difficult to create.

To avoid the difficulties of spending time to study and use the computer memory, other languages exist. These languages use a program called a compiler. A compiler receives instructions in a humanly readable language. The compiler translates the instructions to Assembly. That way, the person writing the instruction doesn't have to know Assembly or the machine language. Because the instructions are written in regular English, their language is referred to as higher-level.

Computer programming consists of creating instructions, using an appropriate language.

Introduction to C

C is a higher level language used to give instructions to the computer. C is an international language used on different types of computers. Because it is a neutral language, the instructions written in C can be given to various types of computers. The C language is so powerful it can be used to create an operating system such as Microsoft Windows.

To make it possible to create programs for the Microsoft Windows family of operating systems, Microsoft developed its own version of the C language. A computer programmer, also called an application programmer, also called a programmer, is a person who creates computer programs or computer applications.

Introduction to Win32

Microsoft Windows is the most commonly used operating system. In its early releases (Microsoft Windows 3.x), it was difficult to create programs for it. To alleviate this task, Microsoft developed a library named Win32. This is a series of objects and tools used to communicate with the operating system. Throughout our lessons, we will get acquainted with the Win32 library and what it offers.

Introduction to C++

C is a very powerful language. For example, it can be used to create objects and manipulate them. It can even be used to communicate with the computer memory. Still, there are things that are not available in C. An example is the ability to give an object the ability to perform its own actions. To get such features, another language was derived from C. This language is called C++. As we will learn throughout our lessons, everything available in C is also available in C++, plus more.

To create instructions for your program, you will use easily recognizable words of the English language. Of course, you must follow some (strict) rules. The instructions are written as text in a normal file. Each instruction in C and C++ must end with a semi-colon.

 

Introduction to MFC

The Microsoft Window operating system was primarily written in C. As a consequence, the Win32 library was written in C. This means that in the beginning, to create applications for Microsoft Windows, you had to know C. One problem was that C was difficult. Another problem was that not everybody liked C and not everybody wanted to use C. In fact, many other languages such as Pascal were developed and could as well be used in Microsoft Windows. Microsoft created a language named Visual Basic. With the advent of C++, there were people who wanted to use the features of the new and powerful language. In light of the difficulties of using the Win32 language, the limitations of C, the modern features and the powers of C++, Microsoft created a new library named Microsoft Foundation Classes library (MFC).

The Microsoft Foundation Classes library, or MFC, is a custom implementation of the Win32 library. The MFC offers everything available in Win32 but using the C++ language. Throughout our lessons, we will learn the features that the MFC has to offer.

As its name indicates, the MFC is just a library. It is not a language. To provide an environment where the MFC can be used, Microsoft created the Microsoft Visual C++ programming environment.

A C++ Application

As a computer language, C has its own ways of giving instructions to the computer, so does C++. To make it possible to create these instructions, each language ships with its own library. These libraries are already created and ready to be used. In our lessons, we will use many libraries, including Win32, MFC, C, and C++.

The primary library in C is called stdio, which stands for standard input/output. If you want to use it, you must include it in your application. In C and C++, a library is represented by a file called a header file. A header file has the extension .h. Therefore, to include a header file, type #include followed by the name of the file between < and >. For example, to include stdio, you would type:

#include <stdio.h>

The name of the library is not case sensitive. This means that you can write it as STDIO.H without any problem.

The primary library in C++ is called iostream, which stands for input/output stream. In C++, most header files are included through a namespace. In future lessons, we will study what a namespace is. The primary namespace in C++ is called std. To include the iostream library and to use the std namespace in your program, you would type:

#include <iostream.h>
using namespace std;

The words using and namespace will be explained in future lessons. For now, simply use them like that.

#include <iostream>
using namespace std;

To make it possible to create Windows applications, the Win32 library provides a header file named window.h. Therefore, if you want to use anything that is part of Win32, make sure you include windows.h. This can be done as follows:

#include <windows.h>

Of course, you can add it to a file that already includes stdio or iostream. Here is an example:

#include <iostream>
#include <Windows.h>
using namespace std;

To give you the ability to support MFC in your application, the MFC library provides a header file named afxwin.h. If you want your application to use MFC features, make sure you include this library. Here is an example:

#include <iostream>
#include <afxwin.h>
using namespace std;

"Microsoft Specific"

The compiler that ships with Microsoft Visual C++ is called cl.exe. That compiler can be used to build C and C++ applications. This compiler is not particularly easy to use. For this reason, for all the instructions in our lessons, we will use Microsoft Visual C++ Express and Microsoft Visual Studio.

These lessons are intended to teach how to use the C and the C++ languages to create console applications on Microsoft Windows. To make it possible, Microsoft created its own implementation of the languages. Although C and C++ are international languages and Microsoft Visual Studio strives to follow the standards as faithfully as possible, there are some sections in our lessons that will conform to Microsoft's implementation.

To support both C and C++, Microsoft released a version named Microsoft C/C++. It means the same compiler is used to create both C and C++ applications as the languages are implemented in Microsoft Visual C++. It is important to know that these lessons serve as an introduction to graphical applications that would be created using Microsoft Visual C++. There are words and expressions we will use in our lessons:

  • When the letter C or the expression "C language" is used, it refers to an issue that is unique or proper to the C language
  • The expression C++ or "C++ language" refers to an issue that is unique or proper to the C++ language
  • C/C++: There is no language named C/C++ but we will sometimes use the expressions "C/C++" or "C/C++ language" in our lessons. The notation C/C++ or the expression "C/C++ language" will refer to an issue that is common to both C and C++. It is usually a feature that C++ inherited from its parent language, C
  • The name Microsoft Visual C++ will refer to the way the programming environment is used either in Microsoft Visual C++ Express or in Microsoft Visual Studio. As you will find out, even though Microsoft Visual Basic, Microsoft Visual C#, Microsoft Visual F#, and Microsoft Visual C++ share the same programming environment, when you start creating an application and you choose a language, the programming environment automatically adapt to that language. Some things that are available to one language are not available to another
  • Microsoft C/C++: As mentioned already, this is Microsoft's implementation of both the C and the C++ languages as they are available in Microsoft Visual C++. Therefore, the expression "Microsoft C/C++" in our lessons will refer to common issues of both languages as they are used in Microsoft Visual C++
  • The abbreviation MFC will refer to the Microsoft Foundation Classes library
  • The name Win32 will refer to that library

In the documentation of the C and the C++ languages on the Microsoft web site, you may see a section stating "Microsoft specific". This would describe an issue that is not necessarily part of the international standards but the description would give information about how the language is used in Microsoft C/C++. Because these lessons are explicitly targeting both the MFC and Microsoft Windows, we will not signal  when an issue is "Microsoft specific". You should just assume that, because these lessons serve as an introduction to MFC, everything, or almost, is always "Microsoft specific".

Microsoft Visual C++ Projects

 

Introduction

In this series of lessons, we will study both the C and the C++ languages as they are used in the Microsoft Visual C++. We only want to study the languages. We will not learn how to create graphical applications. Instead, the applications we will learn show their results in a black window called the DOS window or the Command Prompt. These are called console applications, because they are traditionally executed at the console.

To create a console application, after starting Microsoft Visual C++ Express Edition or Microsoft Visual Studio:

  • In the start page, you can click New Project
  • On the main menu, you can click File -> New Project...
  • On the Standard toolbar, you can click the New Project button
  • You can press Ctrl + N

Any of these actions would display the New Project dialog box. From it, you can click either Win32 Console Application, Win32 Project, or Empty Project. You must give a name and location to the project. When you click OK, if you had selected Empty Project, you would be taken to an empty project. If you had selected either Win32 Console Application or Win32 Project, a wizard would come up. The first page of the wizard only presents some text. Read it and click OK. If you had started the project with Win32 Project, the Windows Application radio button will be selected:

Win32 Application Wizard

If you want to create a console application, you should click the Console Application radio button. If you want to create and configure everything from scratch, you should click the Empty Project check box. If you want the wizard the add the necessary header files of the C and C++ languages, click the Precompiled Header check box. If you want your application to use the MFC library, you should click the MFC check box. Once you are ready, click Finish.

Practical LearningPractical Learning: Starting a Project

  1. Start Microsoft Visual C++ Express or Microsoft Visual Studio
  2. To create an application, on the main menu, click File -> New Project...
  3. In the middle list, click Empty Project
  4. In the Name box, type gdcs1 (which stands for Georgetown Dry Cleaning Services 1):
    New Project
  5. Accept or change the path in the Location and click OK

Source Files

As mentioned already, a computer must receive instructions to do its job. There are various ways these instructions can be given. The primary means is through a Windows file. The C/C++ language uses two primary types of files to give instructions: header files and sources files. The primary types of files in C/C++ are called source files. These files contain the code of a program. A source file is just a normal computer file that contains text.

To create a source file in Microsoft Visual C++:

  • On the main menu, click Project -> Add New Item...
  • In the Solution Explorer (we will come back to the Solution Explorer), right-click either the name of the project or any folder (Header Files, Resource Files, or Source Files), position the mouse on Add, and click New Item...

Any of these actions would display the Add New Item dialog box:

  • If you want to create a C++ source file, in the middle list, click C++ File (.cpp) and give a name to the file. You don't have to specify an extension. The .cpp extension will be automatically given when you click OK
  • If you want to create a C source file, you can click anything in the middle list. In the Name text box, type a name for the file and (explicitly) add the .c  extension

When you are ready, click Add.

Practical LearningPractical Learning: Starting a Project

  1. On the main menu, click Project -> Add New Item
  2. In the middle list, click C++ File (.cpp)
  3. Select the text in the Name text box and replace it with CleaningOrder
    Add New Item
  4. Click Add
  5. Based on what we have seen so far, type:
    #include <iostream>
    using namespace std;

The main Entry Point to an Application

We saw that, in Assembly, an assignment is formulated as an instruction that is passed to the computer memory. In C and C++, an instruction is called a function. A function must have a name. There are two categories of functions you will use in your programs. You will create some functions yourself and your will use many functions that have already been created and ready to be used. For example in C, an example of a function you can use is called getchar. The name of a function is always followed by parentheses. An example would be getchar().

An application must have a section where its instructions start and where they end. This section is called the entry point to the application. In the C/C++ language, such a section is represented by a function named main. In Microsoft C/C++, this function can also be used as _tmain. Remember that the name of a function must be followed be parentheses: this would be main() or _tmain().

Whenever you create your own function, after its parentheses, it must be followed by an opening curly bracket "{" and a closing curly bracket "}":

main(){}

To distinguish a function from the other types of things you will be using in your programs, a function's name is followed by an opening and a closing parentheses. For example, the main function will always be written at least as main(). When we perform a better study of functions, we will learn more about functions, their parentheses, and other related issues.

When a program is written and you ask the computer to "execute" it, the first thing to look for is the main() function. This means that every C++ program should have the main() function. Because a function is an assignment, in order to perform its job, a function has a body; this is where the behavior (assignment) of the function would be "described". The body of a function starts with Everything in between belongs to, or is part of, the function. Therefore, the main() function can be written as:

main() {}

The section between the curly brackets is referred to as the body of the function.

Because a function carries an assignment, it must specify the type of value it will produce when it has finished. The type of value a function is specified on the left side of the name of the function. If the function will not produce a value, its type is considered void. Here is an example of using void on main:

void main() {}

Because the body of a function usually carries many assignments, the curly brackets are usually written in different lines.

Practical LearningPractical Learning: Specifying a Project's Entry Point

  1. Change the document as follows:
    #include <iostream>
    using namespace std;
    
    void main()
    {
    }
  2. Save the file

Managing Files

 

Introduction

In Microsoft Visual C++ 2010 Express and in Microsoft Visual Studio, when a project is made of various files, each file is represented by a label in the top section of the Code Editor. Here are examples:

When a project is made of various files, each file is represented by a tab in the top section of the Code Editor

Each file is also represented in the main menu under Windows.

The Solution Explorer

The Solution Explorer is a window that displays a list of the files that make up a project. To access the Solution Explorer:

  • If the Solution Explorer is not yet showing on the screen:
    • On the main menu, click View -> Solution Explorer
    • On the Standard toolbar, click the Solution Explorer button Solution Explorer
  • If the Solution Explorer is already showing, click its tab

The Solution Explorer is made of four sections. Like every regular window, the Solution Explorer is equipped with a title bar that displays its name on the left side and three buttons on the right side:

  • The Window Position button Window Position displays a menu when you click it:
     
    Window Position
     
    The Float option is enabled if the window is docked to a side of the screen. The Float option is disabled if the window is already floating. If the window is docked and you click Float, it would be moved from its docked position and would float. As an alternative to float a docked window, drag its title bar away from its docked position. To dock a floating  window, drag its title bar

Under its title bar, the second section of the Solution Explorer is a toolbar:Toolbar:

  • The Properties button allows you to display the property pages of the current project
  • The Show All Files button is used to show the hidden files of the project
  • The View Code button is used to show the code of a class

The third part of the Solution Explorer is its body. It shows the folders, files, and resources that are part of the current project. To expand a node, you can either click its button or double-click its name. To collapse a node, either click its button or double-click it.

The root of the list is the name of the solution. Under the root is the name of the current project. If the solution contains more than one project, the name of each project is represented under the solution. Inside of the project are its folders, files, and resources. The first item under a project name is References. After the References node, there are the names of the classes that are part of the project.

The fourth part of the Solution Explorer is its tab.

The Properties Window

The Properties window shows the Windows operating system's details of the files or resources used in a project. To display it:

  • If the Properties window is not yet displaying
    • On the main menu, click View -> Properties window
    • On the Standard toolbar, click the Properties button Properties
  • If the Properties window is displaying already, click its tab

The display and rectangular behavior of the Properties window follows the description we had for the Solution Explorer. To show the operating system's characteristics of a project or a file, in the Solution Explorer, click the object:

  • If you click a solution, the Properties window would show its name and its location
  • If you click a project, the Properties window would show its project file
  • If you click a file, the Properties window would show its name (in the File Name field) and its location (in the Full Path) field

The Properties window displays different fields depending on the item selected in the Solution Explorer. You can change some things in the Properties window. When a field is disabled, it means you cannot modify it.

Accessing or Opening a File

If a file is a member of the current project, to open it:

  • On the main menu, click Window and click the name of the file
  • In the top section of the Code Editor, click the label that holds the name of the file
  • In the Solution Explorer, double-click the name of the file

If you are using Microsoft Visual C++ 2010 Express or Microsoft Visual Studio, to open a file:

  • On the main menu, click File -> Open File...
  • On the Standard toolbar, click the Open File button Open File

Any of these actions would display the Open File dialog box. From there, locate the file from its folder and click it. When you click Open, Microsoft Visual Studio will try to open the file. If it can, it would display it inside its interface. If it cannot, it would start looking for a program in the computer that can open the file and hand the job to it. That application would then decide whether it can open the file or not. For example, if you try opening a video (a file with AVI, MOD or another video extension), Microsoft Visual Studio would launch the Windows Media Player that would then try to play the video.

Besides the Code Editor, the integrated development interface (IDE) of the Microsoft Visual C++ 2010 is made of various parts, which we will review when necessary.

Building a Project

After creating a project and adding code to it, to see what it performs, you must build the program. That is, you must submit the instructions to the compiler. To build a project:

  • On the main menu, you can click Build -> Build Solution
  • In the Solution Explorer, right-click the name of the project and click Build

After building a project, if there are problems, for example if there is an error in the code, the compiler would signal it by Microsoft Visual Studio displaying an error.

Executing an Application

When a project has been built and if there is no problem, the compiler creates a resulting application called an executable. You can distribute that executable to people who need to use your application. To use your program, it must be executed. There are various ways an application can be executed. For example, at Command Prompt, a user can type the name of the program and press Enter.

If you are creating your project in Microsoft Visual C++, the studio provides an easier way to both build and execute the project in one step. To do this, on the main menu, you can click Debug -> Start Debugging.

 
 
 

Introduction to Value Input/Output

 

Value Output

Probably the most fundamental job of an application is to display something, a name, a number, a sentence, etc. To support this, the C language provides a function named printf_s (which stands for Print Formatted String). To use it add a character, a word, or a sentence in its parentheses. Here is an example:

#include <iostream>
using namespace std;

void main()
{
    printf_s("C/C++ Programming.");
}

To support the ability to write something on the screen, the C++ language provides an operator named cout (pronounce see - out) (which stands for Console Output). The cout word is followed by the extraction operator <<, then some simple rules to display anything. For example, to display a word or a sentence, you include it in double quotes " and ". Here is an example:

#include <iostream>
using namespace std;

void main()
{
    cout << "C++ Computer Application";
}

Practical LearningPractical Learning: Printing to the Screen

  1. Change the document as follows:
    #include <iostream>
    using namespace std;
    
    void main()
    {
        printf_s("Georgetown Dry Cleaning Services");
    
        getchar();
    }
  2. To execute the application, on the main menu, click Debug -> Start Debugging.
    If a message box comes up and ask whether you would like to build it, click Yes
  3. Press Enter to close the DOS window and return to your programming environment

Commenting Your Code

A comment is word, a line, or a section that the compiler will not consider when building the program. In a comment, you can include anything you want. You can create a comment manually or Microsoft Visual Studio can assist you with adding or removing comments.

Microsoft C/C++ supports two types of comments: line-based and section-based. To create a comment on a line:

  • Type // and type what you want. Anything on the right side of // is considererd a comment and will not not be processed by the compiler
  • Click anything on that line. On the Standard toolbar, click the Comment Out The Selected Lines button Comment Out The Selected Lines

To remove a comment:

  • Delete the // symbols on the line
  • Click anything on the line. On the Standard toolbar, click the Uncomment the selected lines button Uncomment the selected lines

Instead of commenting everything on a line, you may want to include only a section of that line or part of that line and the following line. Another type of comment in C++ includes one line or many lines. To create a comment on one line or a section of a line:

  • Click the starting point, type /*, include anything that must be in the comment, and end the section with */. Use the same technique to spread the comment on more than one line as long as the lines are adjacent
  • Select the section that must be included in the comment. The section can start from the middle of one line and continue to the next line or to a section on another line. On the Standard toolbar, click the Comment Out The Selected Lines button Comment Out The Selected Lines

Anything between /* and */ is part of the comment. We will see many examples of all types of comments in various lessons.

Code Colors

Code is written in a wide area with a white background. This is the area you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text.

The colors used are highly customizable. To change the colors, on the main menu, you can click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one:

Options

In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custom button to display the Color dialog box that allows you to "create" a color.

Indentation 

Indentation is another feature that makes your program easy to read. Indentation is a technique of grouping lines of code by category. To delimit the items of your code, you should indent them by two empty spaces or one tab. Indentation should be incremental. That is, when a line of code appears to be a child of the previous line, the new line should be indented.

An MFC Application

 

Introduction

The purpose of this series of lessons is to introduce you to the MFC library, to prepare you to create graphical applications for the Microsoft Windows family of operating systems. Unlike C and C++, the MFC library is not directly available when you create a console application. You must explicitly add it. There are various ways you can do this.

Creating an MFC Application

You can create an MFC application from scratch. You can create an empty project and then configure it for MFC. Or you can ask the wizard to work for you behind the scenes.

To easily create an MFC application using Microsoft Visual C++, on the main menu, click File -> New Project... In the New Project dialog box, click Win32 Console Application. Give a name and a location. Click OK. In the first page of the wizard, click Next. In the second page of the wizard, make sure Console Application is selected. Click the MFC check box:

Win32 Application Wizard

Click Finish.

We also saw that you could create an empty project. If you do and you want to add support for MFC, there are a few things you must do. You must first access the properties of the application. To do that:

  • On the main menu, click Project -> ProjectName Properties...
  • In the Solution Explorer, right-click the name of the project and click Properties

In the Properties, click the combo box Use Of MFC and select Use MFC in a Shared DLL.

Practical LearningPractical Learning: Creating an MFC Application

  1. The gdcs1 project should still be opened.
    On the main menu, click Project -> gdcs1 Properties...
  2. In the left section, click Configuration Properties (or double-click it to expand; then, under Configuration Properties, click General).
    In the right list, click Use of MFC to reveal its combo box
  3. Click the arrow of the Use of MFC combo box and select Use MFC in a Shared DLL
    Properties
  4. Click OK
  5. Change the file as follows:
    #include <iostream>
    #include <Windows.h>
    using namespace std;
    
    void main()
    {
        printf_s("Georgetown Dry Cleaning Services");
    
        getchar();
    }
  6. To execute the application, on the main menu, click Debug -> Start Debugging
  7. Press Enter to close the DOS window and return to your programming environment
  8. To start a new application, on the main menu, click File -> New Project...
  9. In the middle list, click Win32 Console Application
  10. Change the name to gdcs2 and click OK
  11. In the first page of the wizard, click Next

    Win32 Application Wizard
  12. In the second page of the Win32 Application Wizard - gdcs2, make sure Console Application is selected. Click Precompiled Header (or make sure it is clicked). Click the MFC check box
    Win32 Application Wizard
  13. Click Finish
  14. Change the file that was created as follows:
    // gdcs2.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "gdcs2.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    // The one and only application object
    
    CWinApp theApp;
    
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    	int nRetCode = 0;
    
    	HMODULE hModule = ::GetModuleHandle(NULL);
    
    	if (hModule != NULL)
    	{
    		// initialize MFC and print and error on failure
    		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
    		{
    			// TODO: change error code to suit your needs
    			_tprintf(_T("Fatal Error: MFC initialization failed\n"));
    			nRetCode = 1;
    		}
    		else
    		{
    			// TODO: code your application's behavior here.
    			printf_s("Georgetown Dry Cleaning Services - ");
    			cout << "Processing Customers Orders - ";
    		}
    	}
    	else
    	{
    		// TODO: change error code to suit your needs
    		_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
    		nRetCode = 1;
    	}
    
    	return nRetCode;
    }
  15. To execute the application, on the main menu, click Debug -> Start Debugging
  16. Press Enter to close the DOS window and return to your programming environment
 
 
   
 

Previous Copyright © 2010 FunctionX, Inc. Next