Search code examples
c++classwxwidgets

Wxwidgets || Add class and methods


I need to rebuild this code by adding a class with methods. How do I add a class if Zad2frame is already a class? The first class is supposed to handle addition of complex numbers, the second subtraction etc. I have all the code in an external file, Zespolone.h(necessary).

#ifndef ZESPOLONE_H_INCLUDED
#define ZESPOLONE_H_INCLUDED

#endif // ZESPOLONE_H_INCLUDED

struct Zespolona
{
    double rzeczywista;
    double urojona;
};

void Zad2Frame::OnButton1Click(wxCommandEvent& event)
{
    Zespolona przycisk1_1;
    Zespolona przycisk1_2;
    int wynik1;
    int wynik2;
    char* znak;
    wxString wynik;

    if(TextCtrl1->GetValue().ToDouble(&przycisk1_1.rzeczywista) && TextCtrl2->GetValue().ToDouble(&przycisk1_1.urojona)
            && TextCtrl3->GetValue().ToDouble(&przycisk1_2.rzeczywista) && TextCtrl4->GetValue().ToDouble(&przycisk1_2.urojona))
    {
        wynik1 = (przycisk1_1.rzeczywista + przycisk1_2.rzeczywista);
        wynik2 = (przycisk1_1.urojona + przycisk1_2.urojona);
        if(wynik2>=0)
        {
            znak="+";
        }
        wynik << wynik1<<znak<<wynik2<<"i";
        TextCtrl5->SetValue(wynik);
    }
}

void Zad2Frame::OnButton2Click(wxCommandEvent& event)
{
    Zespolona przycisk1_1;
    Zespolona przycisk1_2;
    int wynik1;
    int wynik2;
    char* znak;
    wxString wynik;

    if(TextCtrl1->GetValue().ToDouble(&przycisk1_1.rzeczywista) && TextCtrl2->GetValue().ToDouble(&przycisk1_1.urojona)
            && TextCtrl3->GetValue().ToDouble(&przycisk1_2.rzeczywista) && TextCtrl4->GetValue().ToDouble(&przycisk1_2.urojona))
    {
        wynik1 = (przycisk1_1.rzeczywista - przycisk1_2.rzeczywista);
        wynik2 = (przycisk1_1.urojona + przycisk1_2.urojona);
        if(wynik2>=0)
        {
            znak="+";
        }
        wynik << wynik1<<znak<<wynik2<<"i";
        TextCtrl5->SetValue(wynik);
    }
}

void Zad2Frame::OnButton3Click(wxCommandEvent& event)
{
    Zespolona przycisk1_1;
    Zespolona przycisk1_2;
    int wynik1;
    int wynik2;
    char* znak;
    wxString wynik;

    if(TextCtrl1->GetValue().ToDouble(&przycisk1_1.rzeczywista) && TextCtrl2->GetValue().ToDouble(&przycisk1_1.urojona)
            && TextCtrl3->GetValue().ToDouble(&przycisk1_2.rzeczywista) && TextCtrl4->GetValue().ToDouble(&przycisk1_2.urojona))
    {
        wynik1 = (przycisk1_1.rzeczywista * przycisk1_2.rzeczywista);
        wynik2 = (przycisk1_1.urojona * przycisk1_2.urojona);
        if(wynik2>=0)
        {
            znak="+";
        }
        wynik << wynik1<<znak<<wynik2<<"i";
        TextCtrl5->SetValue(wynik);
    }
}

void Zad2Frame::OnButton4Click(wxCommandEvent& event)
{
    Zespolona przycisk1_1;
    Zespolona przycisk1_2;
    int wynik1;
    int wynik2;
    char* znak;
    wxString wynik;

    if(TextCtrl1->GetValue().ToDouble(&przycisk1_1.rzeczywista) && TextCtrl2->GetValue().ToDouble(&przycisk1_1.urojona)
            && TextCtrl3->GetValue().ToDouble(&przycisk1_2.rzeczywista) && TextCtrl4->GetValue().ToDouble(&przycisk1_2.urojona))
    {
        wynik1 = (przycisk1_1.rzeczywista / przycisk1_2.rzeczywista);
        wynik2 = (przycisk1_1.urojona / przycisk1_2.urojona);
        if(wynik2>=0)
        {
            znak="+";
        }
        wynik << wynik1<<znak<<wynik2<<"i";
        TextCtrl5->SetValue(wynik);
    }
}

I tried 3 ways to do this assignment, every time I had a class error. How do I rebuild my code so that the external class handles individual events (Not zad2frame)?

Please help, if I don't do this task, I can't go on to the next ones.


Solution

  • With C++ each class is usually stored in two files. The header file containing the declaration of the class. For example: MyNewClass.h. And a cpp file containing the implementation of the class. For example: MyNewClass.cpp.

    So in this case to use MyNewClass you would have the line #include "MyNewClass.h" near the top of the program. Once included that file can use MyNewClass wherever needed.