Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complex Number Class

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, I'm new to this C++ thing and I have a question. I have this sample problem and it asks me to "Provide a constructor function that enables an object of this class to be intialized when it is declared" The constructor should contain default values in case no intializers are provided. What in the world are they talking about!!

My class name is Complex and it has a realpart and imagpart. Will someone please help, thanks!
 
the constructor in the .h file would look something like

class foo{
public:
foo(int val = 0);
private:
int m_val;
}

and the cpp would look like

foo::foo(int val):m_val(val)
{
}

implimentation would look like

foo myFoo; // zero default
foo initFoo(7); m_val gets set to 7

Matt

 
u can do something like this

/**********************************************/
//class decleration .h file
/**********************************************/
class Complex
{
int real, imag;
public:
Complex(); //constructor with no arguments
//OR
Complex(int a=1,int b=1); //constructor with default parameters
~Complex();

}


/**********************************************/
//class implementation .cpp file
/*********************************************/


//it will be called when u declare a Complex object
Complex::Complex()
{
real=0;
imag=0;
}

Complex::Complex(int a ,int b)
{
real=a;
imag=b;
}


//somewhere in ur program
Complex z; //z will be (0,0)
Complex y(5); //y=(5,1)
Complex zy(5,12); //obviously zy=(5,12)



hope this helps
 
It makes more sense dima2 but I'm not understanding the last 3 lines. What is that Complex z etc... I'm not understanding why Complex y(5) would be (5,1) Wouldn't it be (5,0) since you declared the imaginary part as 0?? Thanks
 
You also need to look at what constructor he is calling. However...


Complex(); //constructor with no arguments
//OR
Complex(int a=1,int b=1); //constructor with default parameters

Complex(int a = 1,int b = 1) will have problems. This one constructor represents

Complex()
Complex(x);
complex(x,y);

There should be multiple definition errors.

Matt
 
So what is the proper way to intialize variables using the constructor???
 


foo::foo(int a, int b):member_var_a(a), member_var_b(b)
{
}

OR

foo::foo(int a, int b)
{
member_var_a = a;
member_var_b = b;
}

Matt
 
I have the following class

**********************************************
class complex{

public:
complex(double , double);
double addem(double, double);
double subem(double, double);
void print();

private:

int real, imag;
}

complex::complex(double r, i)
{
real = r;
imag = i;

}

void complex::print()
{
cout << &quot;(&quot; << real << &quot;,&quot; << imag;
}

double complex::addem(double real, double imag)
{
return (real + imag);
}

double complex::subem(double real, double imag)
{
return (real - imag);
}
**********************************************

For the main.cpp prog to use this class is the below correct? If not what should it look like, thanks...


#include <iostream>
#include <complex.h>

int main()
{
complex c;

cin << c.real << c.imag;
c.addem(real, imag);
c.print();

cin << c.real << c.imag;
c.subem(real, imag);
c.print();
}




return 0;
 
Yes and no on the class... you dont need params to addem and subem because the member vars are initialized to the original values. Also, your main needs a few changes:

int main()
{
complex c;

cin >> c.real >> c.imag;
c.addem(real, imag); // real and imag undeclared
c.print();

cin >> c.real >> c.imag;
c.subem(real, imag);// real and imag undeclared
c.print();

return 0;

}

Please note the change on the cin as well... bold doesnt show in preview.

Matt


 
I don't understand why real and imag are undeclared. Do I need to say c.subem(c.real, c.imag) I thought they are declared because I am calling them from within complex by using the c.subem.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top