Saturday, April 19, 2025

C++ OO Run External Apps


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void callCalc() {
            system("calc");
        }
};

int main() {

    External Object;
    Object.callCalc();

    return 0;
}

This C++ code is demonstrating how to use the system() function to execute an external program (in this case, the Windows calculator program). The code defines a class named External with a single public method callCalc() that calls the system() function with the argument "calc", which opens the Windows calculator program.

In the main() function, an object of the External class is created and the callCalc() method is called on this object, which results in the Windows calculator program being opened.

But it's important to note that using the system() function to execute external programs can be a security risk and should be used with caution, so here it's used only as an simple example to demonstrate class blueprint and object creation.


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void callCalc() {
            system("calc");
        }
};

int main() {

    External Object;
    Object.callCalc();
    Object.callCalc();
    Object.callCalc();

    return 0;
}

In the main() function, the callCalc() method is called three times on the Object instance, which will open the calculator application three times. Finally, the program returns 0 to indicate successful execution. 


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void caller() {
            system("calc");
            system("ping google.com");
        }
};

int main() {

    External Object;
    Object.caller();

    return 0;
}

This code defines a class called "External" that has a single public method called "caller". Inside the "caller" method, two system commands are executed: "calc" and "ping google.com".

In the "main" function, an object of the "External" class is created called "Object", and the "caller" method of this object is called. This means that the system commands "calc" and "ping google.com" will be executed in order. 


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void caller() {
            system("cmd");
        }
};

int main() {

    External Object;
    Object.caller();

    return 0;
}

This code is useful in situations where the user needs to execute a specific command in the Windows command prompt, which is accessed by running the "cmd" command.

By calling the "system" function with the "cmd" argument, the program will launch a new instance of the command prompt, giving the user direct access to the command line interface for executing a variety of commands, such as navigating through the file system, running other programs, or executing scripts.

This can be particularly useful in cases where the user needs to perform some advanced system administration tasks, or in situations where the program needs to execute specific command line operations that are not available through other APIs or libraries. 

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...