Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: What's wrong with this code?

  1. #1
    Join Date
    Jul 2002
    Posts
    1,388
    Rep Power
    0

    Question What's wrong with this code?

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class Date
    {
        int day;
        int month;
        int year;
    
      public:
    
        Date (int Day=1, int Month=1, int Year=1970)
        {
          day = Day;
          month = Month;
          year = Year;
        }
    
      int getDay() {return day;}
      int getMonth() {return month;}
      int getYear() {return year;}
    };
    
    /*InventoryItem containing Name, Cost, Quantity, DatePurchased(Object of the date Class), InventoryTotal(Static Variable)*/
    
    class InventoryItem
    {
    
      string   Name;
      double Cost;
      int       Quantity;
      Date    DatePurchased;
      static int InventoryTotal;
      
      public:
        InventoryItem (string strName, double dCost, int nQuantity, Date cDatePurchased(int, int, int), int s_nInventoryTotal)
        {
          Name               = strName;
          Cost                 = dCost;
          Quantity           = nQuantity;
          DatePurchased = cDatePurchased(10,10,2010);
          InventoryTotal   = s_nInventoryTotal;
        }
        string getName() {return Name;}
        double getCost() {return Cost;}
        int getQuantity() {return Quantity;}
        Date getDatePurchased() {return DatePurchased();}
        int getInventoryTotal() {return InventoryTotal;}
      };
      
      int main ()
      {
        //InventoryItem.cDatePurchased(10,10,2010);
        cout << InventoryItem.getDatePurchased();
      }
    http://ideone.com/7n14w
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  2. #2
    Join Date
    Apr 2003
    Posts
    13,129
    Rep Power
    28

    Default

    What problems are you having with it?

    Or is this a coding quiz for the programmers out there?
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2009

  3. #3
    Join Date
    Jul 2002
    Posts
    1,388
    Rep Power
    0

    Default

    I tried to make sure everything I did was "according to the book". I don't know how to work with the DatePurchased constructor though. I don't understand the error about it not having values. Am about break it into small pieces and reassemble. Just got home and had to share the lappy. Time to work now
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  4. #4
    Join Date
    Apr 2003
    Posts
    13,129
    Rep Power
    28

    Default

    Quote Originally Posted by carey View Post
    I tried to make sure everything I did was "according to the book". I don't know how to work with the DatePurchased constructor though. I don't understand the error about it not having values. Am about break it into small pieces and reassemble. Just got home and had to share the lappy. Time to work now
    I believe you will need to create an instance of the class, before you can use it, the way you have in the main().

    Code:
    InventoryItem myinvent;
    cout << myinvent.getDatePurchased();
    Also, you have no default constructor for the InventoryItem class. As you probably know, you can assign default values to the attributes in the default constructor.

    Since you don't have any default constructor, your attributes aren't assigned any values.
    You have a primary constructor, which accepts data you provide to it, and then assigns them to your attributes.

    You will either have to create a default constructor which sets default values to the attributes (and then your code will work), or pass values to the primary constructor when you first create the object.

    Default constructor example:
    Code:
    ...
    InventoryItem ()
        {
          Name               = "Arch_angel";
          Cost                 = 0.0;
          Quantity           = 0;
          InventoryTotal   = 0;
        }
    ...
    And in main:
    Code:
    InventoryItem myinvent;
    cout << myinvent.getDatePurchased();
    Or passing data to the primary constructor example:
    Code:
    InventoryItem myinvent(value, value, value, value, etc.);
    cout << myinvent.getDatePurchased();
    Keep this site for reference as well: http://www.cplusplus.com/doc/tutorial/classes/
    Was a big help for me when I did C++.
    Last edited by Arch_Angel; Oct 5, 2012 at 02:00 AM.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2009

  5. #5
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    you should have the compiler on ideone set to c++ and not C++0x.before you use a variable it needs to have a value else your program will run into a lot of problems. on line 43 you call the constructor too late. you should have called it on line 28 when you were declaring the other variables. if you dont know when to use a constructor, you probably dont see the benefit of using one. if you wanted a date then you could have defined an accessor in the Date class, then you can instantiate a Date object in the getDatePurchased() function that you can return after assigning the value returned from the accessor to a local variable in the getDatePurchased() function .
    Last edited by wiel; Oct 5, 2012 at 10:45 AM.
    1337

  6. #6
    Join Date
    Jul 2002
    Posts
    1,388
    Rep Power
    0

    Default

    Quote Originally Posted by wiel View Post
    you should have the compiler on ideone set to c++ and not C++0x.before you use a variable it needs to have a value else your program will run into a lot of problems. on line 43 you call the constructor too late. you should have called it on line 28 when you were declaring the other variables. if you dont know when to use a constructor, you probably dont see the benefit of using one. if you wanted a date then you could have defined an accessor in the Date class, then you can instantiate a Date object in the getDatePurchased() function that you can return after assigning the value returned from the accessor to a local variable in the getDatePurchased() function .
    Thanks. Re constructor, I was following advice from Bucky on thenewboston Youtube channel and some writings from http://www.learncpp.com/ that a good habit is to make a constructor in each class. Even C# generates it automatically now.

    Just got in off the road. Road hot tonight. Marrow flying in Ochi. Well time to work.

    Using C++0x because most of the recent changes. I was guessing support and stability would be higher?
    Last edited by carey; Oct 5, 2012 at 10:29 PM.
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  7. #7
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    how many compilers have full support for C++0x? you cant make a guess like that for c++ because it has been around longer so it would be a case where it puts out what you put in
    1337

  8. #8
    Join Date
    Jul 2002
    Posts
    1,388
    Rep Power
    0

    Default

    Quote Originally Posted by wiel View Post
    how many compilers have full support for C++0x? you cant make a guess like that for c++ because it has been around longer so it would be a case where it puts out what you put in
    Are you saying that, unlike other language compilers (except python), each upgraded "standard" of C++ results in incompatibility with old code? Wiel, if anything, it should support all the old stuff, plus more: it should not be breaking or discarding anything old? It is still C++, right?
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  9. #9
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    wrong, C++ is an iteration of C but not all C code works in C++. same goes for C++0x(C++11). some of the code will work, some of it will fail because functions work differently and some will give you undefined behavior which is hard to fix with your compiler

    http://en.wikipedia.org/wiki/Compati..._C_and_C%2B%2B
    1337

  10. #10
    Join Date
    Jul 2002
    Posts
    1,388
    Rep Power
    0

    Default

    Quote Originally Posted by wiel View Post
    wrong, C++ is an iteration of C but not all C code works in C++. same goes for C++0x(C++11). some of the code will work, some of it will fail because functions work differently and some will give you undefined behavior which is hard to fix with your compiler

    http://en.wikipedia.org/wiki/Compati..._C_and_C%2B%2B
    Wiel, the logic is wrong. I read through some of the standard. Nothing has been removed: lots have been added. The standard namespace has not been changed incompatibly. C++11 is actually more likely to run code than present, prev-gen C++.
    Last edited by carey; Oct 7, 2012 at 12:59 PM.
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •