Writing C++ class and member functions


 
Thread Tools Search this Thread
Top Forums Programming Writing C++ class and member functions
# 1  
Old 05-08-2012
Writing C++ class and member functions

I have the following class and thought that when I call the set command to set a member, I always use value. Would that be fine?

Code:

class ModMisfit {

protected:

  Real    dtau;
  Real    mdacc;
  Real    mindist;
  bool    hw;
  Source**    src;

public:

  void  set_integration(
    const Integration  value);

  void  set_dtau(
    const Real  value);

  void  set_mdacc(
    const Real  value);

  void  set_mindist(
    const Real  value);

  void  set_hw(
    const bool  value);

};

void ModMisfit::set_dtau(
  const Real  value) {

  for ( int i = 0; i < nsrcs; i++ ) {
      src[i]->SetdTau( value );
  }

}

inline void ModMisfit::set_mdacc(
  const Real  value ) {

  mdacc = value;

}

inline void ModMisfit::set_mindist(
  const Real  value ) {

  mindist = value;

}

void ModMisfit::set_integration(
  const Integration  value ) {

  for ( int i = 0; i < nsrcs; i++ ) {
      Src[i]->SetIntegration( value );
  }

}


Last edited by kristinu; 05-08-2012 at 07:31 PM..
# 2  
Old 05-08-2012
Could you rephrase the question? I don't think that quite came across.

Which set are you talking about anyway? The code is full of them.
# 3  
Old 05-08-2012
I made the code a bit simpler. Hope it helps. I have defined members

Code:
dtau, mdacc, mindist, hw, ...


When I create the member functions, I call the input argument the same name, I call value. Originally I had something as shown below:

Do you think my new code would be better as I have lot of names that are the same. How does one usually deal with such instances?

Code:
class ModMisfit {

protected:

  int     nsrcs;
  Real    Dtau;
  Real    Mdacc;
  Real    Mindist;
  bool    Hw;
  Source**    Src;

public:

  void  set_dtau(
    const Real  dtau);

  void  set_mdacc(
    const Real  mdacc);

  void  set_mindist(
    const Real  mindist);

  void  set_hw(
    const bool  hw);

};

void ModMisfit::set_dtau(
  const Real  dtau) {

  for ( int i = 0; i < nsrcs; i++ ) {
      Src[i]->SetdTau( dtau );
  }

}

inline void ModMisfit::set_mdacc(
  const Real  mdacc ) {

  Mdacc = mdacc;

}

inline void ModMisfit::set_mindist(
  const Real  mindist ) {

  Mindist = mindist;

}

void ModMisfit::set_integration(
  const Integration  intg ) {

  for ( int i = 0; i < nsrcs; i++ ) {
      Src[i]->SetIntegration( intg );
  }

}

# 4  
Old 05-08-2012
Quote:
Originally Posted by kristinu
When I create the member functions, I call the input argument the same name, I call value.
They're not really the same value, some are capitalized, some aren't.

As long as you have a consistent system for telling which is which and stick to it, it should be maintainable.
# 5  
Old 05-08-2012
Looking for a naming convention that resolves the problem of finding reasonable variable names for setter methods and constructors.

---------- Post updated at 05:48 PM ---------- Previous update was at 05:44 PM ----------

Quote:
Originally Posted by Corona688
They're not really the same value, some are capitalized, some aren't.

As long as you have a consistent system for telling which is which and stick to it, it should be maintainable.
Yes, I capitalize member variables, but all inputs to the set functions use the variable name 'value'.

---------- Post updated at 07:39 PM ---------- Previous update was at 05:48 PM ----------

Can one use the same variable name for the class member and the arguments for the setter functions as shown below?

Code:
class ModMisfit {

// -- Attributes -----------------------------------------------------------------------------------

protected:

  static const Real    dflt_dtmin;
  static const Real    dflt_sigma;
  static const Real    dflt_kbeta;
  static const double  dflt_dangsh;
  static const bool    dflt_hw;
  // static const char    DelStr[];
  int     NPhases;
  int     nSources;
  double  dangsh;
  int     iterMax;
  Real    mdacc;
  Real    mindist;
  Real    dtmin;
  Real    sigmaz;
  Real    kbeta;
  bool    hw;
  bool    extrap;
  Source**     srcs;
  List<int>    ds;
  List<int>    dp;
  List<Real>   x;
  List<Real>   t1Lst;
  List<Real>   t2Lst;
  List<Phase>  ph;
  Verbose  vblevel;
  // Real data

// -- Operations -----------------------------------------------------------------------------------

public:

  ModMisfit():                         //
    mindist( -1 ),
    mdacc( -1 ),
    itermax( -1 ),
    dangsh( dflt_dangsh ),
    hw( dflt_hw ),
    extrap( true ),
    dtmin( dflt_dtmin ),
    sigmaz( dflt_sigma ),
    kbeta( dflt_kbeta ),
    vblevel( none ) { }

  void  set_param(                      //
    Parsing&  P );

  void  set_data(                       //
    Parsing&  P );

  void  set_integration(                //
    const Integration  intg );

  void  set_dangsh(                     //
    const double  dang );

  void  set_dtau(                       //
    const Real  dtau );

  void  set_mindist(                    //
    const Real  mindist );

  void  set_itermax(                    //
    const int  itermax );

  void  set_hw(                         //
    const bool  hw );

  Real  get_misfit(                     //
    Velmod*  vm );

  Real  get_pdf(                        //
    Velmod*  vm );

protected:

  void  ResetSrc();                    //

};

# 6  
Old 05-09-2012
You can call the functions, variables, and parameters whatever you want, as long as they don't overlap. They can't be all the same because, at best, some won't be available to you inside the function, at worst you'll get compiler errors in some contexts.

Beyond that I still have no idea what you're asking.
# 7  
Old 05-09-2012
I have a member function called mdacc (look at the green variable) , and in the setter function I am using mdacc for the input argument. And in the program, the user specifies mdacc as well, that then sets the class value for mdacc.

I used to have the class member starting with capital letter, whereas all the others are lower case. But left me with some confusion . What do people code in such cases?

Code:
class ModMisfit {

protected:

  Real    mdacc;

public:

  void  set_mdacc(
      const Real  mdacc);

};

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

2. Programming

Restricting member of a class non-inheritable in C++

There is base class B, and two derived classes D1 and D2 derived from Base. Base class B, have two data members ( public or protected or private or if any). D1 should inherit both these data members, and D2 should be deriving only one member from Base class. Is this kind of design possible without... (1 Reply)
Discussion started by: techmonk
1 Replies

3. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

4. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

5. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

6. Programming

static use for class inside the same class c++

Hi, I believe the next code is wrong: class Egg { Egg e; int i; Egg(int ii=0) : i(ii) {} }; because you would end up with an endless definition (memory allocation) of Egg objects, thus int i. Ok, so God Eckel proposes for a singleton: class Egg { static Egg e; int... (5 Replies)
Discussion started by: xavipoes
5 Replies

7. Programming

C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento) typedef struct elemento { char name; char value; List<struct elemento> ltElementos; ... (7 Replies)
Discussion started by: pogdorica
7 Replies

8. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

9. Programming

Handling a signal with a class member function

Hello, i am using the sigaction function to handle the SIGCHLD signal.Is it possible to use a class member function as the handler function (the sa_handler member of the sigaction structure)? The function's signature is: void (*sa_handler)(int);so i don't think i can use a static member function... (2 Replies)
Discussion started by: Zipi
2 Replies

10. Programming

writing functions

I have to write a program in C++ using several functions. The program consists of ticket sales how many if you are an adult, junior, or toddler and if there are any discounts. I have the program working up to the pricing function. when i put the discount in the equation it <strike>do</strike>... (1 Reply)
Discussion started by: ravenswind35
1 Replies
Login or Register to Ask a Question