0

I am new to programming and currently am learning C++. One of the topic that i couldn't really understand that much is OOP. I had a tutor helping me but he's not really helpful so now I'm stuck on how to solve this. The question is as per image attached and this code is what I've worked on so far. C++ Program Requirements. I hope someone can help me on how to continue from here.

#include <iostream>
#include <iomanip>
#inlcude "term.h"
using namespace std;

class Term{
    private:
        int k   //represents current term number
        int a   //represents coefficient for the current term
        int x   //represents the value for x
        
    public:
        
};

int main()
{
 Term poly[10];
 int n,x;
 cout << "Please enter how many terms that you wish"
 << "to calculate a polynomial equation" << endl;
 cin >> n;

 cout << "Please enter the value for x for the polynomial equation" << endl;
 cin >> x;

 double sum = 0;
 for (int k = 0; k < n; k++)
 {
 int coeff;
 cout << "Please enter coefficient for term " << k << endl;
 cin >> coeff;
 poly[k].setCoeff(coeff); //setting the coefficient
 poly[k].setX(x); //setting the x
 poly[k].setK(k); //setting the k
 
 cout << "The result for term "
 << coeff << " * " << x << "^" << k << " is ";
 cout << poly[k].calcTerm() << endl;
 sum += poly[k].calcTerm();
 }
 cout << "The summation for all terms is " << sum << endl;
 return 0;
}
Ian Moone
  • 1
  • 1
  • 2
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to [edit] your questions, and how to make them *self-contained* (without needing external links) and to copy text *as text*. If the text is from a text-book, please transcribe it into the question, but if it's from a document on your system or a web-page, then copy-paste the text. – Some programmer dude Jul 20 '20 at 07:13
  • OOP is a modeling tool that is suitable for some things and not so for others. Imagine a game with a player avatar, some enemies, and some objects (standing around). In this case, it seems an intuitive way to implement these things in C++ objects, with member variables representing their state (like e.g. current position and health), and member functions to modify these states. In other cases, the introduction of objects doesn't contribute like e.g. a parser which analyses text of a script language by executing match functions in a recursive way (a recursive descent parser). – Scheff's Cat Jul 20 '20 at 07:39
  • C++ offers both ways of modeling and it's on you to chose the proper tool for the appropriate task. In your specific case: When you consider the individual terms as objects with states (the member variables you already sketched) and member functions to create these objects and modify their states, then OOP is the appropriate tool for you. (The rest of the story can be found in every good C++ book.) – Scheff's Cat Jul 20 '20 at 07:44
  • 1
    And finally a personal observation: Once I got infected by OOP, I started to see everything as objects with states, interacting with each other. It caused me additional effort to learn that there are still alternatives... ;-) – Scheff's Cat Jul 20 '20 at 07:44
  • also, [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – yaodav Jul 20 '20 at 07:51

0 Answers0