MVC in C++

Jacob Galam
2 min readMay 25, 2021

--

When I was searching for a way to implement MCV in C++ I did not find any ways online to do this. I write this article to show how I did it.

What is MVC?

By wikipedia:

Model–view–controller (usually known as MVC) is a software design pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.

Model

The central component of the pattern. It is the application’s dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.

View

Any representation of information such as a chart, diagram or table. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.

Controller

Accepts input and converts it to commands for the model or view.

In addition to dividing the application into these components, the model–view–controller design defines the interactions between them.

The model is responsible for managing the data of the application. It receives user input from the controller.

The view renders presentation of the model in a particular format.

The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.

As with other software patterns, MVC expresses the “core of the solution” to a problem while allowing it to be adapted for each system. Particular MVC designs can vary significantly from the traditional description here.

Basically Model is the pure logic of the app and the back-end and save the state of the app, View is the IO, it send and get data to and from the Controller; and the Controller is the connecting between the View and the Model.

This is the UML example for generic “X”

I also have example “app” that use MVC in C++. It is very simple console app that just shows the user the number of times that he enter the line “add” by entering the line “show”.

This is how I initialize the MVC in main:

Model:

ViewListener.h:

View:

Controller:

You can see the app code here:

--

--