About classes and files ( h ? cpp? )

Hello everybody.

I have some confused about classes. Some help will be appreciated.
1.- Where is the class declared, at h file ? at cpp ? both ?
2.- Can I declare more tha one class into the same file ?
3.- In case afirmative, how can I create a class ?
Thank you
1: Either. That depends on your project.
2: Yes.
3: http://cplusplus.com/doc/tutorial/classes/

I hope this helps you with your confusion.

-Albatross
Indirect answers:

1. In order for a programmer to use a class, they have to include the header file in which the class is declared.
2. Try it and see.
3. There are plenty of examples you can find around here or on the 'net in general.
I have read the c++ tutorial (superb help). But I dont find no place where explain what happen with the files (h o r cpp) .
And I think I dont explain me well for question 3.
3.- If I can define more than one class into the same file (h?) . From the cpp with the main code (of course with #include "myclasses.h" ) how can I instanciate myclasses.class3 ?
Thanks
Last edited on
Very usefull article.
But...
I still have some doubts, but I going to try resoving mysefl
Thanks

One thing I find .h and .cpp filename as highly flexible. Image you call Test.h and Test.cpp so normally you will think the contents will have a class Test {.... } but C++ seem not focused on this analogy. You can declare/define a class name that is DIFFERENT from the .h and .cpp !

E.g
Test.h
class Test2 {

};

Test.cpp
Test2::methodA() {...}

This mean when you look at filename don't assume the class name is the SAME! In java the filename correspond to the actual class name defined so it is easier for us to maintain ppl code.
1. In order for a programmer to use a class, they have to include the header file in which the class is declared.

Well, almost... ...that's the easy answer appropriate for the OP.

When you get more advanced, you should actually prefer something called a forward declaration whenever possible, instead of including header files. When projects get really big, #includes with too much depth causes a really long rebuild.

For example, say you have a header file like this listing 1:
1
2
3
4
5
6
7
8
class B; // forward declaration for B, #include not necessary
class C; // forward declaration for C, #include not necessary

class A {
  private:
     B* b;
     C& c;
};


The basic idea is, if the compiler can determine the sizeof( A ) with just a forward declaration, then you don't need to #include the headers B and C. In listing 2 below, you must #include the headers because for the compiler to know the sizeof( A ), it must know the sizeof( B ) and sizeof( C ):
1
2
3
4
5
6
7
#include <B.hpp>   // must have sizeof(B) to determine sizeof(A)
#include <C.hpp>   // must have sizeof(C) to determine sizeof(A)

class A : public B {
  private:
     C c[10];
};


Anything including the listing 2 header, must be recompiled if B.hpp or C.hpp is touched.
But anything including the listing 1 header won't need recompilation even if B.hpp and C.hpp is touched.
Last edited on

#include <B.hpp> // must have sizeof(B) to determine sizeof(A)
#include <C.hpp> // must have sizeof(C) to determine sizeof(A)


This is strange. Usually we include header files of those classes instead. That is, e.g

#include <B.h>
#include <C.h>

class A : public B {
...
}
That's right, in the definition of class A in Listing 2, you must include the headers for B and C. In the definition of class A in Listing 1, you should just forward declare instead (to decrease the degree of dependency/coupling between your headers).

As for B.h or B.hpp - they're basically the same - it's just a matter of preferences. With B.hpp you basically know it's a C++ header file, with B.h, it's somewhat more ambiguous (is it a C header or a C++ header?). Do compilers like gcc care? I just always use .hpp in C++ for clarity.
Last edited on
I cannot make this run....

afile.h: ( two classes into the same file)

1
2
3
4
5
6
7
8
9
10
11
12
13
class A {
A();  // constructor 
public  tellme();
};
A::tellme(){}

class B {
B();  // constructor 
}
B::afunction (){
  A instance_of_A;
  instance_of_A.tellme();
}

( dont see if there is some , or ; neccesary, it is an example)

Where can I create instance_of_A ?
Into the B() constructor ?
Into the B::function() ? how ?
(Now if I have A instance_of_A; i get an error compile : A::A() is private in this context )
( With mingw)
Help please .... (I can not find nothing at the web )

And another question (regarding the last posts), modern compilers understand that a .h is c++ ? or not.

Thanks
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class A {
public:  //  'public' needs a colon after it
   // unlike some other languages, you don't declare each function as public/private.
   //  Instead... everything after the above 'public:' line will be public
   //  (until you put some other qualifier like private: or protected:

  // you should put the constructor AFTER your 'public:' label if you want it to be public
A();

  void  tellme(); // functions that aren't a constructor (or destructor) need a return type.
    // if you're not returning anything, make it a void
};

void  A::tellme(){}  // you need to do that here, too


class B {
public:  // again, you must make members public if you wish to access them outside the class

B();
void afunction();  // if you want B to have 'afunction', you must declare it
   // in B
};  // you need a semicolon (;) after class definitions


void B::afunction (){  //  don't forget the return type here, too!
   //  also this didn't work before because you didn't put the 'afunction' declaration
   //  inside your B class.
  A instance_of_A;
  instance_of_A.tellme();
}


And another question (regarding the last posts), modern compilers understand that a .h is c++ ? or not.


It's whatever it gets compiled as. Headers aren't compiled on their own (usually). They're #included into compiled files.

If you #include a .h file in a .cpp file, then it gets compiled as C++ code, even if it's C code.
If you #include a .h file in a .c file, then it gets compiled as C code, even if it's C++ code.
Last edited on
Thank you very much and specially for your patience.
The constructor must be public, that is the problem.
Good weekend


If you #include a .h file in a .cpp file, then it gets compiled as C++ code, even if it's C code.
If you #include a .h file in a .c file, then it gets compiled as C code, even if it's C++ code.

Thanks for the reminder, Disch.

This is why we often see something like this in a .hpp file:
1
2
3
extern "C" {
#include "SomeFile.h"  // in this example, .h is a C header, this .hpp file is a C++ header
};

so the C++ mangler does the right thing and the linker is happy.
Last edited on
Topic archived. No new replies allowed.