Hi, first of all i'm sorry I didn't include details about the problem i'm having in the title but it feels hard to explain without a wall of text & code for reference, plus the question I have to do for context.

Question (inb4 dwight schrute)
A class called 'sample' with the data a, b and c and two methods as defined below. Define a new member method 'term()' which calculates the value of the term (2b - 4ac) and displays it.

Define the method outside of the class definition. Write the main method to create an array of 2 objects called D of class 'sample' and display the values and find the solution to the term (2b - 4ac)

Code

#include <iostream>

using namespace std;

class sample {
    int a, b, c;

    public:
        void setdata(void)
        {
            cout << "Enter values of a, b and c: ";
            cin >> a >> b >> c;
            cout << "\n";
        };

        void display(void)
        {
            cout << "\nData a ,b ,c : " << endl;
            cout << a << b << c << endl;
        };


        int term();

};
int sample::term();
int term(int a, int b, int c)
        {
            int result = (2*b - 4*a*c);
            return result;
        };

int main()
{
    sample d[2];
    d.setdata();
    d.display();
    d.term();
}

You can probably spot a few very obvious errors straight off the bat, and as for what the problem is right now i'm not sure, all I know is what the error messages tell me. Here are the errors I am getting.

Errors

*Line 27 Error: declaration of 'int sample::term()' outside of class is not definition

Line 38 Error: request for member 'setdata' in 'd', which is of non-class type 'sample[2]'

Line 39 Error: request for member 'term' in 'd', which is of non-class type 'sample[2]'*

That's all, thanks for reading if you did. I know it is a bit of a TL;DR.

Hey there, nice attempt.

When using classes, you should remember that your class definition should be in a (.h) file, and then class methods/functions should be in it's own seperate (.cpp) file. Let me show you an example:

Sample.h:

class Sample {

    public:
        Sample();
        int term(int a, int b, int c);
        void setData(int a, int b, int c);
        int returnA1();

     private:
        int a1, b1, c1;
};

Sample.cpp:

Sample::Sample(){}
void Sample::setData(int a, int b, int c)
{
    this->a1 = a;
    this->b1 = b;
    this->c1 = c;

}
int Sample::returnA1()
{
    return this->a1;
}

int Sample::term(int a, int b, int c)
{
    int result = (2*b - 4*a*c);
    return result;
}

A few errors in your main:

1) You defined an array of class objects, so you need to define which object you're wishing to use. E.g.

Before:

d.setdata();

How it should be done:

d[0].setdata();
// or
d[1].setdata();

Remember you've initalised an array of 2, which starts at 0.

Also IMO classes should not specifically ask for input, or, use commands like "cout", this is because you may want to use the class in the future for other applications other than console based.

I have included a working version of your script, but, remember the points above!!

#include <iostream>

using namespace std;

class Sample {

    public:
        Sample();
        int term(int a, int b, int c);
        void setData(int a, int b, int c);
        int returnA1();


        int a1, b1, c1;
};

Sample::Sample(){}

void Sample::setData(int a, int b, int c)
{
    this->a1 = a;
    this->b1 = b;
    this->c1 = c;

}

int Sample::returnA1()
{
    return this->a1;
}

int Sample::returnA2()
{
    retrun this->a2;
}

int Sample::term(int a, int b, int c)
{
    int result = (2*b - 4*a*c);
    return result;
}



int main()
{
    Sample d[2];

    int foo = 10, foo1 = 20, foo2 = 40;

    d[0].setData(foo, foo1, foo2);

    cout << d[0].returnA1();
    cout << d[0].term(10, 20, 20);
}

Hope this helps you :)

Line 26 is not correct. When you define a function outside the class boady you would do it like this.

returnType ClassName::FunctionName(paramaters)
{
    // function boady here
}

So in your case it would look like this.

int sample::term()
{
    return ((2 * b) - (4 * a * c));
}

I left out the creation of the local variable because in this case it is not needed. You can simply return the calculation directly.

Thank you very much for the detailed reply phorce, and you NathanOliver. I appreciate you both taking the time to help a newb out.

I believe I have it working now, can either of you see if i'm missing something or not properly addressing some aspect of the question?

Here is the updated code:

#include <iostream>

using namespace std;

class sample {
    int a, b, c;

    public:
        int term();
        void setdata()
        {
            cout << "Enter values of a, b and c: ";
            cin >> a >> b >> c;
            cout << "\n";
        };

        void display(void)
        {
            cout << "\nData a ,b ,c : " << endl;
            cout << a << b << c << endl;
        };
};

int sample::term()
{
  return ((2*b) - (4*a*c));
}

int main()
{
    sample p[2];
    p[0].setdata();

    cout << "The answer is: " << p[0].term();
}

It gives this output (NB I have entered in all 3 values)

[IMG]http://i.imgur.com/77SNe.png[/IMG]

Ok, so the question is this:

Question (inb4 dwight schrute)
A class called 'sample' with the data a, b and c and two methods as defined below. Define a new member method 'term()' which calculates the value of the term (2b - 4ac) and displays it.

Define the method outside of the class definition. Write the main method to create an array of 2 objects called D of class 'sample' and display the values and find the solution to the term (2b - 4ac)

  • You have a class called 'sample'
  • You have data (a, b, c)
  • Two methods inside the class
  • Method "term()" I believe HAS to be inside the class (But I might be wrong), the task says DEFINE, nothing about implementing it outside of the class, but, again, I might be wrong.
  • You also should think about making each of the methods, actual methods. At the moment, they seem to be just functions.. Not properties of the class. To do this, do, void sample::setdata()

I don't understand why they have asked you to do this, since, it's the wrong way of using classes. You SHOULD define the protection in the class, public, private, protected etc..

Also, like I said before.. Avoid using "cout" and "cin" inside your class, this should be handled in your MAIN. Accept parameters that are inputted through main, return values in your methods.

Are you sure your reading the question right? It says "define the method outside of the class definition" this may mean that they're asking you to have a class definition (.h) and then the methods (.cpp) rather than just the single method outside of the class!

Overall, looks like a good attempt and IMO you have answered all the points asked of you.

Now to hijack my own thread, if anyone can quickly glance over this and point out anything obvious that i'm doing wrong i'd appreciate it. It's another question from the coursework for which I wrote the code yesterday.

I don't really know what the program should do which makes it harder to write code for it. What should 'getProduct' method do?

Here's the question:

Let us consider an example of an inventory control of products in a store. One way of recording the details of the products is to record their names, code numbers, total items in stock and the price of each item.

Data
product _name
code
price
Methods
GetProducts()

Define a class called 'product' with data items product_name, code, price and the method GetProducts()

My code:

#include <iostream>

using namespace std;

class product
{
    string product_name;
    int code;
    int price;
    int stock;
        public:
        int GetProduct();

        void setdata()
        {
            cout << "Enter product name: \n";
            cin >> product_name;
            cout << "Enter product code: \n";
            cin >> code;
            cout << "Enter price of product: \n";
            cin >> price;
            cout << "How many in stock? \n";
            cin >> stock;
        };

           /* cout << "Enter product name ";
            cin >> product_name;

            cout << "Enter product code ";
            cin >> code;*/
        void display(void)
        {
            cout << "\nProduct name: " << product_name;
            cout << "\nProduct code: " << code;
            cout << "\nPrice: " << price;
        };



int main()
{
    product p; //object of type product
    p.setdata();
    p.display();
}

As you can see it's all over the place but i'm just kind of working at it and hoping that it will make sense as I go along.

Thanks again for the help.

Hello,

To answer your question regarding what "int GetProduct();" should do. Well, it could do a lot of things things, here's some suggestion:

  • Let's assume you're entering the data, and you have a vast amount of products and
    you need to locate a particular product, each product therefore has it's own ID number which can be located inside the array. Therefore, "int getProduct(int theIdNumber);" which would then search through the array for the particular ID and then once found, display that particular ID. (I did something similar for a games store as an assignment).

  • The other assumtion is that your assessor or lecturer wants you to move into "sets" and "gets" approach to classes. You have three variables, code, price, stock and product number. Each of these class variables has to be set and each of these variables have to be returned. Ok, here's an example (Not compiled):

      void setCode(int theCode)
      {
    
      }
    
      void setPrice(float thePrice)
      {
    
      }
    
      void setStock(int theStock)
      {
    
      }
    
      void setProductName(string theName)
      {
    
      }
    

These are your SET'S. They set the variables from main.

Let's look at GET'S:

    int returnCode()
    {
       // return the class variable name for code
    }

    float returnPrice()
    {

    }

    int returnStock()
    {

    }

    string returnName()
    {

    }

Hope this helps you :)

Thanks for your ongoing support phorce.

I have taken this to mean they want me to populate it maybe?

Anyway here's my code & a screenshot of it 'working'

#include <iostream>

using namespace std;

class product
{

    string product_name;
    int code;
    float price;
    int stock;
        public:
        void getProduct(void);
        void display(void);
};

    void product::getProduct(void)
    {
        cout << "Enter product name: \n";
        cin >> product_name;
        cout << "Enter product code: \n";
        cin >> code;
        cout << "Enter price of product: \n";
        cin >> price;
        cout << "How many in stock? \n";
        cin >> stock;
    }

    void product::display(void)
    {
        cout << "\nProduct name: " << product_name;
        cout << "\nProduct code: " << code;
        cout << "\nPrice: " << price;
    };



int main()
{
    product p[10]; //object of type product
    for (int i=0; i < 1; i++)
    {
        p[i].getProduct();
    }
    cout << "\n";

    for(int i=0; i< 1; i++)
    {
        p[i].display();
    }

}

[IMG]http://i.imgur.com/0kseL.png[/IMG]

bump

commented: Do NOT bump threads. It's rude, and sometimes the one helping you might be asleep. -3

Ok, nearly right.. I think..

I'm going to assume, from what you've told me that the program will work as follows:

  1. The user is asked the input the amount of products they wish to insert
  2. An array of Objects is defined with the length of how many products has to be inserted.
  3. The products are then inputted into the correct object (array), e.g. Product 1 will be p[0], product 2, p[1]
  4. The user can then input which product he/she would like to search for, using the product number. in this case, they would enter the location of the object in the array. (For product 1, enter 0 etc).. Each product will therefore have a unique number, as it will be stored in the array.

Take a look at this example, I cannot complete it for you, as I get bad rep for that (hehe) but it should give you some indication to how to move forward in solving this problem.

#include <iostream>

using namespace std;

class product
{

    string product_name;
    int code;
    float price;
    int stock;
        public:
        void getProduct(void);
        void display(void);
};

    void product::getProduct(void)
    {
        cout << "Enter product name: \n";
        cin >> product_name;
        cout << "Enter product code: \n";
        cin >> code;
        cout << "Enter price of product: \n";
        cin >> price;
        cout << "How many in stock? \n";
        cin >> stock;
    }

    void product::display(void)
    {
        cout << "\nProduct name: " << product_name;
        cout << "\nProduct code: " << code;
        cout << "\nPrice: " << price;
    };



int main()
{
    int number_of_products;
    int product_number;

    cout << "Please enter the amount of Products you wish to insert: ";
    cin >> number_of_products;

    // Initalise the amount of Objects
    product *p = new product[number_of_products];

    for(int i=0; (i < number_of_products); i++)
    {
        // for each product, enter the values.
        // So let's say, product[0] (product[i])
        // enter the product name, 
        // enter the product cost,
        // enter the product number (which can be set to "i"
        // enter the product price etc..
    }

    cout << "Please enter the product in which you want to search for: ";
    cin >> product_number;

    // search for the product.  
}

Please try not to bump the thread, we all have other things to do than help you ;)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.