C++ assignment involving classes

In summary, two students are discussing a homework assignment involving creating a polynomial class using arrays of rationals from a rational class. One student is struggling with implementing operator overloading for addition and is asking for help. They discuss using the data member "coeff" and using the brackets [] to access it, and also mention using a debugger for help. The other student provides advice and suggests using the construct "c.coeff[i]" instead of "c[i]" and using if-statements for overloading the output operator.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
Ugh! I'm working on my last homework assignment and I can't seem to even get off the ground. :cry:
The teacher created a class for rational numbers, and now we are working on some functions for a polynomial class which uses arrays of rationals from his class. My polynomials are arrays of coefficients with each stored at an index position representing the power of x the coefficient is multiplied to.
I'm embarrassed to ask for help because there are 4 files involved: the rational class header and implementation file and the polynomial class header and implementation file.
I know you people have better things to do this weekend, but if anyone is really bored, the assignment and files are here:
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/hw6.html
It's the polynomial.cpp file where we have to fill in the blanks.
One of the first things I tried to do was the operator overloading to define addition for polynomials. Here's what I tried:
Code:
// operator+
polynomial operator+(const polynomial & a,const polynomial & b)
{
	polynomial c;
	for (int num = 0; num < polynomial::SIZE; num ++) //fill array with zeros
		c[num] = 0;
	for (int i = 0; i < polynomial::SIZE; i ++) //compute sum
		c[i] = a[i] + b[i];
	return c;
}
So far, not so good. It would be a huge help if someone could help me get just this piece fixed up. Thanks,
MIH
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Why does the class's name not appear in the declaration of the function? Does this function appear in the header, inside the class declaration, or in the implementation file?

- Warren
 
  • #3
Math Is Hard said:
Ugh! I'm working on my last homework assignment and I can't seem to even get off the ground. :cry:
The teacher created a class for rational numbers, and now we are working on some functions for a polynomial class which uses arrays of rationals from his class. My polynomials are arrays of coefficients with each stored at an index position representing the power of x the coefficient is multiplied to.
I'm embarrassed to ask for help because there are 4 files involved: the rational class header and implementation file and the polynomial class header and implementation file.
I know you people have better things to do this weekend, but if anyone is really bored, the assignment and files are here:
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/hw6.html
It's the polynomial.cpp file where we have to fill in the blanks.
One of the first things I tried to do was the operator overloading to define addition for polynomials. Here's what I tried:
Code:
// operator+
polynomial operator+(const polynomial & a,const polynomial & b)
{
	polynomial c;
	for (int num = 0; num < polynomial::SIZE; num ++) //fill array with zeros
		c[num] = 0;
	for (int i = 0; i < polynomial::SIZE; i ++) //compute sum
		c[i] = a[i] + b[i];
	return c;
}
So far, not so good. It would be a huge help if someone could help me get just this piece fixed up. Thanks,
MIH

The polynomial class has a data member coeff that holds the coefficients. Thus, need to use the construct:

c.coeff=a.coeff+b.coeff;

Right?

Oh yea, be nice if you could run this through a debugger. Microsoft Visual C++ has a nice one.
 
Last edited by a moderator:
  • #4
MIH,

In general, please tell also provide us the compiler (and linker) output when asking questions like this. It's really hard (if not impossible) to diagnose a piece of code like this taken from a larger piece of unknown code. You didn't even tell us why it was "not so good."

- Warren
 
  • #5
ok, sorry. I am using MS VC++ v 6. The whole thing (all four files) is at the link I listed in my OP. The class name is polynomial. It's defined in polynomial.h.
Saltydog - yes I believe that's the problem I was having. It was not using that coeff data member. That's where I was screwing up, I bet.
I wasn't sure if it was ok for me to use the brackets [], because I was getting errors (among the jillions of other errors) about [ not being defined. I felt so lost I didn't even know how to describe the problem I was having because there were so many.
Thanks so much for looking at that. I'll go work on this some more.
 
  • #6
Math Is Hard said:
about [ not being defined.

You got that error because using the construct c implies an "array" of polynomials which you did not define. Use the same c.coeff construct for operator-, a little more complicated looping for operator*, and some if-statements for overloading the output operator and return "os" after you've filled it up with the polynomial.
 
  • #7
cheers, SD! this helps a lot.
 

1. What is a class in C++?

A class in C++ is a user-defined data type that encapsulates data and functions together. It serves as a blueprint for creating objects, which are instances of a class. Classes are essential for creating modular and reusable code in C++.

2. How do you declare a class in C++?

To declare a class in C++, you use the class keyword followed by the name of the class and a pair of curly braces. Inside the braces, you can declare member variables and member functions. For example:

class Car {
    private:
        int speed;
        string brand;
    public:
        void accelerate();
        void brake();
        void startEngine();
        void stopEngine();
};

3. How do you define member functions in a class?

Member functions in a class can be defined inside or outside of the class declaration. To define a member function inside the class, you use the inline keyword before the function's return type. To define a member function outside of the class, you use the class_name:: scope resolution operator before the function name. For example:

class Car {
    public:
        void accelerate() {
            // function body
        }
        void brake(); // defined outside of the class
};
void Car::brake() {
    // function body
}

4. How do you create an object of a class in C++?

To create an object of a class in C++, you use the new keyword followed by the name of the class and a set of parentheses. The parentheses can contain arguments to pass to the class constructor, if it has any. For example:

Car* myCar = new Car(); // creates a new Car object
Car* yourCar = new Car("Ford"); // creates a new Car object with brand set to "Ford"

5. How do you access member variables and functions of a class object?

To access member variables and functions of a class object, you use the dot operator (.) between the object name and the member name. For example:

myCar.brand = "Tesla"; // sets the brand of myCar to "Tesla"
myCar.accelerate(); // calls the accelerate function of myCar

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
830
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
984
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
45
Views
4K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top