Thursday, May 14, 2015

Some Qt and C++ tickles for UNISA COS2614

Qt software
Some simple things to remember for the UNISA COS2614 exam, in no particular order:

Download Qt here:

Nice simple Qt examples here:

C++ if-else shorthand (ternary operators): 
condition ? value_if_true : value_if_false

Command Line Arguments: 
int main ( int argc, char *argv[] )
The integer, argc is the ARGument Count (hence argc). It is the number of arguments passed into the program from the command line, including the name of the program.

The array of character pointers is the listing of all the arguments. argv[0] is the name of the program, or an empty string if the name is not available. After that, every element number less than argc is a command line argument. You can use each argv element just like a string, or use argv as a two dimensional array. argv[argc] is a null pointer.

Explain two benefits of applying design patterns in OOP:
Improves re-usability and extensibility of software.
  • Re-usability - want to be able to re-use relevant bits of code in the simplest way possible. Design patterns attempt to express similar code in an abstract way, this allows for code reuse. We are able to design abstract classes in a way that allows us to derive from these classes and use similar code, this promotes code reusability.
  • Extensibility - how easy is it to add enhancements to an app. Design patterns also promote extensibility. A measure of how easy it is to add improvements to an application and one way of making code extensibility is to provide classes which can easily be sub-classed. Qt framework is a good example of this.


The keyword explicit can be placed before a single argument constructor in the class
definition to prevent automatic conversion from being used by the compiler. This is useful if the argument is not anything like the class it is constructing, or if there is something akin to a parent-child relationship between them.
Classes generated by QtCreator place this keyword before the generated custom widget constructors, and it is advisable that you do the same with your QWidget -derived classes.
(The keyword explicit prevents the compiler from automatically using that constructor for implicit type conversions.)

Stack and Heap stuff:
When QObjects are created with new, they are created on the heap.
Otherwise they are created on the stack.

Polymorphic Assignment:
Polymorphic assignment allows a pointer of type superclass to point to objects of its concrete subclasses.

QMessageBox::information (0, "String Reverse", reversed);

Very Simple Random number generator:

int main()
QTextStream cout(stdout);

int random = qrand() % 5;
cout << "Random number: " << random << endl;

return 0;

Very simple SIGNALS and SLOTS example:
//include necessary pre-processor directives here

int main(int argc, char *argv[]){
QApplication a(argc, argv);
QWidget w;
QLabel *label = new QLabel("Enter Text:");
QLineEdit *input = new QLineEdit("Hello");
QPushButton *clear = new QPushButton("Clear");
QVBoxLayout *layout = new QVBoxLayout();
QObject::connect(clear, SIGNAL(clicked()), input, SLOT(clear()));;
return a.exec();

What is the difference between a function declaration and a function
A function declaration describes how a function can be called.
A function definition includes the actual code that is to be executed.

3D models