Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Similar presentations


Presentation on theme: "Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220."— Presentation transcript:

1 Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

2 The char Data Type 1 byte integer data type Used for storing characters as an int ASCII (American Standard Code for Information Interchange) Table

3 Character literals are not string literals! Program 2-12 and 2-13 char letter; Letter = ‘a’; char letter; Letter = “a”;

4 // Relationship between integers and characters #include using namespace std; int main() { char letter; letter = 65; cout << letter << endl; letter = 66; cout << letter << endl; return 0; }

5 // A simple C++ program #include using namespace std; int main() { char letter; letter = ‘A’; cout << letter << endl; letter = ‘B’; cout << letter << endl; return 0; }

6 Floating-Point Data Types Used to define variables that hold real numbers float (single – precision) double (double – precision) long double (at least 2x double size)

7 Tips 1.2F – forces literal to be stored as a float The following are equivalent floating-point literals 1.4E11 1.4e11 1.4E+11 1.4e+11 140000000000.0

8 // A simple C++ program #include using namespace std; int main() { float distance; double mass; distance = 1.496979E11; mass = 1.989E30; cout << “The sun is “ << distance << “ meters away.\n”; cout << “The sun\’s mass is “ << mass << “ kilograms.\n”; return 0; }

9 The bool Data Type Boolean variables can either be true or false ( 0 or 1) Program 2-16

10 // A simple C++ program #include using namespace std; int main() { bool boolValue; boolValue = true; cout << boolValue << endl; boolValue = false; cout << boolValue << endl; return 0; }

11 Determining the Size of a Data Type The sizeof operator can be used to determine the size of a data type on any system. Example: sizeof(int); Program 2.17

12 // A simple C++ program #include using namespace std; int main() { long double apple; cout << “The size of an integer is “ << sizeof(int) << bytes.\n”; cout << “The size of a long integer is “ << sizeof(long) << bytes.\n”; cout << “An apple can be eaten in “ << sizeof(apple) << bytes!\n”; return 0; }

13 Variable assignments and initializations An assignment operation assigns, or copies, a value into a variable. When a value is assigned as part of it’s definition, it is called initialization. Program 2-18

14 // A simple C++ program #include using namespace std; int main() { int month =2, days =28; cout << “Month “ << month << “ has “ << days << “ days.\n”; return 0; }

15 Scope A variable’s scope is that part of the program that has access to that variable. Complex, and the rules that define a variables scope will be covered throughout the lectures Rule 1: A variable cannot be used in any part of the program until it is defined.

16 Arithmetic Operators There are many operators for manipulating numeric values and performing arithmetic operations. Three types Unirary (requires only 1 operand. Exmp. -5) Binary (requires 2. Exmp. A = 3) Ternary (requires 3. Exmp. Discussed in chp. 4)

17 Common Arithmetic Operators Binary operators. +addition, total = cost + tax; -subtraction, cost = total – tax; *multiplication, tax = /division, salePrice = original / 2; %modulo, remainder = value % 3; Tip: when both operands in a division are integers, the statement will perform int division. Use one number as floating point if need. Do program 2-20

18 Comments and Programming Style Not used by the compiler, i.e. invisible // - single line comment /* ……… */ - multiline comment Programming Style is the visual arrangement of your code. Make it readable for yourself and others!

19 Homework Check moodle! Due Saturday before Midnight.


Download ppt "Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220."

Similar presentations


Ads by Google