# classes, friend functions, and overloaded operators

Page 1 of 1

## 3 Replies - 850 Views - Last Post: 21 October 2009 - 07:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=133223&amp;s=0ca803fc361f74990594d1127c6f9477&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chromeman605

Reputation: 0
• Posts: 11
• Joined: 28-August 09

# classes, friend functions, and overloaded operators

Posted 20 October 2009 - 09:10 AM

So I am having trouble using mostly with the overloaded operators and friend functions. Attached is the assignment I am working on and I don't know where to start on parts 6 and 7. Any help would be appreciated!
ece231assn6.doc (30K)

```#include <iostream>
#include <cmath>

using namespace std;

{
private:
double a;
double b;
double c;
public:
bool operator==( const quad & );
void solve();
dim2 vertex();
bool direction();
friend ostream & operator<<( ostream &, const quad & );
friend istream & operator>>( istream &, quad & );
};

{
}

{
a=b=c;
}

{
}

{
double r1=0;
double r2=0;
double d=0;
d=((b*b )-(4*a*c));

if (d>0))
{
r1=(((-1)* b ) + sqrt(d))/(2*a)
r2=(((-1)* b ) - sqrt(d))/(2*a)

if (r1==r2)
{
cout<<"The root is: "<<r1<<endl;
}
else
{
cout<< "The roots are "<< r1 << "and "<< r2<<endl;
}

}

if (d<0)
{
d=d*(-1);
r1=(((-1)* b ) + sqrt(d))/(2*a)
r2=(((-1)* b ) - sqrt(d))/(2*a)

if (r1==r2)
{
cout<<"The root is: "<<r1<<"j"<<endl;
}
else
{
cout<< "The roots are "<< r1 << "j and "<< r2<< "j" <<endl;
}
}
}

{
double h=0.0;
double k=0.0;

h=((-1)*b )/(2*a);
k=((4*a*c)-(b*b ))/(4*a);
cout<<"The vertex is: (" << h << "," << k << ")" << endl;
}

{

if(a<0)
{
}
}
}

{
double h=0.0;
double k=0.0;

h=((-1)*b )/(2*a);
k=((4*a*c)-(b* b ))/(4*a);
cout<<"The vertex is: (" << h << "," << k << ")" << endl;
}

{

if(a<0)
{
cout<<"Concave down";
return 0;
}

if(a>0)
{
cout<<"Concave up";
return 1;
}
}
```

This post has been edited by JackOfAllTrades: 20 October 2009 - 10:38 AM

Is This A Good Question/Topic? 0

## Replies To: classes, friend functions, and overloaded operators

### #2 jwwicks

Reputation: 24
• Posts: 162
• Joined: 31-July 08

## Re: classes, friend functions, and overloaded operators

Posted 20 October 2009 - 11:39 AM

Hello

chromeman605, on 20 Oct, 2009 - 08:10 AM, said:

So I am having trouble using mostly with the overloaded operators and friend functions. Attached is the assignment I am working on and I don't know where to start on parts 6 and 7. Any help would be appreciated!
attachment

```#include <iostream>
#include <cmath>

using namespace std;

{
private:
double a;
double b;
double c;
public:
bool operator==( const quad & );
void solve();
dim2 vertex();
bool direction();
friend ostream & operator<<( ostream &, const quad & );
friend istream & operator>>( istream &, quad & );
};

{
}

{
a=b=c;
}

{
}

{
double r1=0;
double r2=0;
double d=0;
d=((b*b )-(4*a*c));

if (d>0))
{
r1=(((-1)* b ) + sqrt(d))/(2*a)
r2=(((-1)* b ) - sqrt(d))/(2*a)

if (r1==r2)
{
cout<<"The root is: "<<r1<<endl;
}
else
{
cout<< "The roots are "<< r1 << "and "<< r2<<endl;
}

}

if (d<0)
{
d=d*(-1);
r1=(((-1)* b ) + sqrt(d))/(2*a)
r2=(((-1)* b ) - sqrt(d))/(2*a)

if (r1==r2)
{
cout<<"The root is: "<<r1<<"j"<<endl;
}
else
{
cout<< "The roots are "<< r1 << "j and "<< r2<< "j" <<endl;
}
}
}

{
double h=0.0;
double k=0.0;

h=((-1)*b )/(2*a);
k=((4*a*c)-(b*b ))/(4*a);
cout<<"The vertex is: (" << h << "," << k << ")" << endl;
}

{

if(a<0)
{
}
}
}

{
double h=0.0;
double k=0.0;

h=((-1)*b )/(2*a);
k=((4*a*c)-(b* b ))/(4*a);
cout<<"The vertex is: (" << h << "," << k << ")" << endl;
}

{

if(a<0)
{
cout<<"Concave down";
return 0;
}

if(a>0)
{
cout<<"Concave up";
return 1;
}
}
```

Let's start with the constructors. Shouldn't these be doing something with the values being passed in ??

```quad::quad(){} // This should initialize a, b and c to some KNOWN value say 0.0
quad::quad(double x){ a=b=c;} //This should initialize a,b,c to the value in x
quad::quad(double x, double y, double z){} //This should initialize a to x, b to y and c to z

```

You could get by with one constructor if you use default values
```quad::quad(double x=0.0, double y=0.0, double z=0.0)
{ } //This should initialize a to x, b to y and c to z

```

Jw

### #3 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: classes, friend functions, and overloaded operators

Posted 20 October 2009 - 11:49 AM

### #4 chromeman605

Reputation: 0
• Posts: 11
• Joined: 28-August 09

## Re: classes, friend functions, and overloaded operators

Posted 21 October 2009 - 07:13 AM

Okay, this is a more updated code. Now how do I use the copy constructor, assignment operator, addition operator, subtraction operator, equals comparison operator, and increment and decrement operators for this code? I'm a little lost with that. Also how would I create friend functions that input/output values?

```#include <iostream>
#include <cmath>

using namespace std;

{
private:
double a;
double b;
double c;
public:
bool operator==( const quad & );
void solve();
dim2 vertex();
bool direction();
friend ostream & operator<<( ostream &, const quad & );
friend istream & operator>>( istream &, quad & );
};

{
a=b=c=0.0;
}

{
a=b=c=x;
}

{
a=x;
b=y;
c=z;
}

{
double r1=0;
double r2=0;
double d=0;
d=((b*B)-(4*a*c));

if (d>0))
{
r1=((-1*B) + sqrt(d))/(2*a)
r2=((-1*B) - sqrt(d))/(2*a)
if (r1==r2)
{
cout<<"The root is: "<<r1<<endl;
}
else
{
cout<< "The roots are "<< r1 << "and "<< r2<<endl;
}

}

if (d<0)
{
d=d*(-1);
r1=((-1*B) + sqrt(d))/(2*a)
r2=((-1*B) - sqrt(d))/(2*a)

if (r1==r2)
{
cout<<"The root is: "<<r1<<"j"<<endl;
}
else
{
cout<< "The roots are "<< r1 << "j and "<< r2<< "j" <<endl;
}
}
}

{
dim2 v;
double h=0.0;
double k=0.0;

h=((-1)*B)/(2*a);
k=((4*a*c)-(b*B))/(4*a);
v.set(h,k);
return v;
}

{

if(a<0)
{
return 0;
}

if(a>0)
{
return 1;
}
}

```

This post has been edited by chromeman605: 21 October 2009 - 07:14 AM