1. C++ basics

1. C++ basics - ENGG1002 Computer Programming and...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGG1002 Computer Programming and Applications Prepared by Dr. K.K.Y. Wong
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Variables Variables in computer programming Used to store data Implemented as memory locations The compiler will assign an appropriate number of memory cells in the main memory to each variable according to the type of data to be stored Referred to by its identifier (the name of a variable) The data stored in a variable may change over time 2 int main() { int width = 5; int height = 10; int area = width * height; return area; } variables
Background image of page 2
Identifier An identifier must start with either a letter (i.e., A to Z , and a to z ), or the underscore symbol (i.e., _ ) The rest of the character may be letters (i.e., A to Z , and a to z ), digits (i.e., 0 to 9 ), or the underscore symbol (i.e., _ ) C++ is case-sensitive So radius , RADIUS , Radius , etc., are different Cannot be a keyword in C++ 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Keywords in C++ Reserved words in C++ with predefined meanings Cannot be used as names for variables or anything else C++ Keywords: 4 asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit extern false float for friend goto if inline int log long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while
Background image of page 4
Valid Identifiers Which of the following identifiers are valid in C++? 5 a_man const an integer string cout 2008 year1-student change%2 Days_of_Week delete program.cc _oOOo_ ABCx123 friend cos Words like cin , cout , string , and cos are not keywords in C++. They are defined in libraries required by the C++ language standard. Redefining these words, though allowed, can be confusing and thus should be avoided
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Date Types Data type of a variable Determines the number of memory cells needed to store the data Tells the computer how to interpret the data stored in a variable Some basic data types in C++: 6 Name Description Size Range char Character or small integer 1 byte 0 to 255 bool Boolean value 1 byte true (1) or false (0) int Integer 4 bytes -2147483648 to 2147483647 double Double precision floating point number 8 bytes 1.7e-308 to 1.7e+308 (15 digits) The size and range of a particular data type depend on the system under which a program is compiled. The values shown above are those found on most 32-bit systems
Background image of page 6
Declarations All variables must be declared before use A declaration specifies a type, and contains a list of one or more variables of that type Examples: 7 Syntax type_name variable_name_1, variable_name_2, …; int age, steps; char c; bool win; double height, width, length; double weight;
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Initializations A variable that has not been given a value is said to be
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/26/2011 for the course BENG 1002 taught by Professor Dr.x during the Winter '10 term at HKU.

Page1 / 27

1. C++ basics - ENGG1002 Computer Programming and...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online