-
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 ?
-
(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.
-
Use a constructor:
Code:
class anotherclass
{
public:
int huh;
anotherclass();
};
anotherclass :: anotherclass()
{
huh = 5;
}
-
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 ????
-
Well, pretty obvious, the anotherclass function is the class' constructor. You know about constructors/destructors, do you?
-
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.
-
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
-
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.
-
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
-
ohhhh
sorry... now I understand...
I printed the thing and observed my
code on paper..
thanks :-D
http://mahurshi.tripod.com/mainframes.htm
-
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