Member Avatar for Katara1
Katara1

how to let add &subtract methods work at main!!

#include<iostream.h>
class complex{
    private:
    double real,imag;
    public:
    complex();// default constructor
    complex(double r,double i)
    {
        real=r;imag=i;
    };
void complex::setComplexNumber(double r,double i)
{
    real=r;imag=i;
}
complex complex::add(complex x)
{
    complex a;
    a.real=real+x.real;
    a.imag=imag+x.imag;
    return a;
}
complex complex::subtract(complex x)
{
    complex s;
    s.real=real-x.real;
    s.imag=imag-x.imag;
    return s;
}
void print(){

    cout<<"("<<real<<","<<imag<<")"<<endl;
 }
 double getReal() {
  return real;
 }
 double getImag() {
  return imag;
 }
 void setReal(double r){
  real = r;

 }
 void setImag(double i){
        imag = i;
 }

};
main()
{
    double real1,imag1,real2,imag2;
    cout<<"Enter the Real part of first Number: ";
    cin>>real1;
    cout<<"Enter the imaginary part of first Number: ";
    cin>>imag1;
    complex obj1(real1,imag1);
    obj1.print();
    cout<<"Enter the real part of second Number: ";
    cin>>real2;
    cout<<"Enter the imaginary part of second nomber: ";
    cin>>imag2;
    complex obj2(real2,imag2);
    obj2.print();

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.