Object Oriented Design and Programming Notes


Constructors

  • Definition: Special method automatically called when an object of a class is created.

  • Naming: Same as the class or structure.

  • Return Type: No return value, hence no return type.

  • Syntax:
    ```cpp
    (list-of-parameters) { //initialize values to the data members }

### Example of Constructor
- **Simple Class Example**:  

cpp
class MyClass {
public:
MyClass() {
cout << "Hello World!";
}
};
int main() {
MyClass myObj;
return 0;
}

---

## Parameterized Constructor
- **Details**: Constructors can take parameters to initialize values.  
- **Example Class**:  

cpp
class Car {
public:
string brand;
string model;
int year;
Car(string x, string y, int z) {
brand = x;
model = y;
year = z;
}
};
int main() {
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}

---

## Types of Constructors
1. **Default/Static Constructor**: No arguments.  
   - Example:  

cpp
class Employee {
public:
Employee() {
cout << "Default Constructor Invoked" << endl;
}
};

2. **Parameterized Constructor**: Takes parameters to initialize different objects.  
   - Example:  

cpp
class Employee {
public:
int id;
string name;
float salary;
Employee(int i, string n, float s) {
id = i;
name = n;
salary = s;
}
};

3. **Copy Constructor**: Initializes an object using another object of the same class.  
   - Syntax:  

cpp
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}

---

## Destructor
- **Definition**: Special member function called to destroy objects created by the constructor.  
- **Name**: Same as the class name but prefixed with a tilde (~).  
- **Return Type**: No return type or arguments. Automatically called when the object goes out of scope.
- **Syntax**:  

cpp
~() {}

---

## Polymorphism
- **Definition**: Ability to present the same interface for different underlying forms (data types).  
- **Types**:  
   - Compile-time Polymorphism  
   - Runtime Polymorphism  
- **Importance**: Allows one interface to have multiple implementations.

---

## Method Overloading
- **Definition**: Multiple functions can have the same name with different parameters.  
- **Example**:  

cpp
void add(int a, int b) { cout << "sum = " << (a + b); }
void add(double a, double b) { cout << "sum = " << (a + b); }

---

## Operator Overloading
- **Definition**: Overloading operators to provide user-defined functionality.  
- **Cannot Overload**:  
   - Scope operator (::), sizeof, member selector (.), member pointer selector (*), ternary operator (?:)  
- **Syntax**:  

cpp
returntype classname::operator op(argument_list) { /* body */ }

### Example of Operator Overloading  

cpp
class Test {
private:
int num;
public:
Test() { num = 8; }
void operator ++() { num += 2; }
};
```


UML Interaction Diagrams

  • Purpose: Describe interactions among elements in a model, focusing on dynamic behavior.

  • Types: Sequence Diagram & Collaboration Diagram.

Sequence Diagram
  • Definition: Represents the time-ordered flow of messages in a system.

  • Purpose:

    • Model interactions among active objects

    • Illustrate high-level interactions

Notations**:
  • Lifeline, Actor, Activation, Messages, Sequence Fragments.

  • Benefits:

    • Easy maintenance and update

    • Real-time application depiction

  • Drawbacks: Complexity with many lifelines.


Collaboration Diagram
  • Definition: Show relationships between objects instead of message flow.

  • Notations: Objects, Actors, Links, Messages

  • Usage: Best suited for analyzing use cases.