@Bazzy
Create a temporary?
I could not understand you say
Imaginary constructor is private, why is it?
And why does its prototype have the Imaginary::
prefix if it's in the class body?
@Bazzy
I dont want to create object the Imaginary type in main
@Bazzy;
the following?
I want to create only with through complex class;
I do not want to create of "Imaginary type" with another way
@Disch
while my Imaginary constructor is private constructor
How do I create a new object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#pragma once
#ifndef _IMAGINARY_HPP_
#define _IMAGINARY_HPP_
class Imaginary{
double i;
Imaginary(double ii = 0.0):i(ii){}
public:
Imaginary (const Imaginary &r);
Imaginary &operator=(const Imaginary &r);
Imaginary operator+(const Imaginary&b)const;
Imaginary operator-(const Imaginary& b)const;
double operator*(const Imaginary& b);
double operator/(const Imaginary& b);
Imaginary setImaginary(double ii);
};
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include "Imaginary.hpp"
#include <iostream>
using namespace std;
Imaginary &Imaginary::operator=(const Imaginary &r)
{
i = r.i;
return *this;
}
Imaginary Imaginary::operator+(const Imaginary&b)const
{
Imaginary result = *this;
result.i += b.i;
return result;
}
Imaginary Imaginary::operator-(const Imaginary& b)const
{
Imaginary result = *this;
result.i -= b.i;
return result;
}
double Imaginary::operator*(const Imaginary& b)
{
return -1*i*b.i;
}
double Imaginary::operator/(const Imaginary& b)
{
Imaginary result = *this;
result.i /= b.i;
return result.i;
}
Imaginary Imaginary::setImaginary(double ii)
{
return Imaginary i(ii);
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#pragma once
#ifndef _COMPLEX_HPP_
#define _COMPLEX_HPP_
#include "Imaginary.hpp"
class Complex{
double a;
Imaginary b;
public:
Complex(double aa, Imaginary bb);
Complex(const Complex &r);
Complex &operator=(const Complex &r);
Complex operator +(const Complex& c);
void set(double ii);
//and so on.
};
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include "Complex.hpp"
#include "Imaginary.hpp"
#include <iostream>
Complex::Complex(double aa, Imaginary bb)
{
a = aa;
b = bb;
}
Complex::Complex(const Complex &r)
{
a = r.a;
b = r.b;
}
Complex &Complex::operator=(const Complex &r)
{
a = r.a;
b = r.b;
return *this;
}
Complex Complex::operator+(const Complex& c)
{
return Complex(a+c.a, b + c.b);
}
void Complex::set(double ii)
{
b.setImaginary(ii);
}
//and so on.
|
1 2 3 4 5 6 7
|
int main()
{
Complex c(2.2, Imaginary(3.4));
return 0;
}
|
Last edited on
You need then to make complex a friend class, and pass two doubles as its constructor
I posted code for a fairly complete working Complex class for you in your last thread on this same subject. What did you do with that?
@fun2code
I could not understand what you say.
the above code does not work