Program Constructs
Program Constructs in Object-Oriented Development
Variables and Data Types
- Variables are named storage locations in memory.
- Primitive data types include int (integer), float (floating point number), bool (boolean), char (character), and string (a sequence of characters).
- Variables can also hold objects - instances of user-defined or built-in classes.
Control Structures
- If-else conditional control structure is used to execute a certain block of code if a certain condition is met, and another block if not.
- Switch-case statements are also used for multiple conditions, where each condition or ‘case’ can correspond to a different action.
- Loops such as for, while, and do while allow execution of a block of code multiple times, usually until a certain condition is met.
- Jump statements like break and continue can control the flow within the loops and conditional structures.
Functions And Methods
- A function is a block of code that accomplishes a specific task, and can be called by its name. It can accept input parameters and optionally return a value.
- In object-oriented programming, functions associated with objects are known as methods.
- A method in a class can access and modify the data members of that class, which is encapsulated within the object.
Arrays and Lists
- An array is a container object that holds a fix number of values of a single type.
- A list, or dynamically-sized array, can grow or shrink as needed. In many languages, it can hold values of different types.
- Arrays and lists are used to store multiple values in a single variable.
Classes and Objects
- A class is a blueprint or template for creating objects (a particular data structure).
- An object has properties and behaviours, defined in the class.
- Inheritance, polymorphism, and encapsulation are key principles of object-oriented design.
Exception Handling
- Exceptions are events that occur during the execution of programs that disrupt the normal flow of instructions.
- Try-catch blocks can handle exceptions, where the code in the ‘try’ block is executed, and if any exception occurs, it is caught and handled in the ‘catch’ block.
- Proper exception handling is essential for building robust software.