Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Attention C++ coders, simple problem need help.

Options
  • 26-06-2008 11:29am
    #1
    Closed Accounts Posts: 496 ✭✭


    OK this is very basic coding but the thing is I have a coursework repeat for uni in object orenitated programming, I'm not askin you ppl to do it for me just that there is a lot of pressure on me to do this, as it will jepordise a placement I have in America if i do not have this passed by the 30th of June. What I'm asking is if people could have a look over my answers and let me know if i'm doing it right due to the that fact it must be Passed.
    If anyone is willing to help I'd be over the moon,


    Also the is VERY EASY CODE,

    Joe


Comments

  • Closed Accounts Posts: 496 ✭✭j0e


    heres the first 5 questions out of 5 and my attempt at them
    Let us consider an example of an inventory of products in a store. One way of recording the details of the products is to record their names, code number, total items in the stock and the price of each item.

    product_name
    product_code
    Unit_Price

    (i) Define a class called ‘product’ with data items product_name, product_code, and Unit_price. Save the class as a header file, i.e. **.h.
    (ii) Define a constructor with parameter for dynamic initialization.
    (iii) Define a method for displaying prices of products.
    (iv) Write a main program to test the product system. Use the header file for the class defined in (i).

    My attempt
    //i)

    class product
    {
    char product_name[30];
    int product_name, unit_price;
    public:
    //ii)
    product() {}// very unsure about this constructor with parameter for dynamic initialization
    product (int code, int price, char name)
    {
    product_name = name;
    product_code = code;
    unit_price = price;
    }
    //or should it be
    void getdata(void)
    {
    cout << "Enter name: ";
    cin >> name;
    cout << "Enter account no: ";
    cin >> account;
    cout << "Enter balance amount: ";
    cin >> balance;
    }

    //iii)
    void display(void)
    {
    cout << "\nName: " << product_name;
    cout << "\nCode: " << product_code;
    cout << "\nPrice: " << unit_price;
    }

    }// then save as .h


    //iv)

    #include "product.h"

    main()
    {
    product p;
    p.getdata();
    p.display();
    getche();
    }

    //or

    #include "product.h"

    main()
    {
    product p[3]; //array of object of type product
    for(int i=0;i<3;i++)
    {
    cout << "\nGet details of product " << (i+1) << ": \n";
    p.getdata();
    }
    cout<<"\n";
    for(int i=0;i<3;i++)
    {
    cout << "\nDetails of product " << (i+1) << ": \n";
    p.display();
    }
    cout<<"\n";
    getche();
    }



  • Closed Accounts Posts: 25,848 ✭✭✭✭Zombrex


    My C++ is a bit rusty, but sure post your code up ... I may not tell you exactly what to do, but I will certain try and explain where you are going wrong.


  • Closed Accounts Posts: 10,808 ✭✭✭✭chin_grin


    Um, could we have details or a snippet of code or where you think you're going wrong? I have a little C++ myself. Not much, but a bit more information would be helpful. Cheers.

    Edit: Oops, forget that I was too quick off the mark. Sorry.


  • Closed Accounts Posts: 25,848 ✭✭✭✭Zombrex


    A few quick comments, just first things that spring to mind

    1)


    Firstly off, you appear to be programming in a Java style where the definition of the classes contain their functionality as well. This is traditionally done a bit differently in C++

    For each class you would have two files, a header file and a code file.

    For example, say you have a class called "Product." You would have two files,

    product.h
    product.cpp


    In product.h you have the definition of the class

    product.h
    class Product
    {
    private:
        int m_code;
        int m_id;
    
    public:
        Product ();
        Product (int, int);
        void setData (int, int);
    }
    

    This defines all elements of the class, including the variables and the methods. But it doesn't contain any actual implementation of the methods. This is done in the second file, the product.cpp file

    product.cpp
    #include "product.h"
    
    void Product::Product ()
    {
    
    }
    
    void Product::Product (int initialId, int initialCode)
    {
        m_id = initialId;
        m_code = initialCode;
    }
    
    void Product::setData (int, int)
    {
        m_id = newId;
        m_code = newCode;
    }
    

    The purpose of this was that other areas of the program that use the class only have to have the header file to find out how to communicate with each object through its public methods.

    In a commercial environment that allowed programmers to ship their actual implementation (the product.cpp) as an already compiled object file, so people using their objects as libraries for their own programs couldn't see the code of how the object actually does what it does.

    Languages like Java, PHP and C# have done away with this convention, with them the class declaration contains everything. I don't know if you can technically do that with C++ (think you can with the inline command), but the way I described is the traditional way of structuring C++ files.

    BTW the "m_" in front of each private variables is just a naming convention I like, it isn't a command or anything. It just stops things getting confusing when you are thinking about if the variable is a member variable or just a variable you have declared in a method. It means you can do thinks like this
    void Customer::setCustomerId (customerId)
    {
        m_customerId = customerId;
    }
    

    Where as if you called your private variable just customerId, you couldn't, you would have to call the variable you are passing something else, like this
    void Customer::setCustomerId (newCustomerId)
    {
        customerId = newCustomerId;
    }
    



    2)


    It is generally not good practice to design objects like you have done with your "getdata" method. The problem with this method is that it encapsulates a lot of outside business logic, like the interface of your program, within the object itself.

    In the object yourself you are presenting a user interface and getting input from the command line with the cout and cin function.

    A better way of doing it would be to pass a variable to your object using a specific "set" method, and handle the input from the command line some where else in your code, such as in your "main()" function.

    For example
    /* --- product.h --- */
    class ProductClass
    {
    private:
        int m_id
        int m_code
    
    public:
        void setData (int, int);
    }
    
    /* --- product.cpp --- */
    void Product::setData (int newId, int newCode)
    {
            m_id = newId;
            m_code = newCode;
    }
    
    /* --- main.cpp --- */
    
    #include "product.h"
    
    int main ()
    {
        MyClass currentProduct;
    
        int productId;
    
        cout << "Enter a new product ID"
        cin >> productId;
    
        currentProduct.setData(productId);
    }
    

    The reason for this is that you want to seperate your product objects from the rest of the elements in your system, such as the user interface. It might be fine now, but imagine your product object also in the future needs to be populated from say a database, rather than the user interface.

    If you wanted the data to come from a database you obviously don't want to be printing out the user interface every time.

    3)

    It is a good idea to have an individual "set" and "get" method for each private member variable. For example
    /* --- product.h--- */
    
    class ProductClass
    {
    private:
        int m_id;
        int m_code;
    
    public:
    
        int setProductId ();
        int setProductCode ();
        void setProductId (int);
        void setProductCode (int);
    }
    
    /* --- product.cpp --- */
    
    #include "product.h"
    
    int Product::getProductId ()
    {
            return (m_id);
    }
    
    int Product::getProductCode ()
    {
            return (m_code);
    }
    
    void Product::setProductId (int newId)
    {
            m_id = newId;
    }
    
    void Product::setProductCode (int newId)
    {
            m_code = newId;
    }
    

    The reason for this is that it gives you more flexibility. Say you only want the product code, not the whole set of data. Or you want to change just the product id.


    Hope this helps. Have a read over this, see if you understand it all, and re-write the code. Feel free to post it up for us to have a look at.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Wicknight wrote: »
    Languages like Java, PHP and C# have done away with this convention, with them the class declaration contains everything. I don't know if you can technically do that with C++ (think you can with the inline command), but the way I described is the traditional way of structuring C++ files.
    Actually you can write code for a class exactly like as in Java in C++, however most modern compilers will inline every function implementation you define, even without the inline keyword. So no-one bothers, plus the convention of having your class defined simply in a header file, as a well defined interface when shipping libs is the main reason not to break this convention.


  • Advertisement
  • Registered Users Posts: 7,518 ✭✭✭matrim


    One other thing is that unit_price should probably be a double.

    You also have an error here
    char product_name[30];
    int product_name, unit_price;


Advertisement