Thread: [Dev-C++] Question about class objects
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2002-10-23 11:33:14
|
When I create a class object, can I call a member function right away? I'm thinking that if I have private data I want to initialise when the object is created I could put in a member function that does it, but can I call it immediately? Instead of MyClass ObjectOne; ObjectOne.StartUp(); I know that a constructor initialisation list will do the same, can someone show me an example of that as well as I am a little confused by the syntax. Daniel _________________________________________________________________ Get a speedy connection with MSN Broadband. Join now! http://resourcecenter.msn.com/access/plans/freeactivation.asp |
|
From: Teodorescu C. <te...@xt...> - 2002-10-23 12:01:05
|
Daniel Glenfield wrote:
>
>
> When I create a class object, can I call a member function right away?
>
> I'm thinking that if I have private data I want to initialise when the
> object is created I could put in a member function that does it, but
> can I call it immediately? Instead of
>
> MyClass ObjectOne;
> ObjectOne.StartUp();
>
>
> I know that a constructor initialisation list will do the same, can
> someone show me an example of that as well as I am a little confused
> by the syntax.
class MyClass{
public :
MyClass (int x=0){
m_intX=x;
}
private:
m_intX;
}
void main (void ){
MyClass a,b(10);
//a.m_intX=0 and in b.m_intX=0;
}
for more info read Bruce Eckel thinking in c++ (http://www.mindview.com)
it's free.
>
> Daniel
>
>
>
> _________________________________________________________________
> Get a speedy connection with MSN Broadband. Join now!
> http://resourcecenter.msn.com/access/plans/freeactivation.asp
>
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future of Java(TM)
> technology. Join the Java Community Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--
Best Reagards Teodorescu Cristian Liviu (te...@xt...)
|
|
From: Abhijit S. <mu...@gm...> - 2002-10-23 13:00:55
|
Greets, Daniel.
> When I create a class object, can I call a member function right away?
>
> I'm thinking that if I have private data I want to initialise when the
> object is created I could put in a member function that does it, but can I
> call it immediately? Instead of
>
> MyClass ObjectOne;
> ObjectOne.StartUp();
>
>
> I know that a constructor initialisation list will do the same, can
someone
> show me an example of that as well as I am a little confused by the
syntax.
A constructor is basically a function that is called whenever an object of
the class is created. It's called in this case:
MyClass MyObject;
and also in this case:
pMyObject = new MyClass;
[-open-brace
But not here:
pMyObject = (MyClass *)malloc(sizeof(MyClass));
]-close-brace
The constructor should have the same name as the class, but no return type
(not void, it should have *no* return type), as in:
class MyClass
{
public:
MyClass(); // Constructor
~MyClass(); // Destructor
};
Constructors can take arguments, if necessary, like this:
MyClass(int some_foo);
You would invoke this constructor using
MyClass MyObject(666);
If you don't specify a constructor for a class, a default set of constructor
and destructor are provided for the class.
You don't have to explicitly call the constructor (in fact, you can't), it
is called whenever the object is created, and the destructor is called when
it goes out of scope. You can see the working if you write a test program
using cout within the constructor, something like this:
class foo
{
public:
foo()
{
cout << "The foo has risen.\n";
}
foo(int baz)
{
cout << "The foo has risen with " << baz
<< " coconuts.\n";
}
~foo()
{
cout << "The foo had you foolen. Now I will leave.\n";
}
}
and then creating objects using different means (static, dynamic).
___________________________________________________________
Abhijit Shylanath
E-mail: mu...@gm... || ibr...@bi...
Web-site: http://mudeth.tripod.com/
|
|
From: Per W. <pw...@ia...> - 2002-10-23 13:04:59
|
If you declare a method as static, you may call it before instancing
an object of the class type, i.e.
class MyClass {
public:
...
static void StaticMethod();
protected:
static int a_value;
};
int MyClass::a_value;
void MyClass::StaticMethod() {
a_value =3D rand();
}
int main() {
MyClass::StaticMethod();
MyClass obj;
return 0;
}
EMA System email: pw...@ia...
Per Westermark web: http://iapetus.neab.net
Vargvaegen 174B phone: +46 8 410 17 198
S-906 42 Umeaa
Sweden
On Wed, 23 Oct 2002, Daniel Glenfield wrote:
>=20
>=20
> When I create a class object, can I call a member function right away?
>=20
> I'm thinking that if I have private data I want to initialise when the=20
> object is created I could put in a member function that does it, but can =
I=20
> call it immediately? Instead of
>=20
> MyClass ObjectOne;
> ObjectOne.StartUp();
>=20
>=20
> I know that a constructor initialisation list will do the same, can someo=
ne=20
> show me an example of that as well as I am a little confused by the synta=
x.
>=20
> Daniel
>=20
>=20
>=20
> _________________________________________________________________
> Get a speedy connection with MSN Broadband.=A0 Join now!=20
> http://resourcecenter.msn.com/access/plans/freeactivation.asp
>=20
>=20
>=20
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future=20
> of Java(TM) technology. Join the Java Community=20
> Process(SM) (JCP(SM)) program now.=20
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
|