Exercise 4: Complex Numbers

Let's test your understanding of the Class Template by using it to define a class that returns the real and imaginary parts of a Complex number.

Problem Statement

In this exercise, you are required to use Class templates to write template type class called Complex. This class contains the following properties:

  • Two private variables: The real and imaginary elements of a complex number.

  • Constructor that takes real and imaginary parts of a complex number as input and initializes them.

  • Contains the two following member functions:

    • get_real(): returns the real part of the complex number.

    • get_im(): returns the imaginary part of the complex number.

The code should be generalized; hence should work for any data types.

Sample input

Create a free account to access the full course.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy