Learn Online Program
Today Use Tomarrow Use

C/C++ » C++ Program

Introducing C++


C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980's, and is based on the C language. The name is a pun - "++" is a syntactic construct used in C (to increment a variable), and C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled (i.e. converted into a series of low-level instructions that the computer can execute directly) using a C++ compiler.

C is in many ways hard to categorise. Compared to assembly language it is high-level, but it nevertheless includes many low-level facilities to directly manipulate the computer's memory. It is therefore an excellent language for writing efficient "systems" programs. But for other types of programs, C code can be hard to understand, and C programs can therefore be particularly prone to certain types of error. The extra object-oriented facilities in C++ are partly included to overcome these shortcomings.

Variables, Types and Expressions

  1. Identifiers
    • Reserved Words. These are words such as if, int and else, which have a predefined meaning that cannot be changed. Here's a more complete list.
    • Library Identifiers. These words are supplied default meanings by the programming environment, and should only have their meanings changed if the programmer has strong reasons for doing so. Examples are cin, cout and sqrt (square root).
    • Programmer-supplied Identifiers. These words are "created" by the programmer, and are typically variable names, such as year_now and another_age.

    An identifier cannot be any sequence of symbols. A valid identifier must start with a letter of the alphabet or an underscore ("_") and must consist only of letters, digits, and underscores.


Data Types

  •  Integers
  • C++ requires that all variables used in a program be given a data type. We have already seen the data type int. Variables of this type are used to represent integers (whole numbers). Declaring a variable to be of type int signals to the compiler that it must associate enough memory with the variable's identifier to store an integer value or integer values as the program executes. But there is a (system dependent) limit on the largest and smallest integers that can be stored. Hence C++ also supports the data types short int and long int which represent, respectively, a smaller and a larger range of integer values than int. Adding the prefixunsigned to any of these types means that you wish to represent non-negative integers only. For example, the declaration

    unsigned short int year_now, age_now, another_year, another_age;

    reserves memory for representing four relatively small non-negative integers.

    Some rules have to be observed when writing integer values in programs:

    1. Decimal points cannot be used; although 26 and 26.0 have the same value, "26.0" is not of type "int".
    2. Commas cannot be used in integers, so that (for example) 23,897 has to be written as "23897".
    3. Special prefixes can be used to control the base of the number system used to interpret the value of the integer. A leading "0" indicates that the number following is an octal (base 8) number. For example the compiler will interpret "011" as the octal number 011, with decimal value 9. Similarly "0x" can be used to specify a hexademical (base 16) number and, at least in C++14, "0b" can be used to specify a binary (base 2) number.
    • Real numbers
    • Variables of type "float" are used to store real numbers. Plus and minus signs for data of type "float" are treated exactly as with integers, and trailing zeros to the right of the decimal point are ignored. Hence "+523.5", "523.5" and "523.500" all represent the same value. The computer also accepts real numbers in floating-pointform (or "scientific notation"). Hence 523.5 could be written as "5.235e+02" (i.e. 5.235 x 10 x 10), and -0.0034 as "-3.4e-03". In addition to "float", C++ supports the types "double" and "long double", which give increasingly precise representation of real numbers, but at the cost of more computer memory.

Type Casting

Sometimes it is important to guarantee that a value is storedas a real number, even if it is in fact a whole number. A common example is where an arithmetic expression involves division. When applied to two values of type int, the division operator "/" signifies integer division, so that (for example) 7/2 evaluates to 3. In this case, if we want an answer of 3.5, we can simply add a decimal point and zero to one or both numbers - "7.0/2", "7/2.0" and "7.0/2.0" all give the desired result. However, if both the numerator and the divisor are variables, this trick is not possible. Instead, we have to use a type cast. For example, we can convert "7" to a value of type double using the expression "static_cast<double>(7)". Hence in the expression
answer = static_cast<double>(numerator) / denominator

the "/" will always be interpreted as real-number division, even when both "numerator" and "denominator" have integer values. 


Characters

Variables of type "char" are used to store character data. In standard C++, data of type "char" can only be a single character (which could be a blank space). These characters come from an available character set which can differ from computer to computer.
Character constants of type "char" must be enclosed in single quotation marks when used in a program, otherwise they will be misinterpreted and may cause a compilation error or unexpected program behaviour. For example, "'A'" is a character constant, but "A" will be interpreted as a program variable. Similarly, "'9'" is a character, but "9" is an integer.

For example, the program

#include <iostream> 
 using namespace std; 
 int main() 
 { int num; char character; 
 cout << "Enter  a character:\n"; 
 cin >> character;
 num = character; 
 cout << "The character '" << character; 
 cout << "' is show as the number "; 
 cout << num << " in the computer.\n";
 return 0; 
 }

Strings

Our example programs have made extensive use of the type "string" in their output. As we have seen, in C++ a string constant must be enclosed in double quotation marks. Hence we have seen output statements such as

cout << "' is represented as the number ";

in programs. In fact, "string" is not a fundamental data type such as "int", "float" or "char". Instead, strings are represented as arrays of characters, so we will return to subject of strings later, when we discuss arrays in general.

User Defined Data Types

Later in the course we will study the topic of data types in much more detail. We will see how the programmer may define his or her own data types. This facility provides a powerful programming tool when complex structures of data need to be represented and manipulated by a C++ program.

Declarations, Constants and Enumerations

As we have already seen, variables have to be declared before they can be used in a program, using program statements such as

float number;

Between this statement and the first statement which assigns "number" an explicit value, the value contained in the variable "number" is arbitrary. But in C++ it is possible (and desirable) to initialise variables with a particular value at the same time as declaring them. Hence we can write

double PI = 3.1415926535;

Furthermore, we can specify that a variable's value cannot be altered during the execution of a program with the reserved word "const":

Enumerations

Constants of type "int" may also be declared with an enumeration statement. For example, the declaration

enum { MON, TUES, WED, THURS, FRI, SAT, SUN };

is shorthand for

const int MON = 0; const int TUES = 1; const int WED = 2; const int THURS = 3; const int FRI = 4; const int SAT = 5; const int SUN = 6;

rss feed widget