Lab Report for Lab 3: Complex Number Class
2.1 Rationale for Class Members
The class ComplexNumber was designed to represent complex numbers in the Cartesian plane, where
each complex number has a real component and an imaginary component. The following members were
included in the class:
1. Private Attributes:
o double real;: This stores the real part of the complex number.
o double imaginary;: This stores the imaginary part of the complex number.
2. Constructors:
o A default constructor initializes the complex number to (0,0).
o An overloaded constructor initializes the complex number with given real and imaginary values.
This provides flexibility in creating complex numbers.
3. Getters and Setters:
o Getter methods for both real and imaginary components allow controlled access to these private
variables. Setter methods enable updating these values while maintaining encapsulation.
4. Member Function print:
o This function prints the complex number in a user-friendly format, such as a + bi. This is essential
for visual representation and debugging purposes.
5. Operator Overloading Functions:
o Overloading operators (+, -, *, /, ==) allows intuitive arithmetic operations on complex numbers.
This makes the code more readable and user-friendly, enabling users to perform calculations
naturally.
2.2 Changes to Class Declaration
During the implementation and extension of the class, several changes were made to the header file
ComplexNumber.h:
Operator Overloading:
o The addition of operator overloads for +, -, *, /, and == functions. This allowed for direct arithmetic
operations on instances of the class, improving usability.
Parameter Types:
o Adjusted the parameter types for the overloaded operators to ensure they accept instances of the
ComplexNumber class and appropriate scalar types where necessary.
Return Types:
o Ensured that the functions return types aligned with their intended functionality, e.g., returning a
boolean for the == operator to indicate equality.
2.3 Screenshots of Output
Output of Test Cases
2.4 Objectives and Concepts Explored
This lab focused on creating a ComplexNumber class that encapsulates complex number operations.
Key objectives included:
• Understanding the principles of object-oriented programming, particularly encapsulation,
abstraction, and operator overloading.
• Gaining practical experience in class design, including the implementation of constructors,
getters, setters, and operator overloading methods.
Importance to Course and Career
Understanding these concepts is vital for anyone pursuing a career in computer science or engineering,
as they form the basis of software development practices. Proficiently using classes and objects allows
developers to create reusable code and maintainability, ultimately leading to more robust applications.
Design Considerations
Initially, the class was designed to cover basic functionality for complex numbers, including initialization
and representation. As tasks progressed, the following changes were made:
1. Task 2 - Implementation:
o Improved function implementation and ensured all functions were properly defined in the
ComplexNumber.cpp file.
2. Task 3 - Extension:
o Added operator overloads for intuitive arithmetic, which improved usability and user
experience.
3. Task 4 - Testing:
o Enhanced the main program to ensure all member functions were thoroughly tested,
confirming the correctness of operations.
When designing classes, it's essential to consider:
• Modularity: Keeping related functionality together within the class.
• Encapsulation: Protecting data and providing controlled access through public methods.
• Usability: Making operations intuitive through operator overloading and user-friendly output.