Returning a function as a Class Object? Confusing assignment.

Hello,

I am attempting to do a assignment for my cse class, but I am having trouble with returning a class object into a function. If some one could help me out with it it would be great. I seen some topics about this, but after looking through them they didn't seem to be what it is that I needed. I asked my professor , but he has yet to respond even though I sent multiple emails and stopped by his office (IMO he is a horrible professor). I checked online but I can't find anything either that really explains what I need done. So I will post the assignment info ( it is really long, sorry) and I will post the code I have.
P.S I am only posting th eportion of the assignment I don't understand, the main part that confuses my I underlined.

We are not allowed to change the main function or any of teh classes or member functions. I just don't understand when it says return type as class poly.


18. The member function subtract constructs a new Class Poly object that is the subtraction of this polynomial and one that is passed into the function. You should use the functions add and scale to implement this function.
19. The member function scale multiplies each coefficient in the polynomial by a scale factor passed into the function. The scale factor may be a positive or negative value. Note, if the scale factor is zero then scaling is not applied to the polynomial.
20. The member function eval evaluates the polynomial given an instantiation of the variable x. You must invoke the function eval from the Class Term in your solution.
]21.Besides the member functions for the Classes Term and Poly, you will write five functions that are invoked in the main function. Write the function prototype and then implement a function called display banner which displays the welcome banner. The function has no input parameters and has a return type of void.
22. Write the function prototype and then implement a function called read poly which prompts and reads a polynomial from the user. The function has one integer parameter to indicate which polynomial (1 or 2) the user will enter. The return type is the Class Poly. The user will enter the polynomial by typing the value pair, i.e. the coefficient and exponent, for each term of the polynomial in order from the highest exponent to the lowest exponent. For example, if the user entered the polynomial for the example shown above (3x^8 −2x^3 +7x^2 +10x−1), she could enter the following after the prompt (e.g. ”Enter poly #1:”):
Enter poly #1:
3 8 -2 3 7 2 10 1 -1 0 0 -1
In this example, the last value pair entered by the user is a terminating value pair, which is not added as a term to the polynomial but indicates that the user is done entering terms for the polynomial. There are three possible terminating value pairs:
(a) The exponent in the value pair is a negative number.
(b) The exponent in the value pair is greater than or equal to the smallest exponentvalue entered so far.
(c) The coefficent in the value pair is zero.
One exception to the third type of terminating value pair above is when the user
enters value pair (0, 0) as the first value pair in the input sequence. This ends userinput but the value pair is inserted into the polynomial’s representation. If any other terminating value pair is entered as the first value pair in the input sequence, then the input sequence ends and the polynomial will have zero terms. This function must not add a term with a coefficient equal to zero unless it is a polynomial representing
zero.
23. Write the function prototype and then implement a function called display stats which has two input parameters. The first input parameter is a Class Poly object and the second input parameter is a string containing a label to display before displaying the polynomial. Use pass-by-reference for both input parameters. The return type is void. This function displays the polynomial, the degree of the polynomial.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

class Term
{
private:
	int coeff;
	int exp;

public:
	void setCoeff(const int coefficient);
	void setExp(const int exponent);
	
	int getCoeff() const;
	int getExp() const;
	Term derivative() const;
	double eval(const double x) const;
	void displayFirst() const;
	void display() const;
};

class Poly
{
private:
	vector<Term> terms;	

public:
	// member functions
	void addTerm(const int coeff, const int exp);
	void scale(const int fact);

	Term getTerm(const int index) const;	
	int degree() const;						
	int termCount() const;					
	double eval(const double x) const;		
	Poly derivative() const;				
	Poly add(const Poly & poly) const;
	Poly subtract(const Poly & poly) const;
	void display(const string & label) const;
};

// FUNCTION PROTOTYPES GO HERE:

void display_banner();
Poly read_poly(int x);
void display_stats(Poly & x, string prompt);
void evaluate_poly(Poly x);
void goodbye_banner();

int main()
{
	Poly poly1, poly2, add_poly, sub_poly;
	int scale;
	
	display_banner();

       	poly1 = read_poly(1);
	cout << endl;
       	display_stats(poly1, "You entered polynomial #1");

	poly2 = read_poly(2);
	cout << endl;
       	display_stats(poly2, "You entered polynomial #2");

	add_poly = poly1.add(poly2);
       	display_stats(add_poly, "The addition of both polynomials is"); 

	sub_poly = poly1.subtract(poly2);
	display_stats(sub_poly, "The subtraction of both polynomials is"); 
		
	if (poly1.termCount() > 0) {
		cout << "Enter a scale factor to apply to polynomial #1: ";
		cin >> scale;
		poly1.scale(scale);
		display_stats(poly1, "The polynomial after scaling");
	}
	
	evaluate_poly(poly1);
	cout << endl;

       	goodbye_banner();
	
	return 0;
}
		 
// FUNCTION DEFINITIONS GO HERE:

void display_banner()
{
  cout <<"Welcom to fun with polynomials! " << endl;
  cout << "You will enter two polynomials." << endl;
  cout << "Please follow all instructions below." << endl;
}

Poly read_poly(int x)
{
  cout << "Enter poly #1:" << endl;

  return ();
}

void display_stats(Poly & x, string prompt)
{
  
}

void evaluate_poly(Poly x)
{
}

void goodbye_banner()
{
  cout << "Thanks for playing!" << endl;
}
    


// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:
void Term::setCoeff(const int coefficient) 
{
  int coe(0);
  cin >> coe;
  coeff = coe;
}

void Term::setExp(const int exponent) 
{
  int ex(0);
  cin >> ex; // user input of exponet
  exp = ex;
}

void Term::displayFirst() const
{
	// Replace with your code
}

void Term::display() const
{
	// Replace with your code
}

int Term::getCoeff() const 
{
  return (coeff); //returns coeff value
}

int Term::getExp() const 
{
  return (exp); // returns exp value
}

Term Term::derivative() const
{
	// Replace with your code
}

double Term::eval(const double x) const
{
	// Replace with your code
}

Term Poly::getTerm(const int index) const
{
	// Replace with your code
}

void Poly::addTerm(const int coeff, const int exp) 
{
	// Replace with your code
}

void Poly::scale(const int fact) 
{
	// Replace with your code
}

int Poly::degree() const 
{
	// Replace with your code
}

int Poly::termCount() const
{
	// Replace with your code
}

double Poly::eval(const double x) const
{
	// Replace with your code
}

Poly Poly::derivative() const
{
	// Replace with your code
}

Poly Poly::add(const Poly & poly) const
{
	// Replace with your code
}

Poly Poly::subtract(const Poly & poly) const
{
	// Replace with your code
}
	
void Poly::display(const string & label) const
{
	// Replace with your code
}
	
This is an example of a function with a return type of class Poly:
1
2
3
4
5
Poly myFunction()
{
    Poly blah;
    return blah;
}
You already have some functions with a return type of Poly.
Last edited on
ahh, I think I see... The already return types of poly are probably the ones from the template.

but I think you may have helped me. Instead of this (what I was messing with,
1
2
3
4
5
6
7
8
9
10
11
12
Poly read_poly(int x)
{
  int ex(0), coe(0),i(0); // exponet and coeffiecient.
  cout << "Enter poly #1:" << endl;
  cin >> coe >> ex;
  i = ex ; // sets limit.
  while ( ex > 0 && ex < i)
    {
      cin >> coe>> ex;
    }

}


it would be more like this...
1
2
3
4
5
6
7
Poly read_poly(int x)
{
  cout << "Enter poly #1:" << endl;
  Term setCoeff();
  Term setExp();
  return (Term getTerm());
}
You seem to be misunderstanding the difference between a function signature and a function call. In your second code snippet, lines 4, 5, and 6 have function signatures and I am pretty sure you meant for those to be function calls.

Try revisiting the tutorial on functions:
http://www.cplusplus.com/doc/tutorial/functions/
I see, Well I looked into the tutorial you linked, as well as looking into the tutorial about classes. It really helped and pointed me in the right direction so now I think I can handle the code!
Thanks again for the help!
Can you post your code for this?
Topic archived. No new replies allowed.