0% found this document useful (0 votes)
5 views

C++ - Chapter 2

Uploaded by

Mohammed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C++ - Chapter 2

Uploaded by

Mohammed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

The main() Function


- Overall structure of a C++ program contains one function named main(), called the driver function
- All other functions are invoked from main()
- Function header line: First line of a function, which contains:
• The type of data returned by the function (if any)
• The name of the function
• The type of data that must be passed into the function when it is invoked (if any)
- Arguments: The data passed into a function
- Function body: The statements inside a function enclosed in braces
- Each statement inside the function must be terminated with a semicolon ;
- return: A keyword causing the appropriate value to be returned from the function
• The statement return 0 in the main() function causes the program to end

The cout Object


- cout object: An output object that sends data to a standard output display device
- Preprocessor command: Starts with a # causes action before the source code is compiled into machine code
- #include <file name>: Causes the named file to be inserted into the source code
- Header files: Files included at the head (top) of a C++ program
- using namespace <namespace name> : Indicates where header file is located
• Namespaces qualify a name
- String: Any combination of letters, numbers, and special characters enclosed in double quotes
- Delimiter: A symbol that marks the beginning and ending of a string; not part of the string
- Escape sequence: One or more characters preceded by a backslash, \ (like \n) , Tells compiler to treat the
following characters as special instruction codes

Comments: Explanatory remarks in the source code added by the programmer


- Line comment: Begins with // and continues to the end of the line
- Block comments: comments that span across two or more lines , Begin with /* and end with */

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)

Determining Storage Size


- A unique feature of C++ is that you can see where and how values are stored
- sizeof() operator provides number of bytes used to store values of the data type named in the parenthesis
• Values returned by sizeof() are compiler dependent

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

Operator Precedence & Associativity


- Rules for writing arithmetic expressions:
1. Never place two consecutive binary arithmetic operators side by side
2. Use parentheses to form groupings (Contents within parentheses are evaluated first)
3. May nest parentheses within other parentheses (Evaluated from innermost to outermost)
4. Use the * operator for multiplication, not parentheses
- Expressions with multiple operators are evaluated by precedence of operators:
1. All negations occur first
2. Multiplication, division, and modulus are next, from left to right
3. Addition & subtraction are last, from left to right
- Associativity: the order in which operators of the same precedence are evaluated

Variables & Declaration Statements


- Variable: Symbolic identifier for a memory address where data can be held
- Assignment statement: Used to store a value into a variable
• Value of the expression on the right is assigned to the memory location of the variable on the left side

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

Common Programming Errors


• Omitting the parentheses after main()
• Omitting or incorrectly typing the opening brace, {, or the closing brace, }, that signifies the start and
end of a function body
• Misspelling the name of an object or function
• Forgetting to enclose a string sent to cout with quotation marks
• Omitting a semicolon at end of statement
• Adding a semicolon at end of #include statement
• Missing \n to indicate new line
• Substituting letter O for zero and vice versa
• Failing to declare all variables
• Storing an incorrect data type into a variable
• Attempting to use a variable with no value
• Dividing integer values incorrectly
• Mixing data types in the same expression

Good luck… Jumana

Page 3 of 3

You might also like