class question

This is a discussion on class question within the C++ Programming forums, part of the General Programming Boards category; I was just playing with classes for the first time... please see the code below.. Code: #include <iostream.h> #include <stdio.h> ...

  1. #1
    Unregistered
    Guest

    Unhappy class question

    I was just playing with classes for the first time...
    please see the code below..

    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    
    
    class person
    {
    	char name[30];
    	
    	public:
    	void get****();
    	void show****();
    	int age;
    };
    
    void person :: get****()
    {
    	cout << "Enter name : ";
    	cin >> name;
    	cout << "Enter age : ";
    	cin >> age;
    }
    
    void person :: show****()
    {
    	cout << "Name: "<< name << endl;
    	cout << "Age: " << age << endl;
    }
    
    
    class anotherclass
    {
    
    	public:
    	int huh;
    	void anotherfunc();
    };
    
    
    
    void anotherclass :: anotherfunc()
    {
    	huh = 5;
    }
    
    
    main()
    {
    	int i;
    
    	person someone[2];
    	anotherclass someone1;
    
    	for(i=0; i<=1; i++)
    	{
    		someone[i].get****();
    	}
    
    	for(i=0; i<=1; i++)
    	{
    		someone[i].show****();
    	}
    
    	cout << "age "<< someone[0].age << endl;
    	cout << someone1.huh << endl;
    
    	getchar();
    
    }

    now, I can access someone[0].age variable without any problem..

    why can't i access someone1.huh ?

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    (Those stars won't work don't know what're they doing)

    > why can't i access someone1.huh ?
    You can. But you haven't initialized it.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Use a constructor:

    Code:
    class anotherclass
    {
    
    	public:
    	int huh;
    	anotherclass();
    };
    
    
    
    anotherclass :: anotherclass()
    {
    	huh = 5;
    }

  4. #4
    Unregistered
    Guest

    What's the difference ?

    what is the difference between

    Code:
    class anotherclass
    {
    
    	public:
    	int huh;
    	void anotherfunc();
    };
    
    
    
    void anotherclass :: anotherfunc()
    {
    	huh = 5;
    }
    and.....

    Code:
    class anotherclass
    {
    
    	public:
    	int huh;
    	anotherclass();
    };
    
    
    
    anotherclass :: anotherclass()
    {
    	huh = 5;
    }
    except the function name ????

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Well, pretty obvious, the anotherclass function is the class' constructor. You know about constructors/destructors, do you?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If ya don't, a constructor is basically a function with the same name as the class, which will execute whenever you create an instance of the class.

  7. #7
    Unregistered
    Guest

    then...

    then why is the former code not working ???

    i am posting my whole code here...


    Code:
    //program to learn classes
    
    #include <iostream.h>
    #include <stdio.h>
    
    
    
    class person
    {
    	char name[30];
    	//int age;
    
    	public:
    	void get****();
    	void show****();
    	int age;
    };
    
    void person :: get****()
    {
    	cout << "Enter name : ";
    	cin >> name;
    	cout << "Enter age : ";
    	cin >> age;
    }
    
    void person :: show****()
    {
    	cout << "Name: "<< name << endl;
    	cout << "Age: " << age << endl;
    }
    
    
    class anotherclass
    {
    
    	public:
    	int huh;
    	void anotherfunc();
    };
    
    
    
    void anotherclass :: anotherfunc()
    {
    	huh = 5;
    }
    
    
    main()
    {
    	int i;
    
    	person someone[2];
    	anotherclass someone1;
    
    	for(i=0; i<=1; i++)
    	{
    		someone[i].get****();
    	}
    
    	for(i=0; i<=1; i++)
    	{
    		someone[i].show****();
    	}
    
    	cout << "age "<< someone[0].age << endl;
    	cout << someone1.huh << endl;
    
    	getchar();
    
    }

    now the huh value that it is printing is not correct..
    can anyone tell me why ?


    http://mahurshi.tripod.com/mainframes.htm

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you want to use your own function then why aren't you
    using it!!!!!!!!!!!!

    Code:
    main()
    {
    	int i;
    
    	person someone[2];
    	anotherclass someone1;
    
    	for(i=0; i<=1; i++)
    	{
    		someone[i].get****();
    	}
    
    	for(i=0; i<=1; i++)
    	{
    		someone[i].show****();
    	}
    
    	cout << "age "<< someone[0].age << endl;
    	 //This is the functions that set's the value of huh 
                    someone.anotherfunc();
                    cout << someone1.huh << endl;
    
    	getchar();
    
    }
    btw, it is not a good idea to write your own init function, just use
    a constructor, because an object's constructor is called when
    you create it, therefore you could set the value of huh in the
    constructor.

  9. #9
    Unregistered
    Guest

    but...

    under class person

    i put two functions and they're executing...
    (i.e. without calling from main seperately)


    but when i put this anotherfunc() in the
    class anotherclass

    why should i call it from the main ?


    http://mahurshi.tripod.com/mainframes.htm

  10. #10
    Unregistered
    Guest

    Talking ohhhh

    sorry... now I understand...

    I printed the thing and observed my
    code on paper..

    thanks :-D

    http://mahurshi.tripod.com/mainframes.htm

  11. #11
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    The absence of a return value in the constructor specification indicates that it is a constructor (along with the constructor name being the same as the class name), as opposed to some other function.

    I.e. in the class

    class anotherperson
    {
    public:
    anotherperson()
    };

    anotherpersion() is the constructor. A constructor is called automatically whenever an object of that class is instantiated.

    I.e., when the following declaration is executed

    anotherperson ap;

    The anotherpersion constructor will be called automatically. Typically constructors are used to initialise member data (such as setting huh equal to 5).

    It also possible to have multiple constructors, where different constructor may be called depending upon how the class is instantiated. For example, your anotherperson class may have two constructors as follows:

    anotherclass();
    anotherclass(int h);

    The anotherclass() constructor is known as the default constructor because it will be called when instantiating the object under no special conditions.

    For example, the 'integer constructor' may do the following:

    anotherclass::anotherclass(int h)
    {
    huh = h;
    }

    i.e. it sets h to specific desired value, as opposed to a default value of 5. To ensure this constructor is called (rather than the default constructor), you would declare an object as follows:

    anotherclass ap(25);

    This would set huh to 25 rather than 5.

    You should also note that in C++, there are destructors also. These are called automatically when an instance of the object is destroyed, and are typically used to free heap memory being used by that class.

    Destructors always take the form of the class name pre-pended by a tilda (~). Unlike constructors, there can only be one destructor.

    I.e. to declare a destructor, you would write:

    class anotherclass
    {
    public:
    ~anotherclass();
    }

    Constructors & destructors are a really important topic in C++ & I suggest you become familiar with them by following examples from a decent book.

    Davros

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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