Classes Prob

This is a discussion on Classes Prob within the C++ Programming forums, part of the General Programming Boards category; im tryin to do this and getin alot of errors to test my knowledge of classes i dont bother with ...

  1. #1
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178

    Classes Prob

    im tryin to do this and getin alot of errors to test my knowledge of classes i dont bother with shack got lazy lol

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class house 
    {
          public:
                 house();
                 ~house();
                 void setage(int old);
                 int getage();
                 void priceset(int price);
                 int getprice();
                 int rent(int is);
          private:
                  int old;
                  int price;
                  int is;
    }
    
    
    house::~house()
    {
                   cout << "if this is called the classe is demolished" << endl;
    }
    
    void house::setage(int old)
    {
         this->old = old;
    }
    
    int house::getage()
    {
        return old;
    }
    
    void house::priceset(int price)
    {
         this->price = price;
    }
    
    int house::getprice()
    {
        return price;
    }
    
    int house::rent(int is)
    {
         switch(is)
         {
                   case: int 1
                         return TRUE;
                         break;
                   default:
                         return FALSE;
         }
         if (is == TRUE)
         {
                cout << "For Rent!" << endl;
         }
    }               
    
    int main();
    {
        house Mansion;
        house Victorian;
        house Apartment;
        house Shack;
        
        Mansion.setage(2);
        Mansion.setprice(10000000);
        
        Victorian.setage(4);
        Victorian.setprice(800000);
        
        Apartment.setage(6);
        Apartment.setprice(80);
        
        Shack.setage(23);
        Shack.setprice(300);
        
        cout << "Four houses for sale, Mansion, Victorian, Apartment, and Shack" << endl;
        cout << endl << "The Mansion is " << Mansion.getage(); << "years old...." << endl;
        cout << "It costs a HEAPING $" << Mansion.getprice(); << "!" << endl;
        
        cout << endl <<" The Victorian is " << Victorian.getage(); << "years old" << endl;
        cout << " it costs $" << Victorian.getprice(); << "!" << endl;
        
        cout << endl <<" the Apartment is " << Apartment.getage(); << " years old" << endl;
        cout <<" it costs $" << Apartment.getprice() << "!" << endl;
        Apartment.rent(1);
        
        system("PAUSE");
        
        return EXIT_SUCCESS;
    }
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Switch (case) statement syntax is a bit off:
    Code:
    switch(var)
    {
       case 1:
          // blah blah blah...
         break;
       default:
          // etc...
    }
    Also, look at using the bool data type. It can take the values true and false. This seems to be what you are trying to do. (And TRUE and FALSE are not defined anywhere in your program.)

    In the future, please post the errors you are getting (exact compiler output if applicable, or the precise runtime behavior which you don't expect).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >class house
    >{
    >.
    >.
    >}
    Missing semicolon to end the class:
    Code:
    class house 
    {
    .
    .
    };
    > cout << endl << "The Mansion is " << Mansion.getage(); << "years old...." << endl;
    Extra semicolon. Should be:
    cout << endl << "The Mansion is " << Mansion.getage() << "years old...." << endl;
    You have a bunch of those in your cout's.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    You really dont have to use pointer accessors (or whatevr they r called, ->)

    Code:
     this->old = old;
    can be...
    Code:
    old = newold; //you need a different name you have too many conflicting types

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    REMEMBER!:

    Member functions can access Data Members...

    Meaning if you had this:
    Code:
    class test {
       public:
          test ();
          void foo (int one);
          void foo (int one, int two, int three);
       private:
          int num1, num2, num3;
    };
    Functions foo and the constructor can access num1, num2, num3 as if they were global variables.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    setprice() is not a method of your house class. You must mean the one you called priceset(), right?

    A really good way to avoid this kind of error is to use the >new word = capital letter< naming convention, like so:

    Code:
                 void setAge(int old);
                 int getAge();
                 void priceSet(int price);
                 int getPrice();
                 int rent(int is);
    See? It is suddenly easier to spot the error! At least I think so...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21