Section 27: Miscellaneous #2
Data Types
There are 3 main categories of data types in C++:
- Built-in / Primitive
- Integral
intchar
- Floating Point
floatdouble
- Boolean (
bool) - Void (
void)
- Integral
- Derived
- Array
- Pointer
- Reference
- User Defined
- Struct
- Class
- Enum
- Union
graph TD
DT[Data Types]
DT --> Primitive[Built-in / Primitive]
DT --> Derived[Derived]
DT --> User[User Defined]
Primitive --> Integral
Primitive --> Float[Floating Point]
Primitive --> Bool
Primitive --> Void
Integral --> Int
Integral --> Char
Float --> FloatType[Float]
Float --> Double
Derived --> Array
Derived --> Pointer
Derived --> Reference
User --> Struct
User --> Class
User --> Enum
User --> Union
Data Type: Meaning and Size
| Type | Meaning | Typical Size (Bytes) |
|---|---|---|
int |
Integer | 2 or 4 (Machine dependent) |
char |
Character | 1 |
float |
Floating Point | 4 |
double |
Double Floating Point | 8 |
bool |
Boolean | 1 |
void |
Valueless | 0 |
wchar_t |
Wide Character | 2 |
Notes on Size and Encoding:
- ASCII & Unicode:
- Standard ASCII is used for basic characters.
- Unicode (not unicorns) is used to support various national languages (Natural Languages).
-
See http://www.unicode.org. While the data type (
wchar_tetc.) is available, full Unicode support depends on the compiler/environment. -
Integer Size: The size of an
intdepends on the word size of the machine (the bit capacity the machine handles at once). - Void:
voidmeans "nothing".- We cannot declare a variable of type
void(e.g.,void x;is invalid). - However, we can declare a pointer of type
void:
Variables and Literals
Definition
- Variables (also called identifiers) are names given by the programmer to store data.
- A variable must be declared before use. Declaration reserves memory in the Stack.
Declaration and Initialization
Valid vs Invalid Declarations:
- Variables cannot start with a number (e.g.,
1xis invalid). - Variables cannot contain special characters except underscore
_. - Variables cannot be keywords.
Four Methods of Initialization:
int a = 10; // 1. Copy Initialization
int b(10); // 2. Direct Initialization
int c {10}; // 3. Uniform Initialization (C++11)
int d = {10}; // 4. Copy List Initialization
Number Systems (Prefixes)
- Decimal: No prefix.
int x = 10; - Octal: Prefix with
0.int x = 012;(Value is 10) - Hexadecimal: Prefix with
0x.int x = 0xA;(Value is 10)
Literals and Suffixes
-
Floating Point: *
float price = 12.5;-> By default,12.5is treated as adouble. *float price = 12.5f;-> Thefsuffix explicitly makes it afloatliteral. * Note on assignments:int day = 1.7;results in truncation (stores1). -
Long Integer: *
long distance = 65839L;-> TheLsuffix specifies alongtype. -
Scientific Notation: *
float a = 123e2;-> Represents 123 \times 10^2 = 12300 *float a = 123e-2;-> Represents 123 \times 10^{-2} = 1.23 -
Characters vs Strings: *
char section = 'A';-> Single quotes forchar. *char section = 65;-> Valid (uses ASCII value of 'A'). *string name = "John";-> Double quotes forstring. * Note:stringis not a primitive type; it is a Class provided by C++. -
Boolean: *
bool b = true;*bool b = 1;(1 implies true, 0 implies false).
Coercion
Implicit conversion of one data type to another by the compiler is called Coercion.
- Example:
char a = 65.5; - The float
65.5is implicitly converted to integer65, then stored as char 'A'.
Constructor in Inheritance
When creating an object of a Derived class:
- The Parent (Base) class constructor is executed first.
- The Child (Derived) class constructor is executed second.
Calling a Parameterized Base Constructor: If the Parent class has a parameterized constructor, the Child class must explicitly call it using the initializer list syntax.