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.
We'll cover the following
Problem Statment
In this exercise, you are required to use Class templates.
Make a template type class called Complex
.
-
Contains two
private
variables:- The real and imaginary element of a complex number.
-
Has a constructor taking real and imaginary parts a complex number as input and initializing those.
-
Has two other 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.
Hint: Use
template
type for this purpose.
Down below is what the expected output should look like.
Input :
int x=500, y=100;
Expected Output:
Real part of the complex number is: 500
Imaginary part of the complex number is: 100
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy