C++ - Chapter 2
C++ - Chapter 2
Modular program: A program consisting of interrelated segments (or modules) arranged in a logical &
understandable form
- Advantages : Easy to develop, to correct, and to modify
- Modules in C++ can be classes or functions
1. Function: Accepts an input, processes the input, & produces an output
• A function’s processing is encapsulated and hidden within the function
• Function names
Require a set of parentheses at the end
Can use mixed upper and lower case
Should be meaningful, or be a mnemonic
2. Class: Contains both data & functions used to manipulate the data
Identifier: A name given to an element of the language, such as a variable, constant, class or function
- C++ is a case-sensitive language
- Rules for forming identifier names:
• First character must be a letter or underscore
• Only letters, digits, or underscores may follow the initial letter (no blanks allowed)
• Keywords cannot be used as identifiers
Keyword: A reserved name that represents a built-in object or function of the language
• Maximum length of an identifier = 1024 characters
Data type: A set of values & the operations that can be applied to these values
- Two fundamental C++ data groupings:
• Class data type (a class): Created by the programmer
• Built-in data type (primitive type): Part of the C++ compiler
Literal (constant): An actual value
Page 1 of 3
C++ - Chapter 2
Integer: A whole number , C++ has nine built-in integer data types :
1. int data type: Whole numbers (integers), optionally with plus (+) or minus (–) sign , Example: 2, -5
2. char data type: Individual character; any letter, digit, or special character enclosed in single quotes ,
Example: ‘A’ , Character values are usually stored in ASCII code
3. bool data type: Represents Boolean (logical) data , Useful when a program must examine a condition and
take a prescribed course of action, based on whether the condition is true or false
Restricted to two values: true or false
4. Signed & Unsigned Data Types
Signed data type: One that permits negative, positive, and zero values
Unsigned data type: Permits only positive and zero values ( provides double the range of its signed counterpart)
Floating-Point Types
Floating-point number (real number): Zero or any positive or negative number containing a decimal point
- No special characters are allowed
- Three floating-point data types in C++: float (single precision) , double (double precision) , long double
- float literal: Append an f or F to the number
- long double literal: Append an l or L to the number
Arithmetic Operations
- C++ supports addition, subtraction, multiplication, division, and modulus division
- Different data types can be used in the same arithmetic expression
- Arithmetic operators are binary operators
• Binary operators: Require two operands
• Unary operator: Requires only one operand
• Negation operator (-): Reverses the sign of the number
Expression Types
- Expression: Any combination of operators & operands that can be evaluated to yield a value
- If all operands are the same data type, the expression is named by the data type used (integer expression, …)
- Mixed-mode expression: Contains integer & floating-point operands , Yields a double-precision value
- Integer division: Yields an integer result , Any fractional remainders are dropped (truncated)
- Modulus (remainder) operator: Returns only the remainder
Page 2 of 3
C++ - Chapter 2
- Declaration statement: Specifies the data type and identifier of a variable; sets up the memory location
• Multiple variables of the same data type can be declared in a single declaration statement
• Variables can be initialized in a declaration
• A variable must be declared before it is used
Memory Allocation
- Definition statement: A declaration that defines how much memory is needed for data storage
- Three items associated with each variable:
• Data type
• Actual value stored in the variable (its contents)
• Memory address of the variable
- Address operator (&) provides the variable’s address Memory Allocation
- Declaring a variable causes memory to be allocated based on the data type
Page 3 of 3