πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #940

Re: Classes in multiple files

#940
From: Roeland Huys
Date: Thu, 13 Jan 2000 00:00
228 lines
6189 bytes
Some basic rules:
1- The class declarations must become in a header file or more header
files. It is forbidden to put definitions in the header
    No function body's, no global variable declarations, ...
2- For the class definitions (method bodies, ...) you make one or more cpp
files. These files must include the header files from above.
This kind of structure is mostly used.

EX:

============
CLASS1.H
------------
#include <class_related_standard_headers.h>
#include "class_related_user_headers.h"

class A1
{
    A1();
    virtual void blablabla();
);

==========
CLASS1.CPP
----------
#include <context-related-standard-header-files.h>
#include "class1.h"

A1::A1()
{
    // constructor header
}

void A1:blablabla()
{
}
====================
============
CLASS2.H
------------
#include "class1.h"
#include <class_related_standard_headers.h>
#include "class_related_user_headers.h"

class A2: public A1
{
    A2();
    virtual void blablabla();
);

==========
CLASS2.CPP
----------
#include <context-related-standard-header-files.h>
#include "class2.h"

A2::A2() : A1()    // call mother constructor
{
    // constructor header
}

void A2:blablabla()
{
    A1::blablabla();
}
====================



David C. Fox wrote:

> 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.
Technical Details
Message-ID: <387E4D0F.145DA0CF@chello.be>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <38712C84.AFD15365@altern.org> <387391B3.12471AC5@cfa.harvard.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 11318c,e4d48c4967f0bc5
X-Google-Attributes: gid11318c,public
X-Google-Thread: faa64,e4d48c4967f0bc5
X-Google-Attributes: gidfaa64,public
X-Google-Thread: 10ba33,e4d48c4967f0bc5
X-Google-Attributes: gid10ba33,public
X-Google-Thread: 100a19,e4d48c4967f0bc5
X-Google-Attributes: gid100a19,public
From: Roeland Huys <roeland.huys@chello.be>
Subject: Re: Classes in multiple files
Date: 2000/01/13
Message-ID: <387E4D0F.145DA0CF@chello.be>
X-Deja-AN: 572296682
Content-Transfer-Encoding: 8bit
References: <38712C84.AFD15365@altern.org> <387391B3.12471AC5@cfa.harvard.edu>
Content-Type: text/plain; charset=iso-8859-1
X-Complaints-To: abuse@chello.be
X-Trace: news.chello.be 947801308 195.162.213.37 (Thu, 13 Jan 2000 23:08:28 MET)
Organization: Chello Usenet Service
MIME-Version: 1.0
NNTP-Posting-Date: Thu, 13 Jan 2000 23:08:28 MET
Newsgroups: gnu.gcc.help,gnu.g++.help,alt.os.linux,be.comp.os.linux