virtual: Support polymorphism.
=0 (pure virtual): Must be overriden.
final: Cannot be overriden.
#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.
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;
}
double d = 97 ;
char c = d; // warning only
char c {d}; // error and recommended way by Bjarne Stroustrup
int *p = nullptr ; // equivalent of NULL in C
p = new int {99 }; // allocation
cout << " Value = " << *p << endl;
delete p;
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
void fct (int param) {
param++;
}
// value not modified here either
void fct (int *pParam) {
int i = 3 ;
pParam = &i;
}
// value is modified here
void fct (*pParam) {
*pParam = 3 ;
}
Signals: Emitted when an event occurs.
Slots: Function called in response to a signal.
Usage:
connect (obj1, signal, obj2, slot);
// removed at end of block
Dialog myDialog; // calls constructor
Dialog *myDialog = new Dialog(); // until deleted