Exercise 4: Complex Numbers
Use the class template to define a class that returns the real and imaginary parts of a complex number.
We'll cover the following
Problem statement
Use class templates to write a template-type class called Complex
. This class contains the following properties:
Two
private
variables are the real and imaginary elements of a complex number.A constructor that takes the real and imaginary parts of a complex number as input and initializes them.
Contains the following two 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 type.
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