Thread: Passing objects to functions

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Post Passing objects to functions

    Hello, i have a question, so im reading a section on classes and pointers and its telling that we can modify a object using :

    <return type> <function name>(<class name> *<name of pointer>)

    so when they say modify the object, do they mean that you can modify the member variables of the object of a class?

    for example:
    if i had a class named Tires and in the class Tires, i had a member variable named PSI, could i pass the object to a function and change the value of PSI?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Yes.

    An object (an instance of a class, which is like a blueprint) consists of its fields (member variables). Fields are the only things you can change about an object.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by cyberfish View Post
    Yes.

    An object (an instance of a class, which is like a blueprint) consists of its fields (member variables). Fields are the only things you can change about an object.
    i still dont get how you would set it up, could you give me a example please?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Not tested.
    Code:
    class Tires
    {
    public:
       Tires() : psi_(0) {}
       void setPSI(const int psi) { psi_= psi; }
       int getPSI() const { return psi_; }
    
    private:
       int psi_;
    };
    
    void SetTirePsiTo30(Tires *t)
    {
        t->setPSI(30);
    }
    
    int main(void)
    {
        Tires t;
    
        // Because the argument is a pointer, we must pass the address of the variable.
        SetTirePsiTo30(&t);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    70
    is this correct??
    Code:
    #include <iostream>
    using namespace std;
    
    class Tires
    {
       public:
        int PSI;
        float width;
        int weight;
    
    
        void ChangePSI(Tires *change)
        {
        change->PSI = 100;
        }
    };
    int main()
    {
        Tires *ptrTires;
    
        ptrTires = new Tires;
    
        cout << "how much psi does your tire have?" << endl;
        cin >> ptrTires->PSI;
    
        ChangePSI(ptrTires);
    
        cout << ptrTires->PSI << endl;
        return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    ChangePSI won't be a member function then.

    But otherwise yes, you can do that.

    Although it's more natural to make it a member function, and do

    ptrTires->ChangePSI(100);

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by cyberfish View Post
    ChangePSI won't be a member function then.

    But otherwise yes, you can do that.

    Although it's more natural to make it a member function, and do

    ptrTires->ChangePSI(100);
    what do you mean ChangePSI wont be a member function??,i dont get what ptrTires->ChangePSI(100) would do

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Forget about pointers for now.

    Actually, forget about C++, too.

    Learn about general OOP concepts first would make your life much easier.

    The best I could find was the first page of Java's tutorial
    What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
    What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    Don't worry about inheritance for now.

    The second link has some Java code, but it should be pretty self explanatory.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by cyberfish View Post
    Forget about pointers for now.

    Actually, forget about C++, too.

    Learn about general OOP concepts first would make your life much easier.

    The best I could find was the first page of Java's tutorial
    What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
    What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    Don't worry about inheritance for now.

    The second link has some Java code, but it should be pretty self explanatory.
    I need the pointer though because without it i would only be changing the parameter, i want to change the actual value all together.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Well, what I am suggesting is, if you learn about general OOP concepts first, this problem will be very simple.

    ptrTires->ChangePSI(100); calls the ChangePSI function of the ptrTires object, which modifies the object's state.

    But that won't make sense until you learn about OOP.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by cyberfish View Post
    Well, what I am suggesting is, if you learn about general OOP concepts first, this problem will be very simple.

    ptrTires->ChangePSI(100); calls the ChangePSI function of the ptrTires object, which modifies the object's state.

    But that won't make sense until you learn about OOP.
    Okay, thanks for the advice and links! and your time

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    70
    Code:
    #include <iostream>
    using namespace std;
    
    class Waterbottle
    {
       public:
        string Brand;
        float height;
        int WaterLevelPercent;
    
        void Waterbottle::DrinksWater(Waterbottle *bottle)
        {
            bottle->WaterLevelPercent= bottle->WaterLevelPercent - 1;
        }
    };
    
    int main()
    {
        Waterbottle *ptrBottle;
        ptrBottle = new Waterbottle;
    
        ptrBottle->WaterLevelPercent = 100;
        DrinksWater(ptrBottle);
    
        cout << DrinksWater->WaterLevelPercent << endl;
    
        return 0;
    }
    i tried making this to implement my skills with classes but, i get a error where it says that DrinksWater is not declared but, i declared it as a member function in the class definition

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The classname::methodname syntax is for implementing methods outside the class definition.

    If you define the method within the class definition (called an inline definition), you can just say "void DrinksWater".

    Also, you don't need the "bottle" parameter, because that is implicit.
    Code:
    class Waterbottle
    {
    ...
        void DrinksWater() { WaterLevelPercent--; }
    }
    It decreases the WaterLevelPercent of "this" Waterbottle.

    The method (action) is associated with "this" Waterbottle, and you can't use it to modify any other Waterbottle.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    70
    Code:
    #include <iostream>
    using namespace std;
    
    class Waterbottle
    {
       public:
        string Brand;
        float height;
        int WaterLevelPercent;
    
        void DrinksWater()
        {
           WaterLevelPercent--;
        }
    };
    
    int main()
    {
        Waterbottle CrystalGeyser;
    
        DrinksWater();
    
        cout << CrystalGeyser.WaterLevelPercent << endl;
    
        return 0;
    }
    i tried doing this, but, i still got the undeclared error

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    That's because you can't just call DrinksWater.

    What if you have many Waterbottles?

    Which waterbottle will that drink from?

    To tell it to drink from CystalGeyser, do GrystalGeyser.DrinksWater();

    It's just like how you can't do

    cout << WaterLevelPercent << endl;

    WaterLevelPercent of WHICH bottle?

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