0

I have a very poor understanding of inheritance and I can't seem to find the answer I'm looking for. I apologize for the length of this post but I feel I need to be very specific.

I have a base class "ProgCtl" which can send signals to processes. I have a simple program called "Beeper" which just runs forever, giving a "beep!" message every 10 seconds until interrupted. The derived class which I'm tasked with making, "BeepCtrl", is meant to specifically send signals and make changes to the beeper program.

My problem is: I can't figure out how to access the methods and variables of ProgCtl from the methods in BeepCtrl. Mainly in BeepCtrl.cpp where I have to use ProgCtl's alive and send method's to check if the process exists and to end it.

are my beep constructor's ProgCtl extension even doing anything? My notes only say that this allows me to set the attribute values. I'm really sorry. I know it's bad etiquette to try to get more than one question (and broad ones) on here but I'm truly lost.

ProgCtl.hpp

#ifndef  PROGCTL_HPP
#define PROGCTL_HPP

#include <stdlib.h>
#include <unistd.h>
#include <string>

class ProgCtl {
    private:
      std::string   m_program;
      pid_t         m_pid;

      void          find_pid();

    public:
      // Constructors
      ProgCtl():m_program(""),m_pid(-1){}
      ProgCtl(std::string prog):m_program(prog),m_pid(-1){}
      ProgCtl(char *prog):m_pid(-1) {
          m_program = std::string(prog);
      }
      // Setters
      void       setName(std::string prog);
      void       setName(char *prog);
      void       setPid(pid_t pid){m_pid = pid;}
      // Other
      bool       alive();
      int        send(int sig);
      int        start();
};

#endif

BeepCtrl.hpp; far from complete due to this fundamental issue

#ifndef BeepCtrl_HPP
#define BeepCtrl_HPP
#include "ProgCtl.hpp"
#include <string>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <iostream>
class BeepCtrl: public ProgCtl{

public:
    BeepCtrl(std::string name):ProgCtl(name){};
    BeepCtrl():ProgCtl(){};
    BeepCtrl(char *prog):ProgCtl(prog){};
    void stop(); //if alive, send kill
    void beep_faster(); //test if beeper is alive then decrement inveral
    void beep_slower(); // increment interval
};
#endif

BeepCtrl.cpp

#include "BeepCtrl.hpp"

void stop() //if alive, send kill
{
    //std::set_terminate(std::cerr << "terminate handler called\n");
    if(alive()){//---alive and send not declared in this scope---
        send(9);
    }
    else{
    throw 0;
    }
}
void beep_faster() //test if beeper is alive then decrement inveral
{
//throw if an invalid value
}
void beep_slower() // increment interval
{
//throw if an invalid value
}
jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

2

You should specify class for your methods in cpp file:

void BeepCtrl::stop() //if alive, send kill
{
    //std::set_terminate(std::cerr << "terminate handler called\n");
    if(alive()){//---alive and send not declared in this scope---
        send(9);
    }
    else{
    throw 0;
    }
}
Sergi0
  • 1,084
  • 14
  • 28
0

EDIT This answers I can't figure out how to access the methods and variables of ProgCtl from the methods in BeepCtrl. The actual compilation issue was solved by @Sergi0.


Members of the base class that should be accessible by the derived class should be declared protected and not private:

class ProgCtl {
    protected:
      std::string   m_program;
      pid_t         m_pid;

      void          find_pid();

    public:
      // Constructors
      ...
};

The protected names are still not accessible from outside, but the child class has access to them.

A similar thing happens, when you inherit a class:

class BeepCtrl: public ProgCtl { ... }

Here, the access to public names of ProgCtl via an object of type BeepCtrl will remain unchanged. Protected names will still be protected.

class BeepCtrl: protected ProgCtl { ... }

Here, the access to public names of ProgCtl via an object of type BeepCtrl is only possible from functions within BeepCtrl.

class BeepCtrl: private ProgCtl { ... }

Here, any name of ProgCtl is hidden from places within BeepCtrl and outside.

user1978011
  • 3,419
  • 25
  • 38