2Questions:The double semicolon and right way to store classes

I

inuy4sha

1) I know that when I create a class I can access its instance methods
(functions) through the sintax
classname.method(parameters);
so I'm asking you: what does the std::cout means.. what is this sintax
telling about the relation between the "std" and the "cout" ? I hope I
explained my self..

2) I'd like to create a class with its methods and private data.. I
see that most of the time the methods are specified in a cpp file
while the "class interface" (I'm probably thinking in a java way) that
specifies which methods are included and if they're private or public,
is included in a header file: for istance:

// FILE MyClass.h
class MyClass{
private:
method1();
int [] ...;
public:
MyClass();
MyClass(....);
method2()
};

// FILE MyClass.cpp
MyClass::MyClass(){
...
}
......

then I can make a main in a third file and tells the compiler to
include the two file MyClass.cpp and MyClass.h ?? Is this the right
way to work out things?
Thanks in advance
 
V

Victor Bazarov

1) I know that when I create a class I can access its instance methods
(functions) through the sintax
classname.method(parameters);
so I'm asking you: what does the std::cout means.. what is this sintax
telling about the relation between the "std" and the "cout" ? I hope I
explained my self..

'A::B' means that 'B' is declared _inside_ 'A'. In your particular case
'cout' [object] is declared inside 'std' [namespace].
2) I'd like to create a class with its methods and private data.. I
see that most of the time the methods are specified in a cpp file
while the "class interface" (I'm probably thinking in a java way) that
specifies which methods are included and if they're private or public,
is included in a header file: for istance:

// FILE MyClass.h
class MyClass{
private:
method1();
int [] ...;
public:
MyClass();
MyClass(....);
method2()
};

// FILE MyClass.cpp
MyClass::MyClass(){
...
}
.....

then I can make a main in a third file and tells the compiler to
include the two file MyClass.cpp and MyClass.h ?? Is this the right
way to work out things?

Usually, yes.

V
 
J

Juha Nieminen

1) I know that when I create a class I can access its instance methods
(functions) through the sintax
classname.method(parameters);
so I'm asking you: what does the std::cout means.. what is this sintax
telling about the relation between the "std" and the "cout" ? I hope I
explained my self..

The thing at the left of :: is a class or a namespace, while the
thing at the left of . is an object (ie. an instance of a class).
That's the difference.

When you write "std::cout" you know that "std" is the name of either
a namespace or a class (could well be either one), and "cout" is
declared inside it. If you wrote "std.cout" it would mean that you
have an object (a class instance, ie. a "variable") which name is
"std", and the class from which this "std" is an instance has something
inside it named "cout".
 
J

Jacek Dziedzic

> 1) I know that when I create a class I can access its instance methods
> (functions) through the sintax
> classname.method(parameters);
> so I'm asking you: what does the std::cout means.. what is this sintax
> telling about the relation between the "std" and the "cout" ? I hope I
> explained my self..

Here 'std' is a namespace, so std::cout means "use the cout
symbol defined in the cout namespace. The "::" operator is
also used for classes when you access _static_ methods.
I believe that in Java you access static methods with a '.',
just like normal methods. In C++ you use "::", as in
MyClass::my_static_method();
> 2) I'd like to create a class with its methods and private data.. I
> see that most of the time the methods are specified in a cpp file
> while the "class interface" (I'm probably thinking in a java way) that
> specifies which methods are included and if they're private or public,
> is included in a header file: for istance:
>
> // FILE MyClass.h
> class MyClass{
> private:
> method1();
> int [] ...;
> public:
> MyClass();
> MyClass(....);
> method2()
> };
>
> // FILE MyClass.cpp
> MyClass::MyClass(){
> ...
> }
> .....

That's the typical thing to do, unless you're working
with templates.
> then I can make a main in a third file and tells the compiler to
> include the two file MyClass.cpp and MyClass.h ?? Is this the right
> way to work out things?

Almost. You include the '.h' file, but you _don't_ include the
..cpp file, you just compile the two (main.cpp and MyClass.cpp)
together (or link them together later). So with a g++ compiler this
would look like:

g++ main.cpp MyClass.cpp

If you are working in a project-based environment or use a
Makefile, this is usually done for you. Anyway, the thing is
you usually don't "#include" cpps inside other cpps, you
just tell the compiler there are more than one cpps.

HTH,
- J.
 
B

BobR

Jacek Dziedzic said:
Here 'std' is a namespace, so std::cout means "use the cout
symbol defined in the cout namespace.

Typo, S/B:

Here 'std' is a namespace, so std::cout means "use the cout
symbol defined in the *std* namespace.

Don't want to confuse newbies, do we? <G>
 

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
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top