public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Classes in multiple files
@ 2000-01-03 15:25 Thomas Lionel SMETS
  2000-01-05 15:04 ` David C. Fox
  2000-04-01  0:00 ` Thomas Lionel SMETS
  0 siblings, 2 replies; 4+ messages in thread
From: Thomas Lionel SMETS @ 2000-01-03 15:25 UTC (permalink / raw)
  To: help-gcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1490 bytes --]

Well the question should have a rather straight answer but I do know how

to do it, so ...

I wish to have two classes in two separated files, as such :


============== B E G I N    O F   F I L E  1   ====================
#include ...   // Only standart header files <iostream>, etc ...
class ClassName1 {
    // Implementation




}
============== E N D   O F    F I L E  1 =======================

============== B E G I N    O F   F I L E  2   ====================
#include ...   // Only standart header files <iostream>, etc ...
class ClassName2 : class ClassName1 {
    // Implementation




}

int main() {
    // Implementation of pgrm
}
============== E N D   O F    F I L E  2 =======================

Now the question is :
    How do I compile this properly ?

Currently my make file is, as such :

============= Begin of Make File ===================
ClassName1 : ClassName1.cpp
    g++ ClassName1.cpp -o ClassName1

ClassName2 : ClassName2.cpp ClassName1.o
    g++ ClassName2.cpp ClassName1.o  -o ClassName2

ClassName1.o : ClassName1.cpp
    g++ -c ClassName1.cpp
============= End of Make File ===================

As I don't know how to make Header files for other thiings than C
function --> there is no Header file comin' from my code. Secondly ... I

don't link the ".o" files ... Maybe it's a mistake ?

A kind answer would be appreciated   :-)  !

Thanks in advance,

Thomas,

--
mail : thomas.smets@gonthier-be.com (professionnel)
       tsmets@altern.org (privé)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Classes in multiple files
  2000-01-03 15:25 Classes in multiple files Thomas Lionel SMETS
@ 2000-01-05 15:04 ` David C. Fox
  2000-04-01  0:00   ` David C. Fox
  2000-04-01  0:00 ` Thomas Lionel SMETS
  1 sibling, 1 reply; 4+ messages in thread
From: David C. Fox @ 2000-01-05 15:04 UTC (permalink / raw)
  To: help-gcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3496 bytes --]

Thomas Lionel SMETS wrote:

...

> I wish to have two classes in two separated files, as such :
> 
> ============== B E G I N    O F   F I L E  1   ====================
> #include ...   // Only standart header files <iostream>, etc ...
> class ClassName1 {
>     // Implementation
> 
> }
> ============== E N D   O F    F I L E  1 =======================
> 
> ============== B E G I N    O F   F I L E  2   ====================
> #include ...   // Only standart header files <iostream>, etc ...
> class ClassName2 : class ClassName1 {
>     // Implementation
> 
> }

...

> Now the question is :
>     How do I compile this properly ?
> 

...

> Thanks in advance,
> 
> Thomas,
> 
> --
> mail : thomas.smets@gonthier-be.com (professionnel)
>        tsmets@altern.org (privé)

Classes generally have two parts: the class definition, and the
definitions of any member functions.  For example,

// define class First
class First
{
public:
  float fish(); //declare a member function called fish
// ...
private:
  int x; // declare an int data member
}; // end of class definition

// define member functions of First
float First::fish()
{
  return x*2.4;
}

Any source code which uses the class First needs access to the
definition of the class at compile time*, which includes the
declarations of the member functions (their names and argument and
return types).  It does not need access to the definitions of the member
functions (in object file .o form) until link time.  Thus, if I want to
define a class Second which inherits from First, but put that definition
in a separate file, I would organize the code as follows:

// file: First.h
class First
{
//define First
};

// file: First.C
#include "First.h"
// This insures that First.C uses the same definition of class First as
// everyone else.

// define member functions of First


// file: Second.C
#include "First.h"

class Second : public First
{
  float red_fish();
// define Second
};

// define members of Second
float Second::red_fish()
{
  return fish()*0.5; // half of fish are red
}


main()
{
// use Second
}

Then, I would compile First with

  g++ -c First.C

and compile and link Second with

  g++ -c Second.C

(#include gives g++ access to definition of First when compiling
Second.C)

  g++ -o together First.o Second.o

(linking with First.o gives the linker access to First's member
functions
so that it can fill in the function call to First::fish from
Second::red_fish)

or simply

  g++ -o together Second.C First.o


Of course, if I wanted to use class Second in another file,I should
separate its definition into Second.h.  Further complications could
arise if I wanted to use both classes in a third file.  Suppose I wrote

// file: Third.C
#include "First.h"

#include "Second.h"
// error: attempts to redefine class First, since Second.h also
// includes First.h

First f;
Second g;

In this case, I could avoid multiple definitions by simply omitting the
first (or do I mean First :-) #include in Third.C.  However, I would
have to remember that Second.h included First.h.  I could head off this
potential problem by changing the file First.h as follows:

// file: First.h, version 2
#ifndef MY_FIRST_CLASS
#define MY_FIRST_CLASS

// define First
class First
{
// ...
};

#endif

The extra # declarations to the preprocessor insure that only one
definition of First is included in any given source file which is
compiled.

David Fox



*except if the code only declares a pointer to an object of class First.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Classes in multiple files
  2000-01-05 15:04 ` David C. Fox
@ 2000-04-01  0:00   ` David C. Fox
  0 siblings, 0 replies; 4+ messages in thread
From: David C. Fox @ 2000-04-01  0:00 UTC (permalink / raw)
  To: help-gcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3496 bytes --]

Thomas Lionel SMETS wrote:

...

> I wish to have two classes in two separated files, as such :
> 
> ============== B E G I N    O F   F I L E  1   ====================
> #include ...   // Only standart header files <iostream>, etc ...
> class ClassName1 {
>     // Implementation
> 
> }
> ============== E N D   O F    F I L E  1 =======================
> 
> ============== B E G I N    O F   F I L E  2   ====================
> #include ...   // Only standart header files <iostream>, etc ...
> class ClassName2 : class ClassName1 {
>     // Implementation
> 
> }

...

> Now the question is :
>     How do I compile this properly ?
> 

...

> Thanks in advance,
> 
> Thomas,
> 
> --
> mail : thomas.smets@gonthier-be.com (professionnel)
>        tsmets@altern.org (privé)

Classes generally have two parts: the class definition, and the
definitions of any member functions.  For example,

// define class First
class First
{
public:
  float fish(); //declare a member function called fish
// ...
private:
  int x; // declare an int data member
}; // end of class definition

// define member functions of First
float First::fish()
{
  return x*2.4;
}

Any source code which uses the class First needs access to the
definition of the class at compile time*, which includes the
declarations of the member functions (their names and argument and
return types).  It does not need access to the definitions of the member
functions (in object file .o form) until link time.  Thus, if I want to
define a class Second which inherits from First, but put that definition
in a separate file, I would organize the code as follows:

// file: First.h
class First
{
//define First
};

// file: First.C
#include "First.h"
// This insures that First.C uses the same definition of class First as
// everyone else.

// define member functions of First


// file: Second.C
#include "First.h"

class Second : public First
{
  float red_fish();
// define Second
};

// define members of Second
float Second::red_fish()
{
  return fish()*0.5; // half of fish are red
}


main()
{
// use Second
}

Then, I would compile First with

  g++ -c First.C

and compile and link Second with

  g++ -c Second.C

(#include gives g++ access to definition of First when compiling
Second.C)

  g++ -o together First.o Second.o

(linking with First.o gives the linker access to First's member
functions
so that it can fill in the function call to First::fish from
Second::red_fish)

or simply

  g++ -o together Second.C First.o


Of course, if I wanted to use class Second in another file,I should
separate its definition into Second.h.  Further complications could
arise if I wanted to use both classes in a third file.  Suppose I wrote

// file: Third.C
#include "First.h"

#include "Second.h"
// error: attempts to redefine class First, since Second.h also
// includes First.h

First f;
Second g;

In this case, I could avoid multiple definitions by simply omitting the
first (or do I mean First :-) #include in Third.C.  However, I would
have to remember that Second.h included First.h.  I could head off this
potential problem by changing the file First.h as follows:

// file: First.h, version 2
#ifndef MY_FIRST_CLASS
#define MY_FIRST_CLASS

// define First
class First
{
// ...
};

#endif

The extra # declarations to the preprocessor insure that only one
definition of First is included in any given source file which is
compiled.

David Fox



*except if the code only declares a pointer to an object of class First.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Classes in multiple files
  2000-01-03 15:25 Classes in multiple files Thomas Lionel SMETS
  2000-01-05 15:04 ` David C. Fox
@ 2000-04-01  0:00 ` Thomas Lionel SMETS
  1 sibling, 0 replies; 4+ messages in thread
From: Thomas Lionel SMETS @ 2000-04-01  0:00 UTC (permalink / raw)
  To: help-gcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1490 bytes --]

Well the question should have a rather straight answer but I do know how

to do it, so ...

I wish to have two classes in two separated files, as such :


============== B E G I N    O F   F I L E  1   ====================
#include ...   // Only standart header files <iostream>, etc ...
class ClassName1 {
    // Implementation




}
============== E N D   O F    F I L E  1 =======================

============== B E G I N    O F   F I L E  2   ====================
#include ...   // Only standart header files <iostream>, etc ...
class ClassName2 : class ClassName1 {
    // Implementation




}

int main() {
    // Implementation of pgrm
}
============== E N D   O F    F I L E  2 =======================

Now the question is :
    How do I compile this properly ?

Currently my make file is, as such :

============= Begin of Make File ===================
ClassName1 : ClassName1.cpp
    g++ ClassName1.cpp -o ClassName1

ClassName2 : ClassName2.cpp ClassName1.o
    g++ ClassName2.cpp ClassName1.o  -o ClassName2

ClassName1.o : ClassName1.cpp
    g++ -c ClassName1.cpp
============= End of Make File ===================

As I don't know how to make Header files for other thiings than C
function --> there is no Header file comin' from my code. Secondly ... I

don't link the ".o" files ... Maybe it's a mistake ?

A kind answer would be appreciated   :-)  !

Thanks in advance,

Thomas,

--
mail : thomas.smets@gonthier-be.com (professionnel)
       tsmets@altern.org (privé)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2000-04-01  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-03 15:25 Classes in multiple files Thomas Lionel SMETS
2000-01-05 15:04 ` David C. Fox
2000-04-01  0:00   ` David C. Fox
2000-04-01  0:00 ` Thomas Lionel SMETS

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).