Evgenii Legotckoi
Sept. 15, 2017, 1:05 a.m.

C++ - Tutorial 009. Modularity

The key to managing this is to clearly define the interactions among those parts. The first and most important step is to distinguish between the interface to a part and its implementation. At the language level, C++ represents interfaces by declarations. A declaration specifies all that’s needed to use a function or a type. For example:

  1. double sqrt(double); // a square root function that takes arguments of type double and returns a variable of type double
  2.  
  3. // Declaring a class with all the necessary methods and class members
  4. class Vector
  5. {
  6. public:
  7. Vector(int s);
  8. double& operator[](int i);
  9. int size();
  10. private:
  11. double elem;
  12. int sz;
  13. };

The key point here is that the implementation of all methods and functions can be in a completely different place, in another file. Often, functions are declared in header files with the extension .h , and the implementation is performed in the source code files .cpp .

For example, the implementation of the square root function:

  1. double sqrt(double d) // definition of sqrt()
  2. {
  3. // ... algorithm as found in math textbook ...
  4. }

Also, for the Vector class, several participants will need to be implemented:

  1. Vector::Vector(int s) // definition of the constructor
  2. : elem{new double[s]}, sz{s} // initialization of participants
  3. {
  4. }
  5.  
  6. double& Vector::operator[](int i) // definition of an access operator
  7. {
  8. return elem[i];
  9. }
  10. int Vector::size() // definition method size ()
  11. {
  12. return sz;
  13. }

We must define Vector’s functions, but not sqrt() because it is part of the standard library. However, that makes no real difference: a library is simply some ‘‘other code we happen to use’’ written with the same language facilities as we use.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html