Book Image

C++17 By Example

By : Stefan Björnander
Book Image

C++17 By Example

By: Stefan Björnander

Overview of this book

<p>C++ is a general-purpose programming language built with a bias towards embedded programming and systems programming. Over the years, C++ has evolved and is used to develop software for many different sectors. Given its versatility and robustness, C++is a wonderful language to start your coding journey with. This book covers exciting projects built in C++ that show how to implement the language in different scenarios. While developing these projects, you will not only learn the language constructs but also how you can use C++ to meet your software requirements.</p> <p>The book starts with a brief introduction to C++ language constructs where you will learn essential concepts that are required to understand the projects covered in the book. The first module will build a library management system that will teach you how to perform efficient file handling and use pointers in your software. To give you a taste of GUI programming, the next module will build graphical applications using Qt 5. You will then be introduced to game design in C++ and build two interesting games. The final module will teach you how C++ can be used to create a Domain Specific Language.</p> <p>After reading this book, you will have mastered core programming concepts in C++, and how to implement them effectively.</p>
Table of Contents (17 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

The main function


Finally, the main function is almost identical to the previous function.

Main.cpp:

#include <QApplication> 
#include <QMessageBox> 
#include <IOStream> 
using namespace std; 
 
#include "Action.h" 
#include "Error.h" 
#include "Scanner.h" 
#include "Parser.h" 
#include "Evaluator.h" 
#include "ViewerWidget.h" 
 
int main(int argc, char *argv[]) { 
  Scanner::init(); 
  QApplication application(argc, argv); 
 
  try { 
    QString path = "C:\Input.dsl"; 
 
    QFile file(path); 
    if (!file.open(QIODevice::ReadOnly)) { 
      error("Cannot open file "" + path + "" for reading."); 
    } 
 
    QString buffer(file.readAll()); 
    Scanner scanner(buffer); 

The only difference is that the parser generates a sequence of directives rather than actions, as well as a function map, which is sent to the evaluator that generates the final action list that is read and executed by the viewer that displays the graphical objects:

    QList<Directive> directiveList...