0

There is a theme - Separating class code into a header and cpp file

It describes how to separate a class with variables and methods to .h and .cpp

But it's a simple one.

Say I have this in main.cpp

int main() {
    class Filth {
        int a, b;
        void pra() { std::cout << a; }
        class Frank {

            int sacrifices;

            void praisChinChin() { std::cout << "DARK LORD IS COMMINGGGGGG"; }
        }
    };
}

And how do I write THIS class (Filth) into a .h and .cpp so I dont get "undefined reference" and any other mistake?

And how exactly does it work (why I should write this exact code, what exactly does it do to my program)?

sehe
  • 374,641
  • 47
  • 450
  • 633

2 Answers2

1
  1. frank.cpp

    #include "frank.h"
    #include <iostream>
    
    void Frank::praisChinChin() {
        std::cout << "DARK LORD IS COMMINGGGGGG"; 
    }
    
  2. frank.h

    #pragma once
    
    class Frank {
        int sacrifices = 0;
      public:
        void praisChinChin();
    };
    
  3. filth.cpp

    #include "filth.h"
    #include <iostream>
    
    void Filth::pra() {
        std::cout << a; 
    }
    
  4. filth.h

    #pragma once
    
    class Filth {
        int a = 0;
        int b = 0;
        void pra();
    };
    
  5. test.cpp

    #include "frank.h"
    
    int main() {
        Frank f;
        f.praisChinChin();
    }
    
sehe
  • 374,641
  • 47
  • 450
  • 633
  • So, the classes are separated, and we have a Frank class and a Filth class, in condition of Frank{} Flith{} , but not Flank{Filth{}}? – Michael.Medvedskiy Sep 16 '17 at 22:51
  • Well, actually that was a part [I half overlooked](https://stackoverflow.com/questions/46257804/separating-class-code-that-has-another-class-in-it-into-a-header-and-cpp-file/46258065?noredirect=1#comment79479719_46258072). From the code you gave there's absolutely no reason why `Filth` needs to be nested inside `Frank`, so you'd normally not do that. Otherwise, you can simply stick with: http://paste.ubuntu.com/25551674/ – sehe Sep 16 '17 at 23:00
  • Real thanks. That is useful, about the methods - every time i write the definition in .cpp , i should be doing void Class::Class2......::Methodname(){}? Or is there a way to get the code look like I created it in main.cpp (like in example - method in class, that is in another class, structured, no :: :: :: :: :: every time i describe (write the actual code) of a method that is on some depth (in-classed) level)? Real thank you again. – Michael.Medvedskiy Sep 16 '17 at 23:24
  • Yes. Though you can - using discretion - define them inline: http://paste.ubuntu.com/25551807/ -- your [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will have all the details – sehe Sep 16 '17 at 23:27
  • So what you are doing in this example is you just copypaste code from main.cpp to .h, and it works just fine? – Michael.Medvedskiy Sep 17 '17 at 00:54
  • No. It only works for inline definitions. Like I said, a c++ book is going to be indispensable – sehe Sep 17 '17 at 09:34
1

You are missing a semi colon at the end of class Frank. It should compile after that.

To separate the class into .h and .cpp file you should make you function non local to the main function.

Header file might look like this.

class Filth
{
    int a, b;
    void pra();

    class Frank 
    {
        int sacrifices;
        void praisChinChin();
    };
};

And the cpp file

void Filth::pra()
{
    std::cout << a;
}

void Filth::Frank::praisChinChin()
{
    std::cout << "DARK LORD IS COMMINGGGGGG";
}

int main()
{
    return 0;
}

I'm not sure about the "why should I write the exact code". But at the moment your code is not really doing anything. You need to create objects of your classes and call member functions, for it to have any real effect.

jumper0x08
  • 131
  • 5
  • You're missing quite a bit of critical mechanics. Also "Should look like"? "Could look like" is a bit more to the point – sehe Sep 16 '17 at 20:15
  • @sehe I agree. Edited. Thanks – jumper0x08 Sep 16 '17 at 20:18
  • Still - not showing include guards or directives, not making anything public, not initializing non-static data members etc :) Kudos for getting the nested type right (I misread that) – sehe Sep 16 '17 at 20:20
  • All of us were guilty of that at some point. My intention was to explain class declarations and definitions without changing too much of the original code. – jumper0x08 Sep 17 '17 at 06:53