Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[newbie] new class



  • Hello,

    (Excuse my ignorance, I'm trying to get familiar with Qt. I'm coming from C, javascript, vbscript, php and mysql.)

    I created a new class to manage the app settings.
    http://s31.postimg.org/6ybnezvmj/s20160630233711.png

    Now, I'm not sure how to put methods on it and how to call these methods from another class.
    All I need is a basic example, please!
    I found this video on youtube but it only shows how to add a new class (what I just did) but It doesn't show how to create a method and call it from another class (say mainwindow.cpp).



  • Fyi: This is C++ question, not Qt. And i think you have to learn C++ first.
    Anyway, just include the class's header file (settings.h) from mainwindow.cpp, for example:

    #include "settings.h"
    

    Then you will be able to create your class's object and you will be able to use its methods. For example:

    settings newObject; //Create object from settings class
    newObject.methodFoo(); // Call class's method named methodFoo();
    

    The complete version:

    //settings.h content
    class settings //Class definition and declaration
    {
    	public:
    		settings(); // I suppose that you've already defined settings() function in settings.cpp, as far as I can see from pictures
    
    		void methodFoo() //Method definition and declaration
    		{
    			// Do somethings
    		}
    };
    
    
    //mainwindow.cpp file content
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "settings.h"
    
    MainWindow::MainWindow(QWidget *parent) :
    	QMainWindow(parent),
    	ui(new Ui::MainWindow)
    {
    	ui->setupUi(this);
    	settings newObject; //Create object from settings class
    	newObject.methodFoo(); // Call class's method named methodFoo();, but first you have to declare this method
    }
    
    MainWindow::~MainWindow()
    {
    	delete ui;
    }
    


  • Thanks MrKozmon.
    Exactly what I needed!
    I'm looking for C++ tutorials, please recommend me one!
    So, settings.cpp won't be used and I can delete it and keep only settings.h?


  • Lifetime Qt Champion

    You should keep settings.cpp as you, for sure, will add more functionality to it and it is not recommended to have everything in the header file. Usually header file only contains declarations and .cpp file contains definitions (like methods implementations).



  • Thats where I'm getting confused. Which file include in which?
    All tutorials on C++ classes and headers I've seen so far explain the class itself but none of them show how to interact among them. What to #include where.
    They show how to build a struct/class but they don't say how to connect (#include) them.



  • Ok I just watched a video tutorial that explained it.
    So, when I create myClass.h and #include that file into main.cpp it will automatically look for myClass.cpp (?)

    I though I had to #include "myClass.cpp" in myClass.h but seems like the compile will always look for a .cpp for every .h then. I guess I got this right.

    Excuse me for asking such an off-topic question not regarding Qt.


  • Moderators

    Excuse me for asking such an off-topic question not regarding Qt.

    No problem. We're here to help. There's a C++ Gurus section on the forum if you want to ask more non-Qt related c++ questions.

    @LeandroA said:

    I guess I got this right.

    Not really :)
    To simplify - you declare a class in .h file:

    //someclass.h
    
    class SomeClass
    {
    public:
       void someMethod();
    };
    

    Then you define methods of that class in a .cpp file. The code here needs to know what it defines so you include the header:

    //someclass.cpp
    
    #include "someclass.h"
    void SomeClass::someMethod() {  /* do stuff */ }
    

    There are cases when you want to put the definition directly in the header (for templates and inlining), but that's the general way to do it.

    And now you use that class somewhere in your other files. To use it you only need the declaration so you include the header:

    //whatever.cpp
    #include "someclass.h"
    
    void whateverFunc()
    {
       SomeClass someClassInstance; //create an instance of that class
       someClassInstance.someMethod(); //call a method on it
    }
    

    As to what happens - the compiler compiles .cpp files and creates an object file (.obj on Windows) for each one. The headers are not compiled directly, but as part of .cpp compilation (#include statement means just "copy/paste the contents of that file here"). So wherever you need to access methods of a class or create its instance you need to include its header.



  • @LeandroA C/C++ is a little bit hard language to understand than other languages. Because it's very close to hardware, therefore there are different concepts like compiling, linking, declaration and defination. If you want to write a C++ code, first you need to compile your source code with all the declarations (class declarations, function declarations etc.). Then you need to link your compiled "object" code to other compiled definations' object codes. To see the big picture first you need to understand "defination" and "declaration" concepts. For example you've wrote a squaring function.

    // getsquare.cpp
    int getSquare(int number)
    {
        return (number*number);
    }
    

    This was a function defination, so you've wrote what exactly function is doing. Now, e.g. you want to use this function in another code:

    // main.cpp
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << getSquare(5);
        return 0;
    }
    

    Then e.g. you want to compile this code and use the program. But you can't compile your main program's code, because when compiler attempted to compile main.cpp, compiler says that "How can i convert it to machine code (010111010110) because I don't even know what's the getSquare() function is doing". Because the compilers just converters between C++ code and machine(001111101010) code. So, you need to "#include" some instructions about getSquare() function to make compiler informed before compiling. You can do the following for this purpose:

    // main.cpp
    #include "getsquare.cpp"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << getSquare(5);
        return 0;
    }
    

    So, the include word is just compiler directive, and say that to compiler "please add this cpp file to my cpp file's top while the compilation (just like copy-paste)" Hence, compiler will work. Because it's now seeing the actual code (while the compilation), like this:

    // main.cpp
    int getSquare(int number) //Function defination
    {
        return (number*number);
    }
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << getSquare(5);
        return 0;
    }
    

    By the way, include with "blahblah.h" says that to compiler, blahblah.h file is in the same dir with main.cpp code, so compiler will be find the included file easily. And include with <blahblah.h> says that to compiler, blahblah.h file is in the somewhere that in your default directories (e.g. C:\VisualC++\include).

    But you don't have to give all the details about your getSquare function to compiler. For example this is enough (just for compilation).

    // main.cpp
    int getSquare(int number); //Function declaration
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << getSquare(5);
        return 0;
    }
    

    So declaration is enough to compiler if you just compile the "your" source code, for example with "-c" option in the GCC(Gnu C/C++ Compiler). Because compiler know those function name, function return type, function arguments, funtcion arguments' types.
    And those are enough for if you want to "just" compile "your" main.cpp code. (i will explain this more) Compiler know all this things just with declaration:

    int getSquare(int number); //Function declaration
    

    For example if you use the GCC and if you passed the "-c" option while the compilation like following, Compiler will ignore that "what exactly getSquare is doing" (defination). Because it will think that, this information will be linked after "linking" process.

    gcc -c main.cpp
    

    So, C++ programs are beeing after two process, Compiling and Linking. You can choose to give all the informations about your included functions to compiler at a time while compilation, without linking process, this is fine, but this have some disadvantages (i will explain later). So usually programmers choose the make a "header"(*.h) file and just add the declatations on it. Then compile both of main.cpp and getsquare.cpp separately to seperate "object" files, then link this files to one "executable" program. Like this.

    //getsquare.h
    int getSquare(int number); //Function declaration
    
    // getsquare.cpp
    int getSquare(int number) //Function defination
    {
        return (number*number);
    }
    

    Compiling getsquare.cpp to getsquare.o object file.

    gcc -c getsquare.cpp -o getsquare.o
    

    And there is a compiled getsquare.o object file we have.

    // main.cpp
    #include "getsquare.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << getSquare(5);
        return 0;
    }
    

    Compiling main.cpp to main.o object file.

    gcc -c main.cpp -o main.o
    

    Compiler has ignored the actual code of getSquare function for temporarily. it has contented just with declaration for now. Because it will be linked after linking process. So now, there is a compiled getsquare.o object file and main.o object file we have. Let's link.

    gcc main.o getsquare.o -o myprogram.exe
    

    So now, gcc has got the actual machine code of getSquare while the linking process (with getsquare.o)

    The advantage of this method is that, you can easily sell your library without sharing your actualy codes. E.g. think that, you want to sell this squaring function as a math library. But you don't want to show your "works". You can just give your buyer the following files:

    getsquare.h and getsqure.o

    and your buyer will be able to include your function to the its own code, and your buyer will be able to link your getsqure.o machine code to it's program without seeing your actual codes. This is just an advantage.

    And this is valid for class definations and declarations.

    Finally, i think obtain a C++ book yourself, C++ is very powerful language, i suggest you: Object-Oriented Programming in C++ by Robert Lafore



  • @MrKozmon
    By the way, include with "blahblah.h" says that to compiler, blahblah.h file is in the same dir with main.cpp code, so compiler will be find the included file easily. And include with <blahblah.h> says that to compiler, blahblah.h file is in the somewhere that in your default directories (e.g. C:\VisualC++\include).

    Thanks! Those details are clear to me now. In php I also handle 'classess' and 'includes'. But there is no .h files in php. Hence my question. I know object orientation but mostly in php and javascript.
    This .h file is a new concept to me.
    I'm looking into getting the book you recommended.
    Your answer came really handy! helped a lot!
    Thanks!



  • @LeandroA You're welcome


Log in to reply