Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: What's wrong with this code?

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

    Default

    Pay no attention to main yet. I am working on the class errors first.
    Code:
    #include<iostream>
    #include<string>
    //#include<cassert>
    using namespace std;
    
    /*1a.*/
    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;}
    };
    
    /*1b. InventoryItem containing Name, Cost, Quantity, DatePurchased(Object of the date Class), InventoryTotal(Static Variable)*/
    
    class InventoryItem
    {
      string Name;
      double Cost;
      int Quantity;
      Date DatePurchased;              /*Object of the date class*/
      static int InventoryTotal(100);
       
     public:
     /* InventoryItem (string strName, double dCost, int nQuantity, Date cDatePurchased(int, int, int), int s_nInventoryTotal)
        {
          Name = strName;
          Cost = dCost;
          Quantity = nQuantity;
          DatePurchased = cDatePurchased;
          InventoryTotal = s_nInventoryTotal;
        } */
      InventoryItem() : Name("Nobody"), Cost(0.0), Quantity(0), DatePurchased(1,1,1970) { }
     
        string getName() {return Name;}
        double getCost() {return Cost;}
        int getQuantity() {return Quantity;}
        string getDatePurchased() {return DatePurchased;}
        int getInventoryTotal() {return InventoryTotal;}
        
         friend void changeQuantity(int) { cin>> int Quantity ;}
      };
      
      int main ()
      {
        InventoryItem testit;
        cout<<testit.InventoryTotal;
        
      }
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  2. #12
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    http://stackoverflow.com/questions/2...within-a-class
    you might have this problem when you declare DatePurchased there. Ive never done that before so i wondered what would happen. the way you are doing it seems harder

    C++11 is actually more likely to run code than present, prev-gen C++.
    but c++11 is not 100% backward compatible with C++ code. on the c++11 wiki page you'll see the developers tried to make it compatible with c++98.when you click on the c++98 hyperlink you'll see different versions of c++.the version being taught in schools is probably older than c++11 but more recent than c++98 and you would have a problem if you learnt the version in the middle and tried to apply it to c++11
    http://stackoverflow.com/questions/6...roduced-in-c11
    1337

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

    Default

    Quote Originally Posted by wiel View Post
    http://stackoverflow.com/questions/2...within-a-class
    you might have this problem when you declare DatePurchased there. Ive never done that before so i wondered what would happen. the way you are doing it seems harder


    but c++11 is not 100% backward compatible with C++ code. on the c++11 wiki page you'll see the developers tried to make it compatible with c++98.when you click on the c++98 hyperlink you'll see different versions of c++.the version being taught in schools is probably older than c++11 but more recent than c++98 and you would have a problem if you learnt the version in the middle and tried to apply it to c++11
    http://stackoverflow.com/questions/6...roduced-in-c11
    I changed it to initialization lists. Posting it soon. Getting trying to clear up some errors. So far I have what? 6errors, 1 warning?
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

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

    Default

    I am here:
    Code:
    #include<iostream>
    #include<string>
    //#include<cassert>
    using namespace std;
    
    /*2a.*/
    class Date
    {
        int day;
        int month;
        int year;
      public:
        Date (int, int, int) : day(1), month(1), year(1970) {}
      int getDay() {return day;}
      int getMonth() {return month;}
      int getYear() {return year;}
    };
    
    /*2b. InventoryItem containing Name, Cost, Quantity, DatePurchased(Object of the date Class), InventoryTotal(Static Variable)*/
    
    class InventoryItem
    {
      string Name;
      double Cost;
      int Quantity;
      Date DatePurchased;              /*Object of the date class*/
      static int InventoryTotal = 100;
       
     public:
     
      InventoryItem() : Name("Nobody"), Cost(0.0), Quantity(0), DatePurchased(1,1,1970) {}
     
        string getName() {return Name;}
        double getCost() {return Cost;}
        int getQuantity() {return Quantity;}
        getDatePurchased() { }
        int getInventoryTotal() {return InventoryTotal;}
        
        /*2.c friend function to change quantity of Inventory item*/
        
         friend void changeQuantity(int) { cin>> Quantity ;}
      };
      
      int main ()
      {
        /*3. main function that instantiates 10 InventoryItem objects using arrays; should use friend function to change
        the quantity of 4 InventoryItem objects*/
        
        InventoryItem testit;
        cout << testit.getCost;
        
      }
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

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

    Default

    "Almost there!"
    Code:
    #include<iostream>
    #include<string>
    #include "banned.h"
    //#include<cassert>
    using namespace std;
    
    /*2a.*/
    class Date
    {
        int day;
        int month;
        int year;
      public:
        Date (int, int, int) : day(1), month(1), year(1970) {}
      int getDay() {return day;}
      int getMonth() {return month;}
      int getYear() {return year;}
    };
    
    /*2b. InventoryItem containing Name, Cost, Quantity, DatePurchased(Object of the date Class), InventoryTotal(Static Variable)*/
    
    class InventoryItem
    {
      string Name;
      double Cost;
      int Quantity;
      Date DatePurchased;              /*Object of the date class*/
      static int InventoryTotal;
       
     public:
     
      InventoryItem() : Name(" "), Cost(0.0), Quantity(0), DatePurchased(1,1,1970) {}
     
        void setInventoryTotal(int amt) { InventoryTotal = amt; }
        string getName() {return Name;}
        double getCost() {return Cost;}
        int getQuantity() {return Quantity;}
       // Date getDatePurchased() { }
        int getInventoryTotal() {return InventoryItem::InventoryTotal;}
        
        /*2.c friend function to change quantity of Inventory item*/
        
         friend void changeQuantity(int);
         void changeQuantity(int quan) { Quantity = quan; }
      };
      
      int main ()
      {
        /*3. main function that instantiates 10 InventoryItem objects using arrays; should use friend function to change
        the quantity of 4 InventoryItem objects*/
        
        InventoryItem testit;
       /* changing quantity works 
       testit.changeQuantity(10.00);
       cout << testit.getQuantity();
        */
        
        cout << testit.DatePurchased.day ; /* this doesn't */
        
        /* 10 iterations loop */
        
        
        
        
    
        
      }
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  6. #16
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    DatePurchased and day are private
    http://ideone.com/HJjX1
    Last edited by wiel; Oct 8, 2012 at 04:41 PM.
    1337

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

    Default

    Quote Originally Posted by wiel View Post
    DatePurchased and day are private
    http://ideone.com/HJjX1
    Those 3 Ps -- private, protected and public -- are a convoluted mess! Maybe because I don't understand C++ operators yet...

    But even then, the goal is to utilize composition, friendship and abstraction. So, how to deal with private members without making them public?
    Last edited by carey; Oct 9, 2012 at 12:28 AM.
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  8. #18
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Quote Originally Posted by carey View Post
    Those 3 Ps -- private, protected and public -- are a convoluted mess! Maybe because I don't understand C++ operators yet...

    But even then, the goal is to utilize composition, friendship and abstraction. So, how to deal with private members without making them public?
    The friend function should be outside the inventory item class.

    example:

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <cstdio>
    
    using namespace std;
    
    class Date
    {
    private:
        int day;
        int month;
        int year;
    
    public:
        Date() {}
        Date(int day, int month, int year) {
            this->day = day;
            this->month = month;
            this->year = year;
        }
        int getDay() { return this->day; }
        int getMonth() { return this->month; }
        int getYear() { return this->year; }
        void setDay(int day) { this->day = day; }
        void setMonth(int month) { this->month = month; }
        void setYear(int year) { this->year = year; }
        char* get()
        {
            char* buffer;
            buffer = (char*)malloc(sizeof(char)*(10+1));
            sprintf(buffer, "%i/%i/%i", this->day, this->month, this->year);
            return buffer;
        }
    };
    
    class InventoryItem
    {
    private:
        string name;
        double cost;
        int quantity;
        Date datePurchased;
    
    public:
        InventoryItem() {}
        InventoryItem(string name, double cost, int quantity, Date datePurchased)
        {
            this->name = name;
            this->cost = cost;
            this->quantity = quantity;
            this->datePurchased = datePurchased;
        }
        string getName() { return this->name; }
        double getCost() { return this->cost; }
        int getQuantity() { return this->quantity; }
        Date getDatePurchased() { return this->datePurchased; }
    
        void getName(string name) { this->name = name; }
        void getCost(double cost) { this->cost = cost; }
        void getQuantity(int quantity) { this->quantity = quantity; }
        void getDatePurchased(Date datePurchased) { this->datePurchased = datePurchased; }
        friend void changeQuantity(InventoryItem&, int);
        void display()
        {
            cout << "Name: " << this->name << endl;
            cout << "Cost: " << this->cost << endl;
            cout << "Quantity: " << this->quantity << endl;
            cout << "Date Purchased: " << this->datePurchased.get() << endl;
        }
    };
    
    void changeQuantity(InventoryItem& inventoryItem,int quantity)
    {
        //inventoryItem.setQuantity(quantity);
        inventoryItem.quantity = quantity;
    }
    
    int main()
    {
        InventoryItem inventoryItem[10];
        int month;
        int day;
        int year;
    
        for(int i = 0; i < 10; i++)
        {
            month = rand()%13+1;
            year  = rand()%13+2000;
    
            switch(month)
            {
                case 2:
                    if((year%4)==0)
                        day = rand()%29+1;
                    else
                        day = rand()%28+1;
                break;
                case 4:
                case 6:
                case 9:
                case 11:
                    day = rand()%30+1;
                break;
                default:
                    day = rand()%31+1;
                break;
            }
    
            inventoryItem[i] = InventoryItem("Item", (double)(rand()%1000+100), 10, Date(day,month,year));
            changeQuantity(inventoryItem[i], rand()%100+1);
            inventoryItem[i].display();
            cout << endl;
        }
        return 0;
    }
    Last edited by jayrulez; Oct 9, 2012 at 06:13 AM.

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

    Default

    Quote Originally Posted by carey View Post
    Those 3 Ps -- private, protected and public -- are a convoluted mess! Maybe because I don't understand C++ operators yet...

    But even then, the goal is to utilize composition, friendship and abstraction. So, how to deal with private members without making them public?
    cplusplus.com has examples for all those three.you can use the accessors to return private members since they are public functions.
    Code:
      cout << testit.getDatePurchased().getDay();
    1337

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

    Default

    Quote Originally Posted by wiel View Post
    cplusplus.com has examples for all those three.you can use the accessors to return private members since they are public functions.
    Code:
      cout << testit.getDatePurchased().getDay();
    Thanks for all these ppl!. I am analysing wiel and jayrulez stuff now. Was not able to for a while. Some illness has placed a sleeping curse on me. Internal bleeding ... so not cool.

    I am not dead, Jim! He lies! I live!
    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
  •