Thread: Class Arguments???

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    Unhappy Class Arguments???

    My friend and I are very confused about how class arguments work. We have a class in C++ and up until now things have been going pretty smoothly... but this, we can't seem to wrap our minds around. I mostly understand how to set up the implementation file for a class, but how the arguments work eludes me.

    It seems like the class should kind of set up its own versions of the variables already declared? I'm not sure how the input works. ANY help or hints at all would be appreciated!

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I have no idea what you're asking. What arguments are you talking about? Arguments to a member function? Template arguments?

    Please elaborate (and post code if at all possible)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Do you mean constructor arguments?

    Perhalps you mean how overloading operator() works?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    My friend and I are very confused about how class arguments work.
    nobleman: The working class are uneducated sheep who need to be ruled with a firm hand.

    serf: The rich are greedy pigs who exploit the workers.

    Whoops, C++ classes...
    It seems like the class should kind of set up its own versions of the variables already declared? I'm not sure how the input works.
    You certainly do seem confused since it is even hard to decipher what in the heck you are talking about, so here goes. On a very basic level, you can think of a class as merely an array that can hold different types. Think back to arrays. With arrays, you have to declare the type of the array, and the array can only hold values of that type, e.g.
    Code:
    int numbers[3] = {10, 20, 30};
    All the numbers you store in that array have to be int's. On the other hand, a class can store values of any type. You can declare a class like this:
    Code:
    class Data
    {
    public:
    	int age;
    	double height;
    	string name;
    };
    A constructor is used to create an object or "instance" of the class. You give the constructor some values and it stuffs them inside the member variables of the class: age, height, and name. You can define a constructor like this:
    Code:
    class Data
    {
    public:
    	int age;
    	double height;
    	string name;
    
    	Data(int a, double h, string n)
    	{
    		age = a;
    		height = h;
    		name = n;
    	}
    };
    A constructor has the same name as the class, and you do not specify a return value like you are required to do for other functions.

    Something that's very alarming about that constuctor function is that it uses the variable names age, height, and name without first declaring them. Normally, any variables used in a function must be declared as parameters or declared in the body of the function--or else you will get a compiler error. However, when a function is inside of a class, and you want to use the class member variable names inside your function, you do not declare them; you just use their names.

    Here is how you would create an object of the Data class:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Data
    {
    public:
    	int age;
    	double height;
    	string name;
    
    	Data(int a, double h, string n)
    	{
    		age = a;
    		height = h;
    		name = n;
    	}
    };
    
    
    int main()
    {
    	Data myData(18, 6.2, "John");
    	
    	cout<<myData.name<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 04-23-2006 at 06:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM