tags:

views:

46

answers:

5

Alright, im trying to understand this,

so a class is simply creating a template for an object.

class Bow
{

int arrows;

};

and an object is simply creating a specific item using the class template.

Bow::Bow(arrows)
{
arrows = 20;
}

also another question, public specifiers are used to make data members avaible in objects and private specifiers are used to make data memebers only avaialble inside the class?

+3  A: 

The description you gave is mostly correct but your examples don't show what you are describing.

A class describes a set of data members and member functions which can be called on those data members:

class Bow
{
public:
   //Constructor
   Bow::Bow()
   {
     arrows = 20;
   }

   Bow::Bow(int a)
   {
     arrows = a;
   }


   //member functions (in this case also called accessors/modifiers)
   void getArrows(int a) const
   {
     return arrows;
   }

   void setArrows(int a)
   {
     arrows = a;
   }

protected:
  int arrows;
  };

And an object of a class is simply an instance of that class.

//b and d are objects of the class Bow.
Bow b(3);//The constructor is automatically called here passing in 3
Bow d(4);//The constructor is automatically called here passing in 4
Bow e;//The constructor with no parameters is called and hence arrows = 20

Note: I purposely avoided the use of the word template that you used because it's used for something entirely different in C++ than what you meant.


To understand public/private/protected specifiers:

public: means that objects of the class can use the members directly.

protected: means that objects of the class cannot use the members directly. Derived classes that are based on that class can use the members.

private: means that objects of the class cannot use the members directly. Derived classes that are based on that class cannot use the members either.

So in the above example:

Bow b(3);
b.arrows = 10;//Compiling error arrows is not public so can't be accessed from an object
Brian R. Bondy
really? cause the class is just creating member data, i didnt see the point in making methods. so i didnt initialize the class right?
TimothyTech
@TimothyTech: It is a set of data members and a set of member functions, either, both or none of those 2 sets can be empty.
Brian R. Bondy
i like your example. so are you making the arrows variable a const then giving the const a value in your second method?
TimothyTech
@TimothyTech: The const at the end of the second method just means that the function that follows will not change any of the member variables.
Brian R. Bondy
alrighty, thank you very much. you helped me out a bunch.
TimothyTech
+1  A: 

By using the public specifier, you allow anyone using your class to directly access that class member. By private you forbid this and there is no way for the user to directly access it. There is also the protected specifier, which allows derived classes to manipulate that class member.

You are correct that a class is something like a template for an object. When you create an instance of a class, you get the "real" thing, the realization of the template.

PeterK
+2  A: 

Public means the member is visible to code outside the class. Private means the member is visible only to code inside the class (or anything that class specifies is its friend).

For example:

class X { 
    int priv;
public:
    int pub;

    do_something() { 
        // since do_something() is part of X, it can change both pub and priv:
        priv = -1;
        pub = -2;
    }
};

int main() { 
    X x;

    x.pub = 2;    // allowed
    x.priv = 1;   // won't compile; x.priv isn't accessible.

    x.do_something();    // This is no problem though.
    return 0;
}
Jerry Coffin
wait so i can initialize the variables in a class like normal variables?
TimothyTech
To *initialize* them, you have to use a constructor, but you can *assign* to them like normal variables.
Jerry Coffin
+1  A: 

With regard to your first point, "Bow::Bow(arrows)" is a constructor which is a method for class Bow responsible for initializing an instance of that class. Your understanding of access modifiers is correct. Public members are visible outside of the class while Private members are not. Protected members are visible to classes deriving from that class.

Jim Lamb
A: 

an object is simply creating a specific item using the class template.

Replace the word "object" with "constructor" and "item" with "object". And drop the word "template", as this has a different meaning in C++.

public specifiers are used to make data members avaible in objects

Not quite. public makes members available to clients of an object (or a class, if it's a static member).

and private specifiers are used to make data memebers only avaialble inside the class

That is basically correct.

FredOverflow