Skip to content
CS-504 (B) · Object Oriented Programming/Quick Revision Short Notes

Object Oriented Programming (CS-504 (B)) - Unit 1 Short Notes

How unit 1 is examined

This unit covers the C/C++ building blocks: character set, constants, variables, keywords, identifiers, data types, storage classes, conversions and operator precedence; the only past questions are the Jun 2020 7-mark "constants, variables, identifiers, literals" and "type conversions".

Character set

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>The character set is the group of valid letters, digits and symbols that a C++ program may use to build tokens.</mark>

Key points.

  1. Letters are the uppercase A-Z and lowercase a-z, and C++ treats the two cases as different characters.
  2. Digits are 0 to 9, used to form numeric constants and parts of identifiers.
  3. Special symbols include + - * / % = < > ( ) { } [ ] ; , . # & ! _ and quotes.
  4. White space (blank, tab, newline) separates tokens and is ignored by the compiler except inside strings.

Constants

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A constant is a fixed value that does not change during execution of the program.</mark>

Key points.

  1. Integer constants have no decimal point, for example 25, -7, 0x1F (hex) and 017 (octal).
  2. Floating constants have a decimal point or exponent, for example 3.14 and 2.5e3.
  3. Character constants are one symbol in single quotes, such as 'A', and string constants are in double quotes, such as "RGPV".
  4. A named constant is made with const float PI = 3.14;, and any later attempt to change it is a compile error.

Asked: [7 marks] (Jun 2020) Explain the term constants, variables, identifiers and literals.

Variables

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A variable is a named memory location whose value can change while the program runs.</mark>

Key points.

  1. A variable must be declared with its type before use, in the form type name; such as int age;.
  2. It may be initialised at declaration, for example float pi = 3.14;.
  3. The type decides how much memory is reserved and which values are allowed.
  4. Declaring two variables of the same name in one scope is an error.

Keywords

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Keywords are reserved words with a fixed meaning to the compiler, and they cannot be used as identifiers.</mark>

Key points.

  1. Examples are int, float, char, if, else, while, for, class, return, static and const.
  2. They are written in lowercase, so Int is legal while int is not usable as a name.
  3. C++ has about 95 keywords, more than C, including class, public, private, virtual and new.

Identifiers and literals

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>An identifier is the programmer-chosen name of a variable, function, class or array; a literal is a fixed value written directly in the source code.</mark>

Key points.

  1. An identifier may contain letters, digits and underscore, and it must not begin with a digit.
  2. It cannot contain spaces or symbols and cannot be a keyword.
  3. It is case sensitive, so Total and total are different names.
  4. Kinds of literal are integer (10), floating (1.5), character ('x'), string ("hi") and boolean (true).

Type declaration instruction

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A type declaration instruction tells the compiler the data type of a variable before it is used.</mark>

Key points.

  1. The form is type var1, var2 = value;, for example int a, b = 5;.
  2. Declarations tell the compiler how much memory to reserve for each variable.
  3. A variable that is declared but not initialised holds a garbage value if it is a local variable.
  4. In C the declarations come at the start of a block, while C++ allows them anywhere before use.

Integer, long and short types

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Integer types store whole numbers, and the qualifiers short and long change the size and range.</mark>

Key points.

  1. Typical sizes on a 32-bit or 64-bit compiler are short 2 bytes, int 4 bytes, long 4 or 8 bytes and long long 8 bytes.
  2. An int occupies 4 bytes, so its signed range is -2147483648 to 2147483647.
  3. short saves memory for small values, while long holds larger values.
  4. Real numbers use float (4 bytes) and double (8 bytes), and sizeof(type) gives the exact size.

Signed and unsigned

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A signed type can hold negative and positive values, while an unsigned type holds only zero and positive values.</mark>

Key points.

  1. Signed is the default for int, and one bit stores the sign.
  2. With $n$ bits the signed range is $-2^{n-1}$ to $2^{n-1}-1$ and the unsigned range is $0$ to $2^n-1$.
  3. For 16 bits, signed is -32768 to 32767 and unsigned is 0 to 65535.
  4. Assigning a value out of range wraps around, so unsigned char u = 300; stores 44.

Storage classes

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A storage class decides the scope, lifetime, default value and storage place of a variable.</mark>

Key points.

  1. auto is the default for local variables, which live in the stack, have block scope and start with a garbage value.
  2. register requests that the variable be kept in a CPU register for speed, and its address cannot be taken.
  3. static keeps a local variable alive for the whole program with default value 0, and it is initialised only once.
  4. extern declares a global variable defined in another file, which has program-wide scope and default value 0.
Class Storage Default Scope Lifetime
auto Stack Garbage Block Block
register CPU register Garbage Block Block
static Data area 0 Block Program
extern Data area 0 Global Program

Integer and float conversions

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>

Definition. <mark>Type conversion is the change of a value from one data type to another, either automatically by the compiler (implicit) or by the programmer (explicit, casting).</mark>

Key points.

  1. Implicit conversion promotes the smaller type to the larger one in an expression (char to int to float to double), so 'A' + 1 is 66.
  2. Explicit conversion is written (float)i or float(i), so (float)7/2 is 3.5 while 7/2 is 3.
  3. Class-type conversion is done by a constructor (basic to class) or a conversion operator function operator float() (class to basic); class to class uses either of them.
class Meter { float m; public:
  Meter(float x = 0) : m(x) {}          // basic to class
  operator float() { return m; } };     // class to basic
int main() { int i = 7; float a = i;     // implicit
  float b = (float)i / 2;                // explicit: 3.5
  Meter d = 2.5f; float x = d; }         // x = 2.5

Answer frame. Open with the definition of type conversion; then implicit with program, explicit with program, then the three class conversions; close with the note that a narrowing conversion may lose data.

Asked: [7 marks] (Jun 2020) Explain with programs the various types of type conversions.

Type conversion in assignment

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>In an assignment the right-hand value is converted to the type of the left-hand variable.</mark>

Key points.

  1. Assigning a float to an int drops the fractional part, so int a = 3.9; stores 3.
  2. Assigning an int to a float stores it with a decimal part, so float f = 7; stores 7.0.
  3. Assigning a larger integer type to a smaller one may lose the high bits, so short s = 40000; stores -25536.
  4. In float f = 7/2; integer division happens first, so f is 3.0, not 3.5.

Hierarchy of operations

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Hierarchy (precedence) decides which operator is evaluated first, and associativity decides the order among operators of equal priority.</mark>

Key points.

  1. Order from high to low is: (), unary - ! ++ --, then * / %, then + -, then relational, then &&, then ||, then =.
  2. * / % and + - associate left to right, and assignment associates right to left.
  3. Example: 2 + 3 * 4 - 6 / 2 % 2 = 2 + 12 - ((6/2)%2) = 2 + 12 - 1 = 13.
  4. Parentheses override the default order.

Last-minute revision

  1. A constant never changes, a variable is a named changeable memory location, and an identifier is a programmer-chosen name.
  2. Identifiers start with a letter or underscore, are case sensitive and cannot be keywords.
  3. Literals are integer, floating, character, string and boolean values written in code.
  4. Sizes: short 2, int 4, float 4, double 8 bytes; int range is -2147483648 to 2147483647.
  5. Signed range $-2^{n-1}$ to $2^{n-1}-1$, unsigned $0$ to $2^n-1$.
  6. unsigned char u = 300; stores 44.
  7. Storage classes are auto, register, static, extern; static and extern default to 0.
  8. 7/2 is 3 but (float)7/2 is 3.5.
  9. Class conversion uses a constructor (basic to class) and operator type() (class to basic).
  10. 2 + 3 * 4 - 6 / 2 % 2 is 13.

Memory hooks

  • CVIL: Constant, Variable, Identifier, Literal, the four terms of the 7-mark question.
  • ARSE: storage classes Auto, Register, Static, Extern.
  • Promote up, truncate down: small to large is safe, large to small loses data.
  • BODMAS-like: brackets, unary, multiply/divide/mod, add/subtract, relational, logical, assign.

Coverage checklist

  • Character set: covered.
  • Constants: covered; Jun 2020 constants, variables, identifiers, literals question.
  • Variables: covered; same Jun 2020 question.
  • keywords: covered.
  • identifiers literals: covered; same Jun 2020 question.
  • Instructions: Type Declaration Instruction: covered.
  • arithmetic Integer Long Short: covered.
  • Signed unsigned: covered.
  • storage classes: covered.
  • Integer and Float Conversions: covered; Jun 2020 type conversions question.
  • type conversion in assignment: covered.
  • hierarchy of operations: covered.
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in