poniedziałek, 29 maja 2017

[C++] Operators on binary numbers.

    int a = 123;
    a |= 1<<3; //set 1 on 3 bite
    a &= ~(1<<3); //set 0 on 3 bite
    if (a & (1<<3))//if 3 bite is 1?

niedziela, 30 kwietnia 2017

[C++]Static modifier

Local variables

Variables with static modifier are located in the same memory that global variables. Local static variables are not deleated. They remember their last values. Local static variables are initialized with 0 value.

#include <iostream>

int counter()
{
    static int a;
    a++;
    return a;
}

int main()
{
    std::cout<<counter()<<"\n"; // 1
    std::cout<<counter()<<"\n"; // 2
}

Static data members

Static data members of a class are created similarly like static local variables. Static data member is created earlier than first instance of this class. It doesn't matter from which object we can try to get acces to static data member, there is only one variable. Value is the same for all objects.

Also we can acces to static data member without any object. Static data member has to initialise like global variable.

#include <iostream>

class Foo
{
public:
    Foo ()
    {
        ++numberOfInstances;
    }
    static int numberOfInstances;
};

int Foo::numberOfInstances = 0;

int main()
{
    std::cout<<Foo::numberOfInstances<<"\n";// acces to member without object
    Foo a;
    std::cout<<a.numberOfInstances<<"\n";// 1
    Foo b, c;
    std::cout<<b.numberOfInstances<<"\n";// 3
}

Member functions

Static member functions are not associated with any objects. Static member functions have acces only to static data members. They cannot be: virtual, const and volatile.

#include <iostream>

class Foo
{
public:
    Foo ()
    {
        ++numberOfInstances;
    }

    static int getNumberOfInstances()
    {
        z = 4;// error, 'z' is not static!
        this-<numberOfInstances = 0;//error, cannot use 'this' in static functions
        return numberOfInstances;
    }

private:
    int z =3;
    static int numberOfInstances;
};

int Foo::numberOfInstances = 0;

int main()
{
    std::cout<<Foo::getNumberOfInstances()<<"\n";// acces to static function without object
    Foo a;
    std::cout<<a.getNumberOfInstances()<<"\n";// acces to static function with object
}

czwartek, 23 marca 2017

[C++]Virtual methods

#include <iostream>
const float pi = 3.14159;

class Shape
{
public:
    virtual float getArea()//"virtual" couses, compilator
    {                    //have to find out, on which object
        return -1.0;     //pointer points (base or derivative)
    }
};

class Square: public Shape
{
public:
    Square(const float side): a(side){}
    void pureVirtualMethod(){}
    float getArea()
    {
        return a * a;
    }

private:
    float a;
};

int main ()
{
    Shape *shape;
    Square square(3);
    shape = &square;
    std::cout<<square.getArea()<<std::endl;
    /*if in base class the getArea() method wasn't mark as virtual
    that following line will call method from base class(not derivative)*/
    std::cout<<shape->getArea()<<std::endl;
}

piątek, 3 marca 2017

[C++][Qt]How to sort QComboBox.

    combo->addItems(QStringList() << "B" << "C" << "A"); // "B","C","A"
    // for sorting you need the following 4 lines
    QSortFilterProxyModel* proxy = new QSortFilterProxyModel(combo);  // <--
    proxy->setSourceModel(combo->model());                            // <--
    // combo's current model must be reparented,
    // otherwise QComboBox::setModel() will delete it
    combo->model()->setParent(proxy);                                 // <--
    combo->setModel(proxy);                                           // <--
    // sort
    combo->model()->sort(0); // "A","B","C"

czwartek, 5 stycznia 2017

[C++] Symulator Lotto

Dzisiaj chciałbym zaprezentować malutki programik symulujący grę liczbową Lotto. Do napisania tego programiku zainspirowała mnie wczorajsza rozmowa z kolegą.

Program najpierw nas prosi o podanie sześciu liczb z przedziału 1..49. Program jest zabezpieczony przed tym, aby żadna liczba nie dublowała się oraz aby użytkownik nie podał liczby spoza przedziału 1..49. Program nie jest jeszcze odporny na wprowadzenie jakiegoś znaku przez użytkownika i potrafi się "wysypać".

Następnie "następuje zwolnienie blokady" i jest losowanych sześć liczb z zakresu 1..6. Oczywiście tu również sprawdzany jest warunek, czy liczby się nie dublują.

Program sprawdza ile liczb trafiliśmy, następnie pokazuje trafione liczby (usunąć wyświetlane zera) i jeśli coś zarobiliśmy to ta kwota jest dodawana do sumy naszych wygranych.

Program zlicza ilość losowań, po czym informuje ile wydaliśmy i ile wygraliśmy

Na koniec zostajemy zapytani o to czy wziąć udział w następnym losowaniu, czy zakończyć grę. Jeśli chcemy wziąć udział w następnym losowaniu, to kupon (liczby, które wytypowaliśmy) pozostaje ten sam, nie musimy ponownie wypełniać kuponu.

Tutaj jest do pobrania repozytorium: https://github.com/lukaszres/lotek oraz skompilowany plik na Windows: lotek.exe.

Screeny: