Beginner C++ class problem

K

kackson

Hi.
What does it mean when it is written as:

in the test.hpp file->

class MYFUNCTION: public TOP
{
public:
....some variables...
MYFUNCTION(int x, int y, int z.. and some more argument);
.... and some more other variables and functions...;
}

and then in the test.cpp file->

MYFUNCTION::MYFUNCTION(int z, int y, int z,.. and some more
argument):TOP(x,y,z,w),_aa(a), _bb(b),_cc(c)
{
....some functions...;
}

What I dont understand is the meaning of the test.cpp ->
MYFUNCTION::MYFUNCTION:TOP(){}

What does this mean :: and follow by : and then a pair of {} means?
Could someone point me to a website where I could read such basic
things?

Thanks.
 
G

Gianni Mariani

kackson said:
Hi.
What does it mean when it is written as:
.....

What I dont understand is the meaning of the test.cpp ->
MYFUNCTION::MYFUNCTION:TOP(){}

What does this mean :: and follow by : and then a pair of {} means?
Could someone point me to a website where I could read such basic
things?

I think you're referring to the initializer list.

This is a simpler *compilable* example.

#include <iostream>

class B
{
public:
B( int );

int b;
};

class A : public B
{
public:

A( int );
};


A::A( int val )
: B( val ) // <<< initializer list
{
std::cout << "A is constructed\n";
}

B::B( int val )
: b( val ) // <<< initializer list
{
std::cout << "B is constructed\n";
}


A a( 2 );

int main()
{
}

The initializer list is only somthing you can place on a constructor.
 
J

John Carson

kackson said:
Hi.
What does it mean when it is written as:

in the test.hpp file->

class MYFUNCTION: public TOP
{
public:
...some variables...
MYFUNCTION(int x, int y, int z.. and some more argument);
... and some more other variables and functions...;
}

and then in the test.cpp file->

MYFUNCTION::MYFUNCTION(int z, int y, int z,.. and some more
argument):TOP(x,y,z,w),_aa(a), _bb(b),_cc(c)
{
...some functions...;
}

What I dont understand is the meaning of the test.cpp ->
MYFUNCTION::MYFUNCTION:TOP(){}

What does this mean :: and follow by : and then a pair of {} means?
Could someone point me to a website where I could read such basic
things?

Thanks.

You need a book. This one can be downloaded for free:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
 
J

Jon Bell

in the test.hpp file->

class MYFUNCTION: public TOP
{
public:
...some variables...
MYFUNCTION(int x, int y, int z.. and some more argument);
... and some more other variables and functions...;
}

and then in the test.cpp file->

MYFUNCTION::MYFUNCTION(int z, int y, int z,.. and some more
argument):TOP(x,y,z,w),_aa(a), _bb(b),_cc(c)
{
...some functions...;
}

What I dont understand is the meaning of the test.cpp ->
MYFUNCTION::MYFUNCTION:TOP(){}

What does this mean ::

That's the "scope resolution operator." On its left is the name of a
class, and on the right is the name of a member function of that class.
You use this notation when you define a member function because different
classes can have member functions with the same name, so when you define a
member function, you have to say which class it belongs to.
and follow by :

This particular member function is the constructor for class
MYFUNCTION. (You can tell it's the constructor because it has the same
name as the class itself.) The list of things after the : is the
constructor's "initialization list." Basically it says, take the
arguments x,y,z,w and use them to initialize the member data of class TOP
(from which MYFUNCTION is derived), then take the argument a and use it to
initialize the member data _aa of MYFUNCTION, etc.
and then a pair of {} means?

If this constructor had any actual executable statements, they would go
between these braces, just like in any other function. In this particular
constructor, all the work is done in the initialization list, so it
doesn't need any executable statements, but you still have to have the
empty {} to keep the compiler happy.
Could someone point me to a website where I could read such basic
things?

Try a decent textbook instead. If you don't want to pay for a printed
book, I understand the free downloadable e-book "Thinking in C++" at
<http://www.bruceeckel.com/> is OK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top