A little confused on classes

This is a discussion on A little confused on classes within the C++ Programming forums, part of the General Programming Boards category; I am getting a little confused over classes. Take this code for example. Code: #include <iostream> using std::cout; class dog ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    A little confused on classes

    I am getting a little confused over classes. Take this code for example.
    Code:
    #include <iostream>
    using std::cout;
    
    class dog
    {
    	public:
    		void GetAge() { cout << dogAge; }
    		void SetAge(int age) { dogAge = age; }
    	private:
    		int dogAge;
    };
    
    int main()
    {
    	dog Spot;
    	Spot.SetAge(5);
    	Spot.GetAge();
    	return 0;
    }
    Is GetAge() and SetAge() called accessor functions? And is dogAge a data member?

    EDIT: Had a little error in it, but it is fixed now.
    Last edited by Munkey01; 02-02-2003 at 03:06 PM.

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>Is GetAge() and SetAge() called accessor functions? And is dogAge a data member?

    You've got it.

    Have a look here:
    http://www.cprogramming.com/lightatdawn/Classes.html
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Seeing the code on that page reminded me of another question. What is the importantance of using your own constructor and destructor?

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I'm not very sure about the terminology...
    But, dogAge is a data member.
    I'm not sure about what do you call a set function, you might call it an accessor because it's access the data member and modify it so maybe you can call it a modifyer, but the get function does modify the data members so you may call it an observer, and the you should declare it as follows:
    Code:
    int getAge() const{
          return dogAge;
    }
    none...

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Munkey01
    Seeing the code on that page reminded me of another question. What is the importantance of using your own constructor and destructor?
    Sometimes you need to initialize your data members to some values, and you should do that in the constructor, or if you want ot call a specific function.
    If you've dynamically allocated data in your class, you should dynamically deallocate the data in the desctructor.

    Hope it's clear now, if not just tell me.
    none...

  6. #6
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I had already tried to use a return statement in the function (thinking it would be logical to do so) but it never displayed dogAge. Why is this? Oh, and I had forgotten about making it constant, thanks.


    Edit: What is the difference in dynamically allocating memory and statically allocating memory?

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    to display dogAge you call the function getAge() like this:
    Code:
    dog Spot;     //creat object
    cout << getAge();
    That will do the job, and you need to return the value in case you want to do something like this;
    Code:
    dog Spot;     //creat object
    int x;
    x = Spot.getAge();    //assign the age to a variable
    none...

  8. #8
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>What is the importantance of using your own constructor and destructor?

    You mean instead of the default ones? Read the example fully and all will become clear. The constructor is called the instant an instance of the class is created. This allows you to set up everything that needs setting up in your class at that point, without having to call another function manually for each instance. It is usually used to set data members to default values and such... usually. Destructors are called automatically when the variable goes out of scope. It is usually used to clear up any memory or perform any important clean-up actions safely and surely.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Ahhh. Thanks for clearing that up ammar. Now for the question about allocating memory.

  10. #10
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Munkey01
    I had already tried to use a return statement in the function (thinking it would be logical to do so) but it never displayed dogAge. Why is this? Oh, and I had forgotten about making it constant, thanks.


    Edit: What is the difference in dynamically allocating memory and statically allocating memory?
    statically allocated memory is the memory that's not allocated at run time, for example when you declare:
    Code:
    int array[100];
    that's statically allocated memory, but dynamically allocated memory is the memory allocated at runtime, using the new operator, for example:
    Code:
    int *array;
    array = new int[100];
    and it's better to use dynamically allocated memory, because you can allocate memoty at runtime, for example if you are making and array, and you want the user to enter the size of the array, you can do that by dynamic memory allocation.
    none...

  11. #11
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    And that memory is allocated on the heap?

  12. #12
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Yes...
    //second line
    none...

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I thought functions like SetAge() are modifiers and GetAge() are accessors?

  14. #14
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by alpha
    I thought functions like SetAge() are modifiers and GetAge() are accessors?
    As I said, I'm not very sure about the terminology, because actually I'm not very interested in it, I think you can call it whatever you want as long as you know how to use it.
    But ofcourse it's better if you know, how to use it, and what to call it at the same time.
    none...

  15. #15
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Terminology varies from different sources of teachings. As far as im concerned, they are both accessors, because they both access the data member.
    I AM WINNER!!!1!111oneoneomne

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 10:19 AM
  4. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM

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