Help me understand C ++ header files and classes

OK, so I'm trying to switch from intermediate Delphi to object-oriented programming in C ++. I read the Ivar Horton book in Visual C ++ 2010. I can get rid of simple console applications without problems. I get the language itself (kinda). Where I struggle with headings and classes.

I also understand that header files and classes in general. What I don't get is an implementation when I use a header or class? Do I need to create classes for everything I do? Can my actual working functions be in header files or in CPP files? I get lost in the correct use of this data and can use some real recommendations from more experienced programmers.

I am trying to switch to Windows applications using MFC if this is useful.

+3
source share
6 answers

I also understand what header files and classes in general. That I cannot get an implementation when to use a header or class? Do I have to create classes for everything I do? Do I need my actual work functions? be in header files or in CPP files? I am lost in the correct use of these and may use some kind of guidance in the real world from more experienced programmers.

Repeat for several years, and I would have the same question - this is not something that is very well covered, I don’t think. So let me break the part that I see as the meat of your problem.

Do I need to create classes for everything I do?

, , . , , . , , C, int char. String STL ( ). , , , , , , , - -.

, .

, . , C , , C++ , ( , ). :

#include <iostream>
using namespace std;

int testadd()
{
    int a = 4; int b = 2; int c = add(a, b);
    return c;
}

int add(int x, int y)
{
    return x + y;
}

int main(int argc, char** argv)
{
    cout << "result is: " << c << endl;
    return 0;
}

( ) , testadd add, main add.

, , , . - , , , , ( ), . .

, , :

  • ( ) cpp.
  • .

, . 1, , , , , .

, , 1, , main.cpp, myclass.h, myclass.cpp myclass, myclass.cpp main.cpp, , , (*.cpp) (main.obj myclass.obj vc). , exe, ), , , . , exe. , . ( ), .

+3

++ Delphi; , ( ) ( ). , .

++ , Delphi; , .

+2

, .

, . , . , .

"" "" ... .

"" - ++.

"header" - , ++.

, "" , , " ", .cpp. , 1:1 1: . .cpp , .cpp .

, , , "" , ( , class {...};). , , .

( " ", ) , , " ", . , , .

, , - , / , . - . , (, , , , , ), ). - , , , ( / ).

, " ", - , , , , , ( -, f(), ).

, . , , , , .

+2

. . , . .

, . ++.

+1

.. . . . :
Java - Java, . ++ . , . , , "". . , .

0

Source: https://habr.com/ru/post/1782702/


All Articles