Programming with C++ – Types and Declarations
Objectives
- By the end of the week the learner should be able to:
- Identify and correctly use C++ types (fundamental, user–defined, composite).
- Explain and apply declarations (single & multiple), name scope, and various initialization styles.
- Understand the memory sizes associated with fundamental types and use the operator to query them.
Data Types
Key definition
- A data type specifies the form in which data is represented and the set of operations that can be performed on it.
- Every type has an associated data set (range of values) and data size (amount of memory reserved).
Categories of C++ data types
- Integral Types – integers, characters, Booleans.
- Floating-Point Types – , , .
- Arithmetic Types – collective name for all integral + floating-point types.
- User-Defined Types – and classes/structs.
- Built-in Types – fundamental types, pointers, references.
Data sets (value ranges)
- Integer → { whole numbers }.
- Float → { decimal numbers, single precision }.
- Double → { decimal numbers, double precision }.
- Character → { letters, digits, symbols }.
- Boolean → { (1), (0) }.
- String → { sequence of characters, may be empty (null string) }.
Typical data sizes (implementation-dependent, but common on modern 32/64-bit systems)
- Integer: bytes.
- Float: bytes.
- Double: bytes.
- Char: byte.
- Bool: byte.
- String object (empty): bytes (overhead for std::string metadata).
- Reminder: use or at compile-time to obtain exact values on your platform.
Fundamental (Built-in) Types
Integer types
- Examples: , , .
- No commas inside a literal; commas separate list items only.
- Code sample
int x = 25; // declares and initializes x
Floating-point types
- Three precision levels
- – single (≈7 decimal digits).
- – double (≈15 decimal digits).
- – extended (≥15 digits, implementation-specific).
- Example declarations
float amount = 12.72f;
double salary = 125.21313412;
long double pi_ext = 3.141592653589793238L;
Boolean (\texttt{bool})
- Used for logical results.
- Literals: (1 / Yes) and (0 / No).
bool isRaining = true;
Character (\texttt{char})
- Smallest integral type; stores a single byte/character.
- Literals between single quotes: , , ' '\$ (blank space).
char gender = 'M';
String (\texttt{std::string})
- Standard-library class representing a sequence of 0 or more chars.
- Enclosed in double quotes.
string name = "Ama"; // null string would be ""
Void
- Signifies “no type” or “absence of value”.
- Only legal in compound constructs (pointer, function return type, reference).
void f(); // function returning nothing
void* pv; // pointer to unknown type
void& r = *pv; // reference to void (rare)
Signed vs. Unsigned modifiers
- Signed (default) → supports negative & positive numbers.
- Unsigned → non-negative only (wrap-around differs).
signed int sx = -1;
unsigned int ux = 4294967295u; // max for 32-bit unsigned
Other length modifiers (integral)
- \texttt{short} (≈2 bytes)
- \texttt{long} (≈4 bytes)
- \texttt{long\ long} (≈8 bytes)
Composite Types
- Pointers – hold memory addresses.
- Arrays – contiguous collection of elements of same type.
- References – alias to an existing object.
User-Defined Types
- Structures / Classes – aggregate data + functions.
- Enumerations (enum) – finite set of symbolic constants, e.g. weekdays.
Declarations
Purpose & syntax
- An identifier must be declared before use so the compiler knows what entity it denotes.
- General form:
[type] [declarator];- Examples:
- \texttt{char ch;}
- \texttt{string s;}
- \texttt{int count = 1;}
- \texttt{const double pi = 3.1415926535897;}
Declaring multiple names
- Same type + comma-separated list
int x, y; // equivalent to: int x; int y;
Scope (visibility region of a name)
- Local scope – inside a block/function.
- Global scope – outside all functions.
- Class scope – inside class definition.
- Namespace scope – inside a given namespace.
- Statement scope – names introduced in control statements (e.g., for-loop initializers).
int student_age; // global
int main() {
int age; // local
}
Initialization Styles (C++11 forward)
- Determines the initial value of an object; also usable to modify.
- Four equivalent syntactic forms (given type Xv):
- Brace list: X\ a_1\ { v };
- Brace w/ =: X\ a_2 \ = { v };
- Copy init: X\ a_3 \ = \ v;
- Functional: X\ a_4 ( v );
Example:
int age = 25; // copy init
char gender{'M'}; // brace init (preferred, avoids narrowing)
Practical Pointers & Connections
- Always match the type to the data’s real-world use: choosing unsigned for naturally non-negative quantities, floating point for measurable quantities with fractions, etc.
- Memory footprint matters in embedded/low-resource systems; use \texttt{sizeof}$$ to audit.
- Scope rules prevent name collisions and aid readability; prefer smallest necessary scope.
- Initialization over default-construction + assignment prevents uninitialized memory bugs.
- Ethical/Practical: representing information faithfully (e.g., not forcing negative ages) helps program correctness and user trust.