Use of C++ Classes


 
Thread Tools Search this Thread
Top Forums Programming Use of C++ Classes
# 1  
Old 01-25-2010
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.

Code:
const REAL    ModMisfit::DefMinDT  = 0.01;
const REAL    ModMisfit::DefSigma0 = 0.01;
const double  ModMisfit::DefDAngSh = 2;
const REAL    ModMisfit::DefKBeta  = 5;
const bool    ModMisfit::DefHW     = false;

Outline of the C++ class is in a modmis.h file as below.


Code:
class ModMisfit
{

public:

  ModMisfit() : MinDist(-1),
                MDAcc(-1),
                MaxIter(-1),
                DAngSh(DefDAngSh),
                HW(DefHW),
                Extrap(true),
                MinDT(DefMinDT),
                Sigma0(DefSigma0),
                KBeta(DefKBeta),
                Vrb(none) { }

  void SetMaxIter(
      const int  maxiter );

  void SetDAngSh(
      const double  dangsh );

  void SetdTau(
      const REAL  dtau );
protected:

  static const REAL    DefMinDT;
  static const REAL    DefSigma0;
  static const double  DefDAngSh;

  int            NPhases, NSrc, MaxIter;
  REAL    MinDist, MDAcc,  MinDT, Sigma0, KBeta;
  double    DAngSh;
  bool        HW, Extrap;
  Source    **Src;
  List<REAL>   X, T1, T2;

  void ResetSrc();

};

void ModMisfit::SetdTau(
    const REAL  dtau )
{
    for ( int i = 0; i < NSrc; i++ )
    {
        Src[i]->SetdTau(dtau);
    }
}

// ***************************************************************************

void ModMisfit::SetIntegration(
    const Integration  intg )
{
    for ( int i = 0; i < NSrc; i++ )
    {
        Src[i]->SetIntegration(intg);
    }
}

etc

const REAL    ModMisfit::DefMinDT  = 0.01;
const REAL    ModMisfit::DefSigma0 = 0.01;
const double  ModMisfit::DefDAngSh = 2;
const REAL    ModMisfit::DefKBeta  = 5;
const bool    ModMisfit::DefHW     = false;

# 2  
Old 01-25-2010
You cannot define anything before it is declared. It will just complain 'no such class' or the like. Once it is declared though you can instantiate things in any order you like.

Anyway that's what headers are for. You should declare the class in the header file and instantiate functions and static values in the .cpp:
Code:
/* modmisfit.h */

/* these ifdefs prevent modmisfit.h being multiply included */
#ifndef __MODMISFIT_H__
#define __MODMISFIT_H__

class ModMisfit
{

public:

  ModMisfit() : MinDist(-1),
                MDAcc(-1),
                MaxIter(-1),
                DAngSh(DefDAngSh),
                HW(DefHW),
                Extrap(true),
                MinDT(DefMinDT),
                Sigma0(DefSigma0),
                KBeta(DefKBeta),
                Vrb(none) { }

  void SetMaxIter(
      const int  maxiter );

  void SetDAngSh(
      const double  dangsh );

  void SetdTau(
      const REAL  dtau );
protected:

  static const REAL    DefMinDT;
  static const REAL    DefSigma0;
  static const double  DefDAngSh;

  int            NPhases, NSrc, MaxIter;
  REAL    MinDist, MDAcc,  MinDT, Sigma0, KBeta;
  double    DAngSh;
  bool        HW, Extrap;
  Source    **Src;
  List<REAL>   X, T1, T2;

  void ResetSrc();

};

#endif/*__MODMISFIT_H__*/

Code:
/* modmisfit.cpp */
#include "modmisfit.h"

const REAL    ModMisfit::DefMinDT  = 0.01;
const REAL    ModMisfit::DefSigma0 = 0.01;
const double  ModMisfit::DefDAngSh = 2;
const REAL    ModMisfit::DefKBeta  = 5;
const bool    ModMisfit::DefHW     = false;

void ModMisfit::SetdTau(
    const REAL  dtau )
{
    for ( int i = 0; i < NSrc; i++ )
    {
        Src[i]->SetdTau(dtau);
    }
}

// ***************************************************************************

void ModMisfit::SetIntegration(
    const Integration  intg )
{
    for ( int i = 0; i < NSrc; i++ )
    {
        Src[i]->SetIntegration(intg);
    }
}

etc

...that way you can use these functions from any .cpp file by including the header file, and avoid getting multiple definition errors.
# 3  
Old 01-25-2010
Oh yes, cannot define before the class as they are class members. But can define them before the member functions as you say.

I have the ifdef's to prevent modmisfit.h being multiply included. Has not included everything as it was quite long class.

Thanks for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Equivalence classes don't work

Hello: I can't get equivalence classes to work in globs or when passing them to tr. If I understood correctly, matches e, é, è, ê, etc. But when using them with utilities like tr they don't work. Here's an example found in the POSIX standard: I decided to create the aforementioned files in... (9 Replies)
Discussion started by: Cacializ
9 Replies

2. Programming

Java Abstract Classes

Can anyone tell me if this is correct when creating classes in Java? public abstract class Animal { public class Point { public int x,y; } public class Animal { protected Point loc; protected String name; protected Random rng; String... (3 Replies)
Discussion started by: totoro125
3 Replies

3. Programming

Set java classes path

I add some new java jar files in the old project, how can I edit the class path? How can I compile all the java classes by just type 'C':o (3 Replies)
Discussion started by: Hscript
3 Replies

4. Programming

OOP in FORTRAN derived classes

I am using an extended class to describe a point in 2-D The following is my program, after that the definition of the classes I am getting the following error gfortran gshapes.f08 gshapes_utest.f08 -o gshapes_utest.x gshapes_utest.f08:53.27: call ellips%set_ellipse (crnr1, crnr2) ... (0 Replies)
Discussion started by: kristinu
0 Replies

5. 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

6. Programming

Writing C++ program Arguments and Classes

I want to write a C++ program that uses a class to do some calculations. I pass arguments to the program, some of which are used to set up class members. A class function will then perform the necessary calculations. I am wondering how I should pass the arguments from the program to set the... (2 Replies)
Discussion started by: kristinu
2 Replies

7. 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

8. Programming

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? ////////////////////////////////////////////////////////////////////////// #ifndef... (3 Replies)
Discussion started by: kristinu
3 Replies

9. 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

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