Programming Fundamentals and Methodology COMP104: C++ Basics
This document provides an introduction to programming fundamentals and the C++ programming language. It discusses key concepts like programming languages having their own vocabulary and grammar, the general form of a C++ program including main functions and comments. It also covers basic C++ programming elements such as data types, variables, input/output operations, and compiling and running a simple "Hello World" program. The document is meant to teach beginner C++ programming concepts.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
93 views59 pages
Programming Fundamentals and Methodology COMP104: C++ Basics
This document provides an introduction to programming fundamentals and the C++ programming language. It discusses key concepts like programming languages having their own vocabulary and grammar, the general form of a C++ program including main functions and comments. It also covers basic C++ programming elements such as data types, variables, input/output operations, and compiling and running a simple "Hello World" program. The document is meant to teach beginner C++ programming concepts.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59
Programming Fundamentals and Methodology
COMP104: C++ Basics
Dr. Brian Mak Dr. Pedro Sander Dr. Zhen Zhou Department of Computer Science & Engineering The Hong Kong University of Science and Technology Hong Kong SAR, China Fall 2007 { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.1 Programming Languages Just like dierent human languages (e.g. Chinese, English, Japanese, French, etc.), each programming language (e.g. Pascal, C++, Java, etc.) has its own vocabulary = the set of legal words grammar or syntax: how words are put together to make a legal sentence A program consists of a sequence of statements (c.f. sentence in human language) Some parts of a statement are called expressions (c.f. phrase in human language). e.g. logical expression: x > y arithmetic expression: 5 + 4 { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.2 Part I A Simple C++ Program { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.3 Hello World! / A common program used to demo a new language / #include <iostream> // preprocessor directives using namespace std; // standard namespace int main(int argc, char argv) // programs entry point
Write, Compile, and Run the Program STEP 1 : Write the program using an editor. e.g. vi, kate STEP 2 : Save the program into a le called hello-world.cpp. STEP 3 : Compile the program using g++. g++ -o hello-world hello-world.cpp If you dont specify the output lename using the -o option, the default is a.out. g++ hello-world.cpp STEP 4 : Run the program in a Linux terminal as follows: linux:: hello-world Hello World! { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.5 C++ Comments Use /* */ for multiple-line comments. / A common program used to demo a new language / Single-line comments start with //. // programs entry point Comments are just for human to read. They will not be translated by the compiler into machine codes. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.6 Preprocessor Directives Preprocessor is another software that runs before a compiler. As its name indicates, it does some preprocessing work. Preprocessor directives are keywords that start with a #: e.g. #include #dene #ifdef #ifndef #endif #include will include information of a library another program or sub-program. e.g. #include <iostream> gets the information of the library program that deals with I/O reading inputs using cin and printing outputs using cout. Two forms: #include <iostream> // standard C++ library #include my lib.h // user-dened C++ library { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.7 Namespace Two dierent persons may have the same name. To avoid name conicts, one may say, I mean the John Woo from Hong Kong and not the one from Shanghai. Similarly, two programs may also use the same names. To avoid name conicts in C++, we have to specify which namespace the names appears. Always include the following statement which uses names in the standard C++ library in your program in this course unless you are told to use other namespaces. using namespace std; // standard namespace { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.8 Main: the Entry Point Every program must have exactly one and only one main( ) function which has the following general form: int main (int argc, char argv) A simpler form of main is: int main () (Well talk about argc and argv later.) Between the braces and are the program codes consisting of zero or more program statements. Each simple C++ statement ends in a semicolon ;. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.9 C++ is a Free Format Language Extra blanks, tabs, lines are ignored. Thus, codes may be indented in any way to enhance readability. More than one statement can be on one line. Here is the same Hello World program: #include <iostream> using namespace std; int main (int argc, char argv) cout"Hello World!"endl;return 0; On the other hand, a single statement may be spread over several lines. cout "Hello World!" endl; { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.10 Good Programming Style Place each statement on a line by itself. For long statements if possible, break it down into several shorter statements. wrap it around with proper indentation (since extra space doesnt matter!) Use blank lines to separate sections of related codes that together perform some action. Indent consistently. Use the same indentation for the same block of codes. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.11 Example: General Form of a C++ Program Problem: For a given number, report the product after it is multiplied by 10. First have the solution written as a sequence of instructions: 1 Ask the user to input a number. 2 Multiply the number by 10. 3 Output the product. Computer programming is to re-write the human instructions in a computer language. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.12 Example: General Form of a C++ Program .. #include <iostream> // Preprocessor directives using namespace std; // standard namespace / Other variables and class declarations and denitions / int main(int argc, char argv) // programs entry point
const int TIMES = 10; // constant declarations
int x = 0, y = 0; // variable declarations and denitions cin x; // input statements y = x TIMES; // computations cout "y = " y endl; // output statements return 0; // program ending and feedback to the shell
Part II Simple Data Types in C++ { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.14 Data Types: Introduction The Web has to deal with dierent multimedia data: text, sound/music, image, video, etc., and they can only be read/viewed with dierent softwares such as MS Notepad, Acrobat Reader, RealPlayer, etc. Similarly, a computer program has to deal with dierent types of data. In a programming language, data are categorized into dierent types. Each data type comes with a set of operations for manipulating its values. Operations on basic data types are built into a programming language. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.15 How Numbers are Represented in Computers: Binary Numbers Computer uses binary numbers (base 2) to represent data. In the decimal system: 423 10 = 4 10 2 + 2 10 1 + 3 10 0 . In the binary system: A digit has only 2 possibilities: 0,1. Example: 101 2 = 1 2 2 + 0 2 1 + 1 2 0 Thus, the maximum N-digit number in base 2 = A binary digit is aka bit. 8 bits = 1 byte. (smallest amount of data that a computer can bite at once.) { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.16 Hexadecimal Numbers A decimal number written as a binary number requires much more digits and can be hard to read. e.g. 10101010101 2 = We want a shorter representation for binary numbers. In the hexadecimal system: A digit has 15 possible values: 0 9, A, B, C, D, E, F . There is an easy conversion between the binary and hexadecimal representation of a number. Example: 504 16 = ABC 16 = Hexadecimal numbers are commonly used for writing e.g. memory locations. Hexadecimal numbers are written with the prex 0x or 0X. e.g. 0xA2B1. Question: What are kB, MB, GB, TB, ...? { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.17 C++ Basic Types Types Size(#bytes in a 32-bit machine) Value Range bool 1 true, false char 1 [-128, 127] short 2 [-32768, 32767] int 4 [2 31 , 2 31 1] long 4 [2 31 , 2 31 1] oat 4 [1.17549E-38, 3.40282E+38] double 8 [2.22507E-308, 1.79769E+308] { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.18 Integers Type names: short (int), int, long (int), long long (int) Their sizes depend on the CPU and the compiler. ANSI C++ requires: size of short size of int size of long size of long long Question: How to represent -ve numbers? Question: What are little endian and big endian? { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.19 signed and unsigned Integral Data Types How many dierent numbers can a 2-byte short int represent? What are they? In C++, each integral data type has 2 versions: signed version: represents both +ve and -ve integers. e.g. signed short, signed int, signed long unsigned version: represents only +ve integers. e.g. unsigned short, unsigned int, unsigned long signed versions are the default. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.20 Floating-Point Data Types Type names: oat for single-precision numbers, double for double-precision numbers. Floating-point numbers are used to represent real numbers and very large integral numbers (which cannot even be held in long long). In scientic notation, a number has 2 components. e.g. 5.16E-02 mantissa: 5.16 exponent: -2 Many programming language uses the IEEE 754 oating-point standard. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.21 Floating-Point Data Types .. float double #bits 1 8 (11) 23 (52) signbit exponent mantissa Binary Representation of mantissa: e.g. 1.011 2 = 1 2 0 + 0 2 1 + 1 2 2 + 1 2 3 Binary Representation of exponent: signed integer All oating-point data types in C++ are signed. ANSI C++ requires: size of oat size of double Question: Can every number be represented by oat in C++? { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.22 Boolean Data Type Type name: bool. Used to represent the truth value, true or false of logical expressions like: A > B x + y = 0 HKUST is famous Since C++ evolves from C, C++ follows Cs tradition: zero may be interpreted as false. non-zero values may be interpreted as true. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.23 Character Type name: char, unsigned char. Represent a single character delimited in single quotes. Examples: a, b, 4 For special characters, use the escape character . e.g. t = tab n = newline b = backspace 0 = null character Internally, since a computer only recognizes bits, a char datum is represented as an integer. Or, a char datum is encoded by 256 integers. The most common encoding scheme is called ASCII (American Standard Code for Information Interchange). Question: What is Unicode? { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.24 Character String Character string is NOT a basic data type in C++. There is a string library that denes string objects. Basically, a character string is a sequence of char data ended in the null character 0. h u k s t \0 C++ allows another notation using the double quotes. e.g. hkust = h k u s t 0 { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.25 Find Out Their Sizes using sizeof #include <iostream> #include <values.h> using namespace std; int main(int argc, char argv) { cout "sizeof(bool) = " sizeof(bool) endl; cout "sizeof(char) = " sizeof(char) endl; cout "sizeof(short) = " sizeof(short) endl; cout "sizeof(int) = " sizeof(int) endl; cout "sizeof(long) = " sizeof(long) endl; cout "sizeof(long long) = " sizeof(long long) endl; cout "sizeof(float) = " sizeof(float) endl; cout "sizeof(double) = " sizeof(double) endl; cout "sizeof(long double) = " sizeof(long double) endl; return 0; } { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.26 Size of Basic Types on a 32-bit P4 Linux Machine sizeof(bool) = 1 sizeof(char) = 1 sizeof(short) = 2 sizeof(int) = 4 sizeof(long) = 4 sizeof(long long) = 8 sizeof(float) = 4 sizeof(double) = 8 sizeof(long double) = 12 { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.27 Underow and Overow in Integral Data Types Overow: occurs when a data type is used to represent a number larger than what it can hold. e.g. if you use a short int to store HKs population. when a short int has its max value of 32767, and you want to add 1 to it. Underow: occurs when a data type is used to represent a number smaller than what it can hold. e.g. use an unsigned int to store a -ve number. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.28 Underow and Overow in Floating-Point Data Types Underow: when the -ve exponent becomes too large to t in the exponential eld of the oating-point number. Overow: when the +ve exponent becomes too large to t in the exponential eld of the oating-point number. To prevent these from happening, use double if memory space allows. In fact, all arithmetic operations in C++ involving oat data are done in double precision. oat data are automatically changed to double in all arithmetic operations in C++. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.29 Type Checking and Coercion Analogy: Blood Types Receiver Donor A A, O B B, O AB A, B, AB, O O O For most languages, data types have to be matched during an operation type checking. However, sometimes, a type is made compatible with a dierent type coercion. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.30 Part III Variables in C++ { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.31 Identiers f (x) = x 2 + c where f : name of a function x : name of a variable c : name of a constant In programming languages, these names are called identiers. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.32 Rules for Making up Identier Names Only the following characters may appear in an identier: 09, az, AZ, The rst character cannot be a digit (09). C++ keyword reserved words are not allowed. Examples: amount, COMP104, myname C++ identiers are case-sensitive: lowercase and uppercase letters are considered dierent. hkust, HKUST, HkUst, HKust are dierent identiers. Examples of illegal C++ identiers: Guidelines: use meaningful names. e.g. amount instead of a for long names consisting of several words, use to separate them or capitalize them. e.g. num of students or numOfStudents instead of numofstudents. usually identiers starting with are used for system variables. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.33 Reserved Words in C++ asm auto bool break case catch char class const const cast continue default delete do double dynamic cast else enum explicit extern false oat for friend goto if inline int long mutable namespace new operator private public protected register reinterpret 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 { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.34 Variables A variable is a named memory location for a value that we can write to, retrieve from, and manipulate. 0xA014 0xA010 radius area 2.5 19.64 It can be thought of as a container/box for a value. A variable must be declared and/or dened before it can be used. Syntax for variable denition: < data type > < identier > ; Example: oat radius; // radius of a circle oat area; // area of a circle { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.35 Variable Declaration/Denitions Syntax for dening several variables of the same type at once: < data type > < identier 1 >, < identier 2 >, ; Example: double radius, area, circumference; int num of chars, num of words, num of lines; bool I am the best, hkust is famous; When a variable is dened, the compiler allocates memory for it. The amount of memory is equal to the size of its data type. Some books will call this variable declaration. Actually there is a big dierence between variable declaration and variable denition. Well talk about that later. When a variable is dened, it is also declared. The other way is not true. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.36 Variable Initialization Syntax for initializing variables while they are dened: < data type > < identier >=< value > ; Several variables of the same type may also be initialized at the same time. e.g. double radius = 0.0, area = 0.0; A variable may also be initialized by a separate assignment statement after it is dened: e.g. float radius; // variable denition radius = 2.5; // initialization by assignment ANSI C++ does not require compilers to initialize variables. Thus, in general, if you do not explicitly initialize variables while you are dening them, their initial contents may be garbage. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.37 Part IV Constants { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.38 Literal Constants Constants represent xed values, or permanent values that cannot be modied (in a program). Examples: char constants: a, 5, n string constants: hello world, dont worry, be happy int constants: 123, 456, -89 oat constants: 123.456, -2.90E+11 { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.39 Symbolic Constants A symbolic constant is a named constant with an identier name. A symbolic constant must be declared and/or dened before it can be used. Syntax for constant denition: const < data type > < identier >=< value > ; Example: const char BACKSLASH = \b; const int COMP104L1 QUOTA = 113; const float US2HK = 7.80; const float HK2RMB = 1.03; const float US2RMB = US2HK HK2RMB; The rule for identier names for constants is the same as that for variables. However, by convention, constant identiers are written in capital letters. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.40 Why Symbolic Constants? Compared with literal constants, symbolic constants are preferred because they are more readable. A literal constant does not carry a meaning. e.g. the number 113 cannot tell you that it is the quota of COMP104 L1 this semester. more maintainable. In case we want to increase the quota to 120, we only need to make the change in one place: the initial value in the denition of the constant COMP104L1 QUOTA. type-checked during compilation. Notice that unlike variable denitions, memory may or may not be allocated for constant denitions. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.41 Compile-Time vs. Non-Compile-Time Constants Values of compile-time constants are known at compile time, whereas values of non-compile-time constants are not. const int K = 10; // compile-time constant int x; // variable whose value can be changed cout "Input an integer: "; cin x; const int N = x + K; // non-compile-time constant By denition, all literal constants are compile-time constants. Non-compile-time constants require memory allocation during compilation, while compile-time constants usually dont (though there are exceptions). { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.42 Part V Operators { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.43 Arithmetic Operators Assuming x = 100, y = 67: Operation operator char int oat unary minus addition + subtraction multiplication division / modulus % increment ++ decrement Question: What are the result if x and y are bool? { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.44 Modulo Arithmetic The mod math function is represented as the % operator. mod is used to get the remainder in an integer division. e.g. mod(19, 4) = 19 mod 4 = 19%4 = 3 (Read as 19 modulo 4.) Question: What are the results of (-19)%4 or 19%(-4)? Mathematically, if a mod n = r then r = a n
a n
where | is the oor function.
However, many programming languages do not follow the exact math denition! As a consequence, the result depends on the compiler. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.45 Pre- and Post- Increment, Decrement The unary increment operator ++ add 1 to its operand. The unary decrement operator subtract 1 from its operand. However, there are 2 ways to call them: pre-increment or post-increment. e.g. ++ x x ++ x x If used alone, they are equivalent to: x = x +1 and x = x 1. But if used with other operands, then there is a big dierence: ++ x add 1 to x, and use the result for further operation. x ++ use the current value of x for some operation, and then add 1 to x. cout ++x; /* same as */ x = x + 1; cout x; cout x++; /* same as */ cout x; x = x + 1; { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.46 Example: %, ++, #include <iostream> using namespace std; int main() { int x = 100, y = 100; int a = 10, b = 10, c = 10, d = 10; cout ++x "\t"; cout "x = " x endl; cout y++ "\t"; cout "y = " y endl; a = ++b; cout "a = " a "\t" "b = " b endl; c = d++; cout "c = " c "\t" "d = " d endl; cout 19%4 endl; cout (-19)%4 endl; cout 19%(-4) endl; return 0; } { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.47 Operand Coercion Coercion is the automatic conversion of the data type of operands during an operation. Example: 3 + 2.5 int + double. The C++ compiler will automatically change it to 3.0 + 2.5 double + double Thus, the integer 3 is coerced to the double 3.0. e.g. To change a character from small case to capital letter: char small y, big y; cin small y; // character in small case big y = small y + A - a; // character in big case Here big y, small y, A, and a are converted to int before addition. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.48 Rule for Coercion of Numeric Data Types If at least one operand is a oating-point number convert all operands to double compute using oating-point arithmetic in double precision return the result in double If all operands are int compute using integer arithmetic Question: What is the result of 3/4? { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.49 Assignment Operator Syntax for assignment: < variable > = < value > ; In C++, the = sign is used to assign a value to a variable; it is the assignment operator. Examples: int a, b, x = 2, y = 3, z = 4; a = 10x; b = a - (100y - 1000z); a = a + b; Dont try to understand the assignment statement: a = a + b; using normal math notation, otherwise, it doesnt make sense. Nor should you treat it as a boolean relational equality sign. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.50 Automatic Type Conversion During Assignment float x = 3.2; // initialize x with 3.2 by assignment double y = 5.7; // initialize y with 5.7 by assignment short k = x; // k = ? int n; n = y; // n = ? Since oat[double can hold numbers bigger than short [ int, the assignment of k and n in the above program will cause the compiler to issue a warning not an error. a.cpp:9: warning: converting to short int from oat a.cpp:11: warning: converting to int from double { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.51 Automatic Type Conversion During Assignment .. A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values. A widening conversion changes a value to a data type that can accommodate any possible value of the original data. C++ uses truncation rather than rounding in converting a oat[double to short [ int [ long. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.52 Manual Type Conversion (Casting) int k = 5; int n = 2; float x = n/k; // What is the value of x? In the above example, one can get x = 0.4 by manually converting n and/or k from int to oat[double. Syntax for manual type casting: < data type > (< value >) (< data type >) < value > int k = 5; int n = 2; float x = (double)n/k; // What is the value of x? float y = double(n)/k; // What is the value of y? { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.53 Shorthand Assignment Operators shorthand notation normal notation n + = 2 n = n + 2 n = 2 n = n 2 n = 2 n = n 2 n / = 2 n = n /2 n % = 2 n = n %2 { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.54 Precedence and Associativity Operator Description Associativity minus RIGHT ++ increment decrement multiply LEFT / divide % mod + add LEFT subtract = assignment RIGHT { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.55 Precedence Example: 1/2 + 3 4 = (1/2) + (3 4) because , / has a higher precedence over +, . Precedence rules decide which operators run rst. In general, x P y Q z = x P ( y Q z ) if operator Q is at a higher precedence level than operator P. { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.56 Associativity: Binary Operators Example: 1 2 + 3 4 = ((1 2) + 3) 4 because +, are left associative. Associativity decides the grouping of operands with operators of the same level of precedence. If binary operator P, Q are of the same precedence level if operator P, Q are both right associative, then x P y Q z = x P ( y Q z ) if operator P, Q are both left associative, then x P y Q z = ( x P y ) Q z { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.57 Cascading Assignments C++ allows assigning the same value to multiple variables at once. int w, x, y, z; y = z = 5; // same as y = (z = 5); w = x = y + z; // same as w = (x = (y+z)); { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.58 Expression and Statement An expression has a value which is the result of some operation(s) on its(theirs) operands. Expression examples: 4 x - y 2 - a - (b * c) A statement is a sentence that acts as a command. It does not have a value. It always ends in a ;. Statement examples: Input statement: cin >> x; Output statement: cout << x; Assignment statement: x = 5; Constant denition: const int x; Variable denition: int x; { mak, psander, cszz } cse.ust.hk COMP104 (Fall 2007) p.59