Skip to content

Instantly share code, notes, and snippets.

@h4k1m0u
Last active December 14, 2018 23:51
Show Gist options
  • Save h4k1m0u/db1a464605494d4220738971d7470cad to your computer and use it in GitHub Desktop.
Save h4k1m0u/db1a464605494d4220738971d7470cad to your computer and use it in GitHub Desktop.

Methods keywords

  • virtual: Support polymorphism.
  • =0 (pure virtual): Must be overriden.
  • final: Cannot be overriden.

Arrays:

#include <iostream>

using namespace std;

int main(void) {
    int *p = new int[4] {1, 2, 3, 4};
    for(int i=0; i!=4; i++)
        cout << "i = " << *(p + i) << endl;
    delete[] p;
    
    return 0;
}

Functions (class methods)

  • static: Not associated with an instance.
  • const: Cannot modify object.

Objects (declaration)

Date today = Date(27, 10, 2018);
Date xmas {25, 12, 1990};

Overriding operators in a class

// c is a constant reference below
Complex operator+(Complex const &c) {
    Complex res;
    res.real = real + c.real; // real can be: this->real
    res.img = img + c.img; // img can be: this->img
    
    return res;
}

Conversions

double d = 97;
char c = d; // warning only
char c {d}; // error and recommended way by Bjarne Stroustrup

Memory management

int *p = nullptr; // equivalent of NULL in C
p = new int {99}; // allocation
cout << "Value = " << *p << endl;
delete p;

Classes

class X {
    private:
        int m1;
        int m2;
    public:
        X(int i1=0, int i2=0): m1{i1}, m2{i2} {}
        int getM1();
        int getM2();
};

int X::getM1() {
    return m1;
}

int X::getM2() {
    return m2;
}

Passing variables to functions in C

Pass by value

void fct(int param) {
    param++;
}

Pass pointer by value

// value not modified here either
void fct(int *pParam) {
    int i = 3;
    pParam = &i;
}

Pass by reference

// value is modified here
void fct(*pParam) {
    *pParam = 3;
}

Qt5

  • Signals: Emitted when an event occurs.
  • Slots: Function called in response to a signal.
  • Usage:
connect(obj1, signal, obj2, slot);

Stack variable

// removed at end of block
Dialog myDialog; // calls constructor

Heap variable

Dialog *myDialog = new Dialog(); // until deleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment