0

I'm trying to build a class that inherits from another class and initializes itself by parsing a text file. So I have:

On my header file (both in same file):

class Mother{
    public:
        Mother();
        Mother(std::string path);
        int parse_textfile(std::string path);
   protected:
        std::string path;};

class Daughter : public Mother {
    public:
       Daughter();
       Daughter(path);
       Daughter(std::string TITLE);
    protected:
        std::string path
        std::string TITLE

Now, on my cpp file:

Mother::Mother(){};
Mother::Mother(string path)
:path(path)
{
    parse_textfile(path);
};

Mother::parse_textfile(string path)
{
    cout << "Processing file: " << path << endl;
    ifstream file(path.c_str());
    if (! file){
        cerr << "unable to open input file: " << grid_path << " --bailing out! \n";
    return -1;
}else{
    string TITLE;
    getline(file, TITLE);
    return Daughter(TITLE)

Now, I understand that this code is not correct (obviously, as it won't compile without errors), but I am still so raw with C++ that some expert advice would really come a long way at this point.

The objective is to create a Daughter class that inherits the parsers, because the Daughter class itself should parse the textfile and hold all the variables in it. I will do line by line parsing because there is a lot of information in the text file. I just want this information to be held on the Daughter object so I can operate over this information later on.

Also, I'm using C++ mainly because I don't want to share the source code. If there is a more appropriate language, I would also like to hear it from you. This would be trivial for me in Python, but again, I don't want to share what's happening.

EDIT 1:

Based on what I got from the comments, I can start seeing more clearly what the specific question should be.

I have 3 text files that belong to the same problem, but each of this text files contain different information.

What I want is to get recommendations on how to initialize each of this text files as individual objects that are derived from the same "Mother class". Maybe it makes more sense that the parser actually belongs to each of the Daughters instead of the Mother. Either way, for those whose comments have been geared towards actually answering the question and not asking me "why I want to do this", I appreciate your help and recommendations.

Reniel Calzada
  • 95
  • 1
  • 11
  • 1
    If `Daughter` should do the parsing why does `Mother` have `parse_textfile` and not `Daughter`? Also, do you even need inheritance here? Looks like something a single class can handle. – NathanOliver Aug 30 '17 at 14:56
  • what is wrong about sharing sources? Btw you just did it :P – 463035818_is_not_an_ai Aug 30 '17 at 14:57
  • 1
    By the way, just because you send someone a binary executable does not mean they cannot find out what your program is doing. (Reverse engineering code is not magic) – UnholySheep Aug 30 '17 at 14:59
  • I haven't, because the parser is not the meat of the product. We have all the parsers we need in Python. – Reniel Calzada Aug 30 '17 at 15:00
  • @NathanOliver The idea is that there are more files to parse, and they all come together trough the same mother class, so I want te be able to parse them all, and operate though Mother, which BTW Mother is intended to be yet a daughter of someone else. – Reniel Calzada Aug 30 '17 at 15:02
  • @UnholySheep I don't mind if they make their own open source version. I not getting paid to do this, so I'm doing it for my own benefit, but not to give it away entirely without regards for my own well being. – Reniel Calzada Aug 30 '17 at 15:04
  • What do you want? Mother or Daughter each initialized with an explicit dedicated file? Or parse a file and get a Mother or Daughter depending on what's inside the file? – Jean-Baptiste Yunès Aug 30 '17 at 15:07
  • 1
    Please take a look at [this list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and use one or more items. – n. m. could be an AI Aug 30 '17 at 15:08
  • @NathanOliver I was just thinking of initialization, where one could initialize a Mother and then use a string to find the text file and return the correct Daughter object (there are 3 different Daughters), or if each parser should just be only in their corresponding Daughters, which I guess makes much more sense. But that was the point of asking. I'm a total newbie in C++ so I have no idea what I'm doing. I'm just trying to learn. – Reniel Calzada Aug 30 '17 at 15:08
  • @Jean-BaptisteYunès I want to be able to initialize using either object, but return the Daighter that corresponds to the file I'm parsing. But be able to initialize an empty Mother too. Now that you guys are posting so many comments, it is starting to make itself a little bit more evident to me that proabably the parser should belong to the daughter. I don't know. I'm a bit lost. One Mother class, 3 different text files that relate to the Mother. They all have different meanings. Thoughts? – Reniel Calzada Aug 30 '17 at 15:11
  • I think the problem is that your spec is not clear and we can't catch what you want. "return the Daughter that corresponds to the file I'm parsing. But be able to initialize an empty Mother too" what does this means ? – Jean-Baptiste Yunès Aug 30 '17 at 15:13
  • 1
    @Jean-BaptisteYunès It means that I don't know yet what I'm doing. I will edit with a more specific question, but your comments are helping me see this more clearly. – Reniel Calzada Aug 30 '17 at 15:15
  • "this would be trivial for me in Python" Show your Python implementation. It would be a lot more clear what you are actually trying to do. – n. m. could be an AI Aug 30 '17 at 15:16

1 Answers1

0

Your issue is not about C++ itself, it's about software architecture & design. Could you try to extract "parser" functionality into separate class (let name it "factory"). Given that, you have someone who decides which instance could be created - Mother, Daughter, whatever. And you have a class hierarchy Mother-Daughter-etc which can share necessary common code and override some specific behaviors if needed.

I understand this can be named "overkill" but I'd recommend you so called "Gang of four" book named Design patterns. Here it seems to be public version of the PDF: https://github.com/dieforfree/edsebooks/blob/master/ebooks/Design%20Patterns%2C%20Elements%20of%20Reusable%20Object-Oriented%20Software.pdf

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
  • Yes, I think I am getting now closer to the actual problem. I will read this material. – Reniel Calzada Aug 30 '17 at 15:15
  • Accepted this answer because you were able to identify the main problem, and provide specific text book references that addresses directly the gaps in my knowledge. Definitely not an overkill, but a must read in my case. Thanks!! – Reniel Calzada Aug 30 '17 at 15:37