C++ Primer For Java Programmers


Variables and Data Types

The Main Function Table of Contents Function Declarations

Identifiers

As in all programming languages, we use identifiers to name things such as variables, functions, data types, classes, etc. Identifiers in C++:

  • can consist of characters, digits, and the underscore
  • must begin with a character
  • are case sensitive
  • can be of any length, though the first 32 characters must be unique.

Variables

Variables in C++ must be explicitly declared as they are in Java.

int grade,
    sum,
    total;

double average,
       salary;
 

In C++, variables are not initialized when they are created. You can declare a variable and initialize it at the same time

int total = 0;
double taxRate = 0.05;
 

Data Types

C++ provides a number of primitive data types including

Integers
char 1-byte signed
short 2-bytes signed
int size of system word
(4-bytes: 32-bit architecture)
(8-bytes: 64-bit architecture)
long 4-bytes signed
long long 8-bytes signed
(implemented in software)
unsigned char 1-byte unsigned
unsigned short 1-byte unsigned
unsigned short 2-bytes unsigned
unsigned int same size as an int
unsigned int 4-bytes unsigned
unsigned long long 8-bytes unsigned
(implemented in software)
Reals
float 4-bytes
double 8-bytes
long double 12-bytes
Other
bool 4-bytes
enum 8-bytes
reference same size as an int

Literal values can be used in C++ programs just like in any other language. In C++, literals for the basic types are specified as follows

integers
17 35 −82
reals
0.5 3.14 25.4E+78
character
‘c’ ‘8′ ‘&’ ‘\n’
strings
“This is a string”
boolean
true false

Assignments

Assignments in C++ use the the single equal sign (=) as in Java. Variables are declared to store a specific data type can only be assigned a value of that type.

grade = 89;
taxRate = 0.155;
 

Attempting to sign incompatable types will result in a compilation error.

Constant Declarations

Constant variables or named literals are defined using the keyword const placed in front of the variable declaration. A constant declaration requires an initial value assigned to the variable

const float PI = 3.1415926;
const int NUM_VALUES = 4;
 

Once a value has been assigned to a constant variable, it can not be changed. Constant variables are typically named in all uppercase to distinguish them from other variables.

Mathematical Operators

Mathematical expressions are constructed using the mathematical operators

avg = (grade1 + grade2 + grade3) / 3.0;
 

which are the same as those in Java

  +  -  *  /  %  ( )

If both operands are integers, then integer arithmetic is performed and the result will be an integer value. If one or both of the operands are real values, then real arithmetic is performed and the result is a real value. (For operator precedence, see the order of precedence table.)

Type Casting

There are two approaches for typecasting in C++: the C method or the new C++ method. In C, a value can be typecast or forced to a new type by specifying the value within parentheses

int count = 3;
double avg;
     :
avg = (grade1 + grade2 + grade3) / (float) count
 

while in C++ the type to which a value is being typecast is used as method

avg = (grade1 + grade2 + grade3) / float( count )
 

Type Definitions

In addition to the primitive data types, users can create their own types by simply renaming existing types or constructing new types from existing ones usin the typedef instruction. In the following example,

typedef unsigned char byte;
 

we create a new type called byte from the exisiting type unsigned char. You can now use this new type in your program just as you would any other type. Internally the program will actually use an unsigned char type and thus the use of the new type is still goverened by that type.

byte value;
byte small = 4;
 

The general form of the typedef statement is

typedef original-type new-name;


The Main Function Table of Contents Function Declarations

Print - Changes - Search
Last modified: April 28, 2007, at 11:15 AM.