Assignment 1 4
Assignment 1 4
- **Run-time polymorphism** (method overriding): Resolving method calls at run time through virtual
functions.
Inheritance is a mechanism where a new class (derived class) can inherit attributes and methods from an
existing class (base class). This promotes code reusability and establishes a natural hierarchy between
classes.
- **Types of Inheritance**:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data
within a single unit or class. It restricts direct access to some of the object's components, which is a
means of preventing unintended interference and misuse of the methods and data. Access specifiers
(public, private, protected) are used to enforce encapsulation.
### 4. Abstraction (2)
Abstraction is the concept of hiding the complex reality while exposing only the necessary parts of an
object. It helps in reducing programming complexity and increases efficiency by allowing the
programmer to focus on interactions at a higher level. In C++, abstraction can be achieved through
abstract classes and interfaces.
---
A single line comment in C++ is used to annotate code and is ignored by the compiler. It is initiated with
two forward slashes (`//`).
```cpp
```
Pre-processor directives are commands that are processed before actual compilation begins. They
typically start with a `#` symbol. For example, `#include` is used to include libraries.
```cpp
#include <iostream>
```
```cpp
```
A C++ statement is an instruction that the compiler can execute. It can be a declaration, assignment, or
control flow statement.
```cpp
```
A local variable is declared within a function and can only be accessed within that function. It is created
when the function is called and destroyed when the function exits.
```cpp
void exampleFunction() {
}ASSIGNMENT 2
```Here's a simple Arduino program to control three LEDs representing traffic lights: GREEN, YELLOW,
and RED. Each LED will be turned on for 5 seconds in the specified sequence.
```cpp
// Define pin numbers for the LEDs
void setup() {
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
void loop() {
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
```
## Explanation:
- **Loop Function**:
- Turns on each LED in sequence, with a delay of 5 seconds for each LED.
Upload this code to your Arduino, and it will control the LEDs as specified!#
ASSIGNMENT 3
Question 1: Programming Tools
A flow chart is a visual representation of a process or algorithm. It uses shapes such as rectangles,
diamonds, and arrows to illustrate the steps involved and the flow of control. Flow charts help in
understanding the logic of a program and can be useful for debugging and communication.
Pseudocode is a high-level description of an algorithm that uses a mix of natural language and
programming constructs. It is not meant to be executed but serves as an intermediary step between an
algorithm and actual code, allowing programmers to focus on logic without getting bogged down by
syntax specifics.
An editor is a software application used for writing and modifying code. Text editors can range from
simple ones like Notepad to advanced Integrated Development Environments (IDEs) that offer features
like syntax highlighting, code completion, and debugging tools. Editors help programmers write, test,
and maintain their code efficiently.
An argument is a value or reference passed to a function or method when it is called. Arguments provide
the necessary inputs that the function uses to perform its task. They can be of various data types,
including integers, strings, or objects, depending on the function's requirements.
---
An identifier is a name used to identify a variable, function, class, or any other user-defined item in a
program. Identifiers are essential for referencing these elements in the code, and they must be unique
within their scope.
### 2. Outline the Rules for Naming Identifiers (5)
1. **Character Set**: Identifiers can only contain letters (uppercase and lowercase), digits (0-9), and
underscores (_). They cannot begin with a digit.
2. **Case Sensitivity**: Identifiers are case-sensitive; for example, `variable` and `Variable` are
considered different identifiers.
3. **Length**: There is generally no fixed limit on the length of identifiers, but it's best to keep them
reasonable for readability.
4. **Reserved Keywords**: Identifiers cannot be the same as reserved keywords or built-in function
names in the programming language.
5. **Descriptive Names**: While not a strict rule, it's good practice to use meaningful and descriptive
names that reflect the purpose of the identifier.
The syntax for declaring an array in most programming languages (e.g., C++, Java) typically looks like
this:
```cpp
dataType arrayName[arraySize];
```
**Example**:
```cpp
## For Loop
- **Definition**: The `for` loop is used when the number of iterations is known beforehand. It consists
of three parts: initialization, condition, and increment/decrement.
- **Syntax**:
```cpp
// Code to execute
```
- **Use Case**: Ideal for iterating over arrays or when the exact number of iterations is predetermined.
## While Loop
- **Definition**: The `while` loop continues to execute as long as its condition is true. The condition is
checked before each iteration.
- **Syntax**:
```cpp
while (condition) {
// Code to execute
```
- **Use Case**: Best used when the number of iterations is not known in advance, and the loop should
continue until a certain condition is met.
## Do-While Loop
- **Definition**: The `do-while` loop is similar to the `while` loop, but it guarantees at least one
execution of the loop body since the condition is checked after the loop's body is executed.
- **Syntax**:
```cpp
do {
// Code to execute
} while (condition);
```
- **Use Case**: Useful when the loop body must run at least once, such as when prompting for user
input.
1. **Execution Guarantee**:
- **Do-While Loop**: Executes the body at least once, checking the condition afterward.
2. **Use Cases**:
- **Do-While Loop**: Similar to the while loop regarding initialization and increment.
4. **Readability**:
- **For Loop**: Often more concise and easier to read for fixed iterations.
- **While Loop**: More flexible but can be less clear for complex conditions.
4. **bool**: Represents a boolean value (true or false) (e.g., `bool isPassed = true;`).
### Constructors
- **Definition**: A constructor is a special member function that is automatically called when an object
of a class is created. It has the same name as the class and no return type.
- **Purpose**:
- **Initialization**: Constructors initialize object attributes, ensuring they start with valid values.
- **Overloading**: Multiple constructors can be defined with different parameters, allowing for
flexible object creation.
- **Resource Management**: They can allocate resources (like memory) needed for the object.
### Destructors
- **Definition**: A destructor is a special member function that is automatically called when an object
goes out of scope or is deleted. It has the same name as the class but is prefixed with a tilde (~) and has
no return type or parameters.
- **Purpose**:
- **Cleanup**: Destructors release resources that were acquired during the object's lifetime, such as
memory or file handles, to prevent memory leaks.
- **Finalization**: They can perform any cleanup operations before the object is destroyed, ensuring
that all operations are properly concluded.
Both constructors and destructors are crucial for managing the lifecycle of objects in C++. They ensure
that resources are properly allocated and deallocated, which is essential for writing efficient and reliable
code. Proper use of these features helps maintain the integrity of resource management, avoids
memory leaks, and facilitates easier debugging and maintenance.