C Fundamentals

C++ Fundamentals

C++ is a programming language. As such, it shares a number of similarities with other programming languages. First we may need to clarify what a programming language is. A computer thinks in 1’s and 0’s. Various switches are either on (1’s), or off (0’s). Most humans, however, have trouble thinking in 1’s and 0’s. A programming language is a language that provides a bridge between the computer and human beings. A “low-level” language is one that is closer to a computer’s thinking than to a human language. A prime example would be Assembly language. A “high-level” language is one that is closer to human language. COBOL and BASIC are prime examples of high-level language. Many people consider C and C++ to be high-level languages, but actually C and C++ are both somewhat of a bridge between the low-level languages and high-level languages. You might think of them as mid-level languages. The level of a language, in this context, essentially refers to how far it is removed from actual machine language. It has no relationship to either the power of the programming language, or the difficulty of learning and using it. Various languages, with their basic level indicated, are shown in Figure 1.2.

click to expand
Figure 1.2: Programming languages.

Each programming language has various strengths and weaknesses. Some, like BASIC, are easy to use but are neither flexible nor powerful. Others, such as Assembly, are powerful, but difficult to use. C and C++ are somewhere in the middle. C++ is quite powerful, but relatively easy to use (easier than Assembly but more difficult than BASIC). Some languages are also written with specific purposes in mind. Fortran was written specifically for mathematical programming, BASIC was designed simply for teaching programming, and COBOL was designed for business applications. C++ was designed as a general purpose language. It is used in business applications, telecommunications programming, artificial intelligence programming, games programming, and much more. This is one reason why it is the language that many teachers choose to teach students. Once you have learned C++, you can use it in a wide variety of situations.

Programs are written to handle data. This is why the industry as a whole is often referred to as data processing, information technology, computer information systems, and so on. That data might be information about employees, parts to a mathematical computation, scientific data, or even the elements of a game. No matter what programming language or techniques you use, the ultimate goal of programming is to store, manipulate, and retrieve data. Data must be temporarily stored in the program, in order to be manipulated. This is accomplished via variables. A variable is simply a place in memory set aside to hold data of a particular type. It is a specific section of the computer’s memory that has been reserved to hold some data. It is called a variable because its value or content can vary. When you create a variable, you are actually setting aside a small piece of memory for storage purposes. The name you give the variable is actually a label for that address in memory. For example, you might declare a variable in the following manner.

int j;

Then, you have just allocated four bytes of memory; you are using the variable j to refer to those four bytes of memory. You are also stating that the only type of data that j will hold, is whole numbers. (The int is a data type that refers to integers, or whole numbers.) Now, whenever you reference j in your code, you are actually referencing the contents being stored at a specific address in memory.

With that said, you might now be asking what is meant by “data of a particular type”? Data comes in different types. Some data consists of letters and some consists of numbers. Any programming language will recognize only certain types or categories of data. The basic data types that C++ recognizes, and what they hold, are shown in Table 1.1. In addition, there are a few other data types that will be introduced later in the book.

Table 1.1: Data Types

Data Types

Values stored


This value is for whole numbers, or integers. The size depends on the operating systems. In 32-bit operating systems, the int is usually 32 bits (4 bytes) in length.


This data type holds larger whole numbers


Floats are used to hold decimal numbers such as 2.02798


A double is simply a really big float.


A char can store a single alpha-numeric type. In other words, it can store a number or a letter.


Bool is short for Boolean, a reference to Boolean logic. Therefore, this data type can only store true or false.


This is basically a small integer. Usually 2 bytes in length. But the actually size will depend on the operating system. On 32-bit operating systems such as like Windows 98, 2000, NT, and XP, a short will be 2 bytes long.

When you are deciding what type of variable to create, you need to think about what kind of data you might wish to store in that variable. If you want to store a person’s age, then an int is a good choice. The int data type will hold whole numbers, and will, in fact, hold numbers much larger than you might need for a person’s age. The long data type will also hold whole numbers, but it is designed for even larger whole numbers. If you wished to store bank balances, grade point averages, or perhaps temperatures, you would need to consider using a float. The float data type holds decimal values. However if you were storing astronomical data, you would still want to hold decimals, but because they might be quite large, you would have to consider storing them in doubles. Picking what data type to use is actually simple. Just give some thought to the type of data you intend to store in a variable.

In addition to considering what type of variable you will use, you must also pay attention to how you name the variable. Only certain names are allowed in C++. Variable names must begin with a letter or underscore and may contain any combination of upper/lower case characters, digits, and underscores. Variable names can neither begin with a number nor contain certain symbols such as #, &, *, and so on. Here are some examples of valid and invalid variable names (Table 1.2).

Table 1.2: Valid Variable Names

Valid Variable Names

Invalid Variable Names









In addition, many of the symbols that C++ does not allow you to use in naming your variables actually represent something important in the C++ programming language. This is one reason why such symbols are not allowed in variable names. Also, please understand that simply because a name is valid in C++ does not make it a good name. It is important that your variable names bear some resemblance to the value they hold. The compiler will certainly let you compile your code without this, but it will make your code much more difficult for other programmers to understand. If your variable holds an account number, please don’t name the variable x or i. Instead, give it a name such as account_number or acctnum.

Some programmers go further and add a notation at the beginning of the variable name to indicate what type of variable it is. For example, an account number that is an int might be called i_accountnum or intAccountNum. The following table shows common prefixes used for various data types. Not everyone follows these naming conventions. Nevertheless, they are common enough that you should be aware of them. They are summarized in Table 1.3.

Table 1.3: Naming Conventions

Variable Type

Common Naming Conventions


iaccountnumber, i_accountnum, intaccountnum


fbalance, f_balance, flt_balance


lnumber, l_number, lngnumber


b_isempty, bool_isempty


c_value, chrValue

There is a plethora of ways to name variables; no way is right or wrong. The thing to keep in mind is this: Does your personal naming convention make it easy for others to read your code? Does your variable’s name clearly identify what type of variable it is and/or what data it will hold? If you can answer yes, then your naming convention is fine.

Now that you know how to name variables, let’s give you a few examples.

int int_account_num; boolean bturnedon; float fltsalary;

Notice that these examples all obey the aforementioned rules for naming variables. You might, however, be curious about the semicolon at the end of each line. Each statement in C++ (as well as C, Sun Java, and several other programming languages) ends with a semicolon. The semicolon basically tells the compiler that you are done with that particular line of code, and that the things you have written prior to that semicolon represent one single, concise statement. A statement is simply a single line of code that performs some action. Many programmers also use the term expression when referring to a statement. The two terms, expression and statement, are interchangeable.

You can declare more than one variable on a single line. All the variables declared in that statement will be of the type you declared at the beginning of the statement.

int i_account_num, i_age, int_years_w_company;

All three variables are of type int.


This works the same way in many other programming languages such as Java and C, but does NOT work this way in Visual Basic™.


C++ is case sensitive. Uppercase letters and lowercase letters are treated as two different letters. An A is not the same as an a. That means that int main() and int Main() are not the same thing. One of the most common mistakes beginners make is forgetting the case-sensitive nature of C++.

You can choose to initialize your variable to some default value when you create it. The following examples illustrate this.

int num = 0; float number = 0.0;

A default value is simply some starting value that the variable will hold, by default, if no other value is placed into it. For example, if your program was storing data about people who recently received a high school diploma, you might wish to use a default value of 18, because that is the most probable age of a recent high school graduate.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net