Separating two classes in two files


 
Thread Tools Search this Thread
Top Forums Programming Separating two classes in two files
# 1  
Old 10-13-2011
Separating two classes in two files

I have a file Map.hh shown below. I want to put the two classes Phase and Map in two different files Phase.hh and Map.hh. I have forward declaration before the Map class. How can I tackle this situation?

Code:
//////////////////////////////////////////////////////////////////////////

#ifndef Map_hh
#define Map_hh

#include "ElMap.hh"

#include "GenFunc.hh"
#include "Layer.hh"
#include "String.hh"
#include "DynBaseObj.hh"
#include "List.hh"
#include "Stack.hh"
#include "Tree.hh"


class Map;                                  // Forward declaration for class Map

// ***********************************************************************
// ***** CLASS: Phase ****************************************************
// ***********************************************************************

class Phase {

public:

  Phase();

  Phase(const Phase&  P);

  void  Reset();

  int  size() const;

  ElMap  Last() const;

  void  Convert (const String&  S);

  Phase&  operator = (const Phase&  P);

  void  operator += (const ElMap& E);

  friend bool  Compatible(
      const Phase&  ph,
      const Phase&  hi);

  friend
    bool  Equal(
      const Phase&  ph,
      const Phase&  hi);

//  friend operator += (
//            Phase &P,
//      const ElMap  E  )
//  {
//    P.L += E;
//  }

  friend bool  operator == (
      const Phase&  P1,
      const Phase&  P2);

  friend bool  operator != (
      const Phase&  P1,
      const Phase&  P2);

  friend ostream&  operator << (
      ostream&  os,
      const Phase&  P);

  friend class Map;

protected:

  List<ElMap>  L;

};

// ***********************************************************************
// ***** CLASS: Map ******************************************************
// ***********************************************************************

class Map {

public:

  Map();

  Map(const Map&  m);

//  ~Map();

  void  Start();

  void  Reset();

  void  Add(const Phase&  Ph);

  bool  ShotP() const;

  bool  ShotS() const;

  bool  IsTP(const int  lay) const;

  bool  IsTS(const int  lay) const;

  bool  IsRP(const int  lay) const;

  bool  IsRS(const int  lay) const;

  bool  Continue(const int  lay) const;

  void  Advance(
    const ElMap&  ev,
    const bool  cont);

  void  Print(ostream&  os) const;

  Map&  operator = (const Map&  m);

protected:

  Tree<ElMap>*  M;                                                   // Map

  List<Tree<ElMap> *>  Ptr;              // Pointers to possible directions

  bool  IsNext(const ElMap&  ev) const;

};

//////////////////////////////////////////////////////////////////////////

// ***********************************************************************
// ***** IMPLEMENTATION: Map *********************************************
// ***********************************************************************

inline Map::Map() {

  M = NULL;

}

//////////////////////////////////////////////////////////////////////////

inline Map::Map(const Map&  m) {

  M->Reset();
  M = m.M;
  Ptr = m.Ptr;

}

//////////////////////////////////////////////////////////////////////////

//  Map::~Map();
//  {
//      Reset();
//  }

//////////////////////////////////////////////////////////////////////////

void  Map::Start() {

  Ptr.Reset();
  for ( int i = 0; i < M->NChild(); i++ ) { Ptr += M->GetChild(i); }

}

//////////////////////////////////////////////////////////////////////////

void  Map::Reset() {

  Ptr.Reset();
  if (M != NULL) {
      M->Reset();
      delete M;
  }

}

//////////////////////////////////////////////////////////////////////////

// Create an ElMap tree. ElMap output stream << is overloaded
// so that it can be used for output.

void  Map::Add(const Phase&  Ph) {

  int  i = 0;
  Tree<ElMap>*  T = M;
  int  n;

  if (M == NULL) {
      M = new Tree<ElMap>(ElMap(RT, 0));    // Start from root of the tree
      T = M;
  }

// Ph.L.size() defines the number of phases to consider
  for (i = 0; i < Ph.L.size(); i++) {
      n = T->InsNDup(Ph.L[i]);
      T = T->GetChild(n);
  }

}

//////////////////////////////////////////////////////////////////////////

inline bool  Map::ShotP() const {

  return (M->IsChild(ElMap(SP,0)));

}

//////////////////////////////////////////////////////////////////////////

inline bool  Map::ShotS() const {

  return (M->IsChild(ElMap(SS,0)));

}

//////////////////////////////////////////////////////////////////////////

inline bool  Map::IsTP(const int  lay) const {

  return (IsNext(ElMap(TP,lay)));

}

//////////////////////////////////////////////////////////////////////////

inline bool  Map::IsTS(const int  lay) const {

  return (IsNext(ElMap(TS,lay)));

}

//////////////////////////////////////////////////////////////////////////

inline bool  Map::IsRP(const int  lay) const {

  return (IsNext(ElMap(RP,lay)));

}

//////////////////////////////////////////////////////////////////////////

inline bool  Map::IsRS(const int  lay) const {

  return (IsNext(ElMap(RS,lay)));

}

//////////////////////////////////////////////////////////////////////////

bool  Map::Continue(const int  lay) const {

  if (lay == 0) { return (false); }
  if (Ptr.size() == 0) { return (true); }

  ElMap  ev;

  for (int i = 0; i < Ptr.size(); i++) {

      ev = Ptr[i]->GetVal();
      switch (ev.Ev) {
          case FS : return (true);
          case TP :
          case TS :
          case RP :
          case RS : if (ev.Lay != lay) { return (true); }
      }

  }

  return (false);

}

//////////////////////////////////////////////////////////////////////////

void  Map::Advance(
  const ElMap&  ev,
  const bool  cont) {

  int  i, j;
  ElMap  ev1;

  // Delete pointers that stop
  i = 0;
  while (i < Ptr.size()) {

      ev1 = Ptr[i]->GetVal();

      if (cont) {

          if ((ev.Lay == ev1.Lay) && (ev.Ev != ev1.Ev)) {
              Ptr.Delete(i);
              i = 0;
          } else {
              i++;
          }

      } else {

          if (ev != ev1) {
              Ptr.Delete(i);
              i = 0;
          } else {
             i++;
          }

      }

  }

  // Append pointers that advance
  if (IsNext(ev)) {
      for (i = 0; Ptr[i]->GetVal() != ev; i++);
      for (j = 0; j < Ptr[i]->NChild(); j++) { Ptr += Ptr[i]->GetChild(j); }
      Ptr.Delete(i);
  }

}

//////////////////////////////////////////////////////////////////////////

// Map::Print
// Prints M (Tree<ElMap>*   M) in class Map using an output stream.
// The Tree class is declared in dynstr.h.
// Calls Tree<Type>::Print, where type is ElMap.

inline void  Map::Print(ostream&  os) const {

  M->Print(os);

}

//////////////////////////////////////////////////////////////////////////

inline Map&  Map::operator = (const Map&  m) {

  if (M != NULL) { M->Reset(); }
  M = m.M;
  Ptr = m.Ptr;
  return (*this);

}

//////////////////////////////////////////////////////////////////////////

bool  Map::IsNext(const ElMap&  ev) const {

  for (int i = 0; i < Ptr.size(); i++) {
      if ( (Ptr[i]->GetVal()) == ev ) { return (true); }
  }

  return (false);

}

// ***********************************************************************
// ***** IMPLEMENATATION: Phase ******************************************
// ***********************************************************************

//////////////////////////////////////////////////////////////////////////

inline Phase::Phase() {

  // empty

}

//////////////////////////////////////////////////////////////////////////

inline Phase::Phase(const Phase&  P) {

  L = P.L;

}

//////////////////////////////////////////////////////////////////////////

inline void  Phase::Reset() {

  L.Reset();

}

//////////////////////////////////////////////////////////////////////////

inline int  Phase::size() const {

  return (L.size());

}

//////////////////////////////////////////////////////////////////////////

inline ElMap  Phase::Last() const {

  return (L[L.size()-1]);

}

//////////////////////////////////////////////////////////////////////////

// Phase::Convert
// Convert a string to a Phase class
// In class Phase, create the Elmap list L (of type List<ElMap>) by
// setting ev and lay defined in class ElMap by passing the phase string.

void  Phase::Convert (const String&  S) {

  int  count = 0;
  bool  ex = false;
  String  NS = S;
  char  c1, c2;
  int  lay;

  cerr << endl << "map.h, Phase::Convert" << endl
       << "|-- Phases for (S)hot, (R)eflection, (T)ransmission "
       << "or (F)ree Surface" << endl;

  // Check for aliases
  if (NS == String("P")) {
      cerr << "|-- Phase Alias P encountered" << endl;
      NS = "SP FS";
  } else if (NS == String("S")) {
      cerr << "|-- Phase Alias S encountered" << endl;
      NS = "SS FS";
  } else if (NS == String("PP")) {
      cerr << "|-- Phase Alias PP encountered" << endl;
      NS = "SP RP2 FS";
  } else if (NS == String("PS")) {
      cerr << "|-- Phase Alias PS encountered" << endl;
      NS = "SP RS2 FS";
  }

  NS.ToUpper();
  cerr << "|-- Parameters, S = " << S << ", NS = " << NS << endl;

  L.Reset();

  do {

      // Read each phase entry from string NS
      do {
          NS >> c1;
      } while (c1 == ' ');

      NS >> c2;
      cerr << "|-- c1 = " << c1 << ", c2 = " << c2 << endl;

      switch (c1) {

      // SHOT, ElMap(Ev, Lay) sets Ev = ev and Lay = lay
      case 'S':

          if (count != 0) { error("|-- Bad PHASE string"); }
          if (c2 == 'P') {
              L += ElMap(SP, 0);       // Uses enum Events value SP
          } else if (c2 == 'S') {
              L += ElMap(SS, 0);       // Uses enum Events value SS
          } else {
              error("|-- Bad PHASE string, c1 = S, c2 not P or S");
          }
          break;

      // REFLECTION, ElMap(Ev, Lay) sets Ev = ev and Lay = lay
      case 'R':

          NS >> lay;
          if (c2 == 'P') {
              L += ElMap(RP, lay);     // Uses enum Events value RP
          } else if (c2 == 'S') {
              L += ElMap(RS, lay);     // Uses enum Events value RS
          } else {
              error("|-- Bad PHASE string, c1 = R, c2 not P or S");
          }
          break;

      // TRANSMISSION, ElMap(Ev, Lay) sets Ev = ev and Lay = lay
      case 'T':

          NS >> lay;
          if (c2 == 'P') {
              L += ElMap(TP, lay);     // Uses enum Events value TP
          } else if (c2 == 'S') {      //
              L += ElMap(TS, lay);     // Uses enum Events value TS
          } else {
              error("|-- Bad PHASE string, c1 = T, c2 not P or S");
          }
          if (lay == 0) { error("|- Two is not a valid map element"); }
          break;

      // FREE SURFACE, ElMap(Ev, Lay) sets Ev = ev and Lay = lay
      case 'F':

          if (c2 == 'S') {
              L += ElMap(FS, 0);       // Uses enum Events value FS
          } else {
              error("|-- Bad PHASE string, c1 = F, c2 not S");
          }
          ex = true;
          break;

      // DEFAULT
      default :

          error("|-- Bad PHASE string, c1 not S, R, T, or F");

      }  // switch c1

  } while ( !NS.End() && !ex );

  if ( ! ((L[0] == ElMap(SP, 0)) || (L[0] == ElMap(SS, 0))) ) {
      L.Insert(0, ElMap(SP, 0));
  }

}

//////////////////////////////////////////////////////////////////////////

inline Phase&  Phase::operator = (const Phase&  P) {

  L = P.L;
  return (*this);

}

//////////////////////////////////////////////////////////////////////////

inline void  Phase::operator += (const ElMap&  E) {

  L += E;

}

//////////////////////////////////////////////////////////////////////////

bool  Compatible(
  const Phase&  ph,
  const Phase&  hi) {

  int  iph = 0;
  int  ihi = 0;
  TWave  tw;

  if (hi.size() == 0) { return (true); }

  if (hi.L[0].Ev == SP)  { tw = PWave;
  } else  { tw = SWave; }

  while ((iph < ph.L.size()) && (ihi < hi.L.size())) {

      if (ph.L[iph] == hi.L[ihi]) {

          iph++;

      } else {

          switch (ph.L[iph].Ev) {

          case SP:

          case SS: return (false);

          case TP:

          case TS:

          case RP:

          case RS:

          case FS:
            if ((hi.L[ihi].Ev == RP) || (hi.L[ihi].Ev == RS)) {return (false);}
               if ((hi.L[ihi].Ev == TP) && (tw == SWave)) {return (false);}
               if ((hi.L[ihi].Ev == TS) && (tw == PWave)) {return (false);}
               break;
          }
      }

    if ((hi.L[ihi].Ev == RP) || (hi.L[ihi].Ev == TP)) { tw = PWave; }
    if ((hi.L[ihi].Ev == RS) || (hi.L[ihi].Ev == TS)) { tw = SWave; }

    ihi++;

  }

  return (true);

}

//////////////////////////////////////////////////////////////////////////

bool  Equal(
  const Phase&  ph,
  const Phase&  hi) {

  int  iph = 0;
  int  ihi = 0;
  TWave  tw;

  if (hi.size() == 0) { return (false); }

  if (hi.L[0].Ev == SP)  { tw = PWave;
  } else  { tw = SWave; }

  while ((iph < ph.L.size()) && (ihi < hi.L.size())) {

      if ( ph.L[iph] == hi.L[ihi] ) {

          iph++;

      } else {

          switch (ph.L[iph].Ev) {

          case SP:

          case SS: return (false);

          case TP:

          case TS:

          case RP:

          case RS:

          case FS:
            if ((hi.L[ihi].Ev == RP) || (hi.L[ihi].Ev == RS)) {return (false);}
            if ((hi.L[ihi].Ev == TP) && (tw == SWave)) {return (false);}
            if ((hi.L[ihi].Ev == TS) && (tw == PWave)) {return (false);}
            break;

          }

      }

      if ((hi.L[ihi].Ev == RP) || (hi.L[ihi].Ev == TP)) { tw = PWave; }
      if ((hi.L[ihi].Ev == RS) || (hi.L[ihi].Ev == TS)) { tw = SWave; }
      ihi++;

  }

  if (iph == ph.L.size()) { return (true); }
  return (false);

}

//////////////////////////////////////////////////////////////////////////

inline bool  operator == (
  const Phase&  P1,
  const Phase&  P2) {

  return (P1.L == P2.L);

}

//////////////////////////////////////////////////////////////////////////

inline bool  operator != (
  const Phase&  P1,
  const Phase&  P2) {

  return (P1.L != P2.L);

}

//////////////////////////////////////////////////////////////////////////

// List<ElMap>  L;

inline ostream&  operator << (
  ostream&  os,
  const Phase&  P) {

  return (os << P.L);

}

//////////////////////////////////////////////////////////////////////////

#endif

# 2  
Old 10-13-2011
They depend on each other to the point that if you include one include file, you'll probably end up needing to include the other. One could argue that they really do belong in the same header file. But...


Code:
/* map.hh */
#ifndef __MAP_H__
#define __MAP_H__

#include ... // all the stuff Map needs

// Forward declare only if necessary
#ifndef __PHASE_H__
class Phase; // Forward declaration
#endif/*__PHASE_H__*/

class Map {

public:

  Map();

  Map(const Map&  m);

//  ~Map();

  void  Start();

  void  Reset();

  void  Add(const Phase&  Ph);

  bool  ShotP() const;

  bool  ShotS() const;

  bool  IsTP(const int  lay) const;

  bool  IsTS(const int  lay) const;

  bool  IsRP(const int  lay) const;

  bool  IsRS(const int  lay) const;

  bool  Continue(const int  lay) const;

  void  Advance(
    const ElMap&  ev,
    const bool  cont);

  void  Print(ostream&  os) const;

  Map&  operator = (const Map&  m);

protected:

  Tree<ElMap>*  M;                                                   // Map

  List<Tree<ElMap> *>  Ptr;              // Pointers to possible directions

  bool  IsNext(const ElMap&  ev) const;

};

#endif/*__MAP_H__*/

Code:
/* phase.hh */

#ifndef __PHASE_H__
#define __PHASE_H__

#ifndef __MAP_H__
class Map; // forward declare only if necessary
#endif/*__MAP_H__*/

class Phase {

public:

  Phase();

  Phase(const Phase&  P);

  void  Reset();

  int  size() const;

  ElMap  Last() const;

  void  Convert (const String&  S);

  Phase&  operator = (const Phase&  P);

  void  operator += (const ElMap& E);

  friend bool  Compatible(
      const Phase&  ph,
      const Phase&  hi);

  friend
    bool  Equal(
      const Phase&  ph,
      const Phase&  hi);

//  friend operator += (
//            Phase &P,
//      const ElMap  E  )
//  {
//    P.L += E;
//  }

  friend bool  operator == (
      const Phase&  P1,
      const Phase&  P2);

  friend bool  operator != (
      const Phase&  P1,
      const Phase&  P2);

  friend ostream&  operator << (
      ostream&  os,
      const Phase&  P);

  friend class Map;

protected:

  List<ElMap>  L;

};
#endif/*__PHASE_H__*/


Last edited by Corona688; 10-13-2011 at 04:28 PM..
# 3  
Old 10-14-2011
Usually classes are put into separate files. So in situations like this you would have kept them together?
# 4  
Old 10-14-2011
It's never been that arbitrary. Imagine if you had to include a different .h file for every C structure! Headers are simply a means of organizing things.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help separating file lines into three classes

Hi folks, What I have are config files with lines that: are blank, start with a "!" or start with char's(or a blank space and then char's) I am using ksh I can display each line by doing: for INDEX in {0..$LENGTH} do echo "${data}" done What I need to do requires I can... (12 Replies)
Discussion started by: Marc G
12 Replies

2. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

3. Programming

c++ help with class(new to classes)

Hello there, I am new to using classes, and have been having so many problems. I don't want to go to my teacher if I don't have to, because it is always my luck that it is something easy that I just overlooked somehow. I have been working on this for 3 days and I can't get it to read from a file. ... (1 Reply)
Discussion started by: KingAroan
1 Replies

4. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

5. Shell Programming and Scripting

Separating Pattern Into Separate Files

I am trying to separate a specific pattern match into separate files. Sometimes there is only one pattern match, but other times there could be multiple (up to 6 or 8). Pattern is as follows - its starts with NYZ or VTZ and ends with $$. Again looking to get those blocks of data from one big... (17 Replies)
Discussion started by: Double-E
17 Replies

6. UNIX for Advanced & Expert Users

Grep character classes '\w' '\d'

I am learning regex fundamentals on my own and when I try to use \w for characters (i.e. ), or \d for digits () it doesnt work even though I see in greps man page that \w should be the same as ]... ] and those types of syntactic character classes do work for me, its just the shorthand \w \W and... (4 Replies)
Discussion started by: glev2005
4 Replies

7. Programming

Use of C++ Classes

I was wondering if I could put the section at the beginning rather than at the end before the definition of the class. const REAL ModMisfit::DefMinDT = 0.01; const REAL ModMisfit::DefSigma0 = 0.01; const double ModMisfit::DefDAngSh = 2; const REAL ModMisfit::DefKBeta = 5;... (2 Replies)
Discussion started by: kristinu
2 Replies

8. Shell Programming and Scripting

Merging files into a single tab delimited file with a space separating

I have a folder that contains say 50 files in a sequential order: cdf_1.txt cdf_2.txt cdf_3.txt cdf_3.txt . . . cdf_50.txt. I need to merge these files in the same order into a single tab delimited file. I used the following shell script: for x in {1..50}; do cat cdf_${x}.txt >>... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

9. UNIX for Dummies Questions & Answers

Simple script for separating files

Hi all, I was wondering if someone could help me writing a simple script on separating files into separate folders. I have 92 files called various things and I want to separate these folders like so: Create a folder called "1" and put files 1-23 in it Create a folder called "2" and put... (5 Replies)
Discussion started by: hertingm
5 Replies

10. Programming

how to use classes in c ?!?!?

Hi, I've tried to use classes in my program, but the compiler simply gives an error on the word class . Am I the only one with this problem ? I have no idea how to use classes in c in linux environment(suse). If you've got any idea what should I do I would be very thankful. Thanks to ya all !... (4 Replies)
Discussion started by: atticus
4 Replies
Login or Register to Ask a Question