Thread: Passing objects to functions

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    70
    Yay! it worked ;D thanks a lot for your help, i kinda get classes a little bit better now.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    70
    when i do this:
    Code:
    Waterbottle *ptrBottle;
        ptrBottle = new Waterbottle;
    am i making a instance named ptrBottle of class Waterbottle??
    and if so, when i call a function would i have write ptrBottle.DrinkWater()??

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    70
    i've finished my waterbottle program:
    Code:
    #include <iostream>
    using namespace std;
    
    class Waterbottle
    {
       public:
        string Brand;
        float height;
        int WaterLevelPercent;
    
        void DrinkWater(Waterbottle *change)
        {
            change->WaterLevelPercent -= 10;
            if(change->WaterLevelPercent == 0)
            {
                cout << "No more water!" << endl;
            }
        }
    };
    
    int main()
    {
        Waterbottle *ptrBottle;
        ptrBottle = new Waterbottle;
        string prompt,prompt1;
        ptrBottle->WaterLevelPercent=100;
    
        cout << "Do you want to drink water?" << endl;
        cin >> prompt;
    
        if(prompt == "Yes" || "yes")
        {
            drink:
            ptrBottle->DrinkWater(ptrBottle);
            cout << "You have " << ptrBottle->WaterLevelPercent << " % left" << endl;
        }
        cout << "Do you want to drink again?" << endl;
        cin >> prompt1;
    
        if(prompt1 == "Yes" || "yes" && ptrBottle->WaterLevelPercent > 0)
        {
            goto drink;
        }
        else
        {
            cout << "You can't drink anymore water because there is no more!" << endl;
        }
        return 0;
    }
    any suggestions on making it better?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You still haven't learned anything.
    First off, why do you pass a pointer to ptrBottle->DrinkWater? Why? ptrBottle->DrinkWater implicitly belongs to ptrBottle. It's ptrBottle's DrinkWater function. Other Waterbottle have their own DrinkWater functions. This makes no sense.
    Code:
    if(prompt == "Yes" || "yes")
    This won't do what you think. Try entering "No" and it will still evaluate as true. You must be explicit:
    Code:
    if(prompt == "Yes" || prompt == "yes")
    On a further note, I suggest you use std::getline and not cin >>. It only causes problems.

    You are using goto. Don't. Learn how to formulate logic using loops instead.
    Code:
    if(prompt1 == "Yes" || "yes" && ptrBottle->WaterLevelPercent > 0)
    This won't do what you want either for the same reason as above.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by Elysia View Post
    You still haven't learned anything.
    First off, why do you pass a pointer to ptrBottle->DrinkWater? Why? ptrBottle->DrinkWater implicitly belongs to ptrBottle. It's ptrBottle's DrinkWater function. Other Waterbottle have their own DrinkWater functions. This makes no sense.
    Code:
    if(prompt == "Yes" || "yes")
    This won't do what you think. Try entering "No" and it will still evaluate as true. You must be explicit:
    Code:
    if(prompt == "Yes" || prompt == "yes")
    On a further note, I suggest you use std::getline and not cin >>. It only causes problems.

    You are using goto. Don't. Learn how to formulate logic using loops instead.
    Code:
    if(prompt1 == "Yes" || "yes" && ptrBottle->WaterLevelPercent > 0)
    This won't do what you want either for the same reason as above.
    But dont you need to have a pointer in the parameters of DrinkWater so it can point at an object and modify the member variables of that instance?

  6. #21
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The DrinkWater function BELONGS to the object. Every object has its own DrinkWater*. It knows to modify its own member variable. The object is "implicit", meaning you don't need to pass it in.

    *Actually, that's not technically true, but think of it that way for now. It will help you understand.

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    70
    I was wondering if you guys could think up a exercise using classes and ill try to make a program for it?? it doesnt have to be fancy and stuff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. passing objects
    By r0bbb in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2005, 01:10 PM
  4. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM