3 Replies - 709 Views - Last Post: 21 October 2009 - 07:13 AM Rate Topic: -----

#1 chromeman605  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Attached File  ece231assn6.doc (30K)
Number of downloads: 52

#include <iostream>
#include <cmath>

using namespace std;

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

quad::quad()
{
}

quad::quad(double)
{
        a=b=c;
}

quad::quad(double, double, double)
{
}

void quad::solve()
{
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 quad::vertex()
{
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;
}

bool quad::direction()
{

        if(a<0)
        {
    }
        }
}

dim2 quad::vertex()
{
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;
}

bool quad::direction()
{

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

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


*** MOD EDIT: Added code tags. Please :code: ***

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  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: classes, friend functions, and overloaded operators

Posted 20 October 2009 - 11:39 AM

Hello

View Postchromeman605, 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 attachment

#include <iostream>
#include <cmath>

using namespace std;

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

quad::quad()
{
}

quad::quad(double)
{
        a=b=c;
}

quad::quad(double, double, double)
{
}

void quad::solve()
{
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 quad::vertex()
{
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;
}

bool quad::direction()
{

        if(a<0)
        {
    }
        }
}

dim2 quad::vertex()
{
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;
}

bool quad::direction()
{

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

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


*** MOD EDIT: Added code tags. Please :code: ***


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
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: classes, friend functions, and overloaded operators

Posted 20 October 2009 - 11:49 AM

Overloading Operators Tutorial

Generic example of overloading the out stream


Post your exact question in the post. I won't download extraneous files to read the problem.
Was This Post Helpful? 0
  • +
  • -

#4 chromeman605  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;

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

quad::quad()
{
		a=b=c=0.0;
}

quad::quad(double x)
{
		a=b=c=x;
}

quad::quad(double x, double y, double z)
{
		a=x;
		b=y;
		c=z;
}

void quad::solve()
{
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 quad::vertex()
{
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;
}

bool quad::direction()
{

		if(a<0)
		{
				return 0;
		}

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


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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1