Tutorial 1: Intro/C++ - Sept 15th
TA: Joshua Horacsek
joshua.horacsek@ucalgary.ca
My Job:
Prof: Erika Harrison
eharris@ucalgary.ca
Assignment 0 (0%) - Environment Setup
Although this is not worth any marks, I will grade this assignment and it is in your best interest to do it. You'll use what you learn here in assignment 1.
Assignment 1 (10%) - Intro to OpenGL
Assignment 2 (10%) - ¯\_(ツ)_/¯
Assignment 3 (10%) - Object Modeller
Assignment 4 (10%) - Ray Tracer
This last assignment is no joke, get started on it early!
You must also demonstrate your assignment to me. Demos will take place during the tutorial immediately following the assignment deadline.
Midterm Exam (30%) - Nov 4th, in class
Final Exam (30%) - ???
I will likely not be marking these : )
Assignments are to be done independently. Any instances of code reuse by you for the assignment must be explicitly mentioned within the assignment's README file. As a TA, I will not (and cannot) confront offenders about plagiarism. Just because I won't speak to an offender about plagiarism, does not mean I will not notice it. Instances of suspected plagiarism will be reported and escalated up the chain of command -- at best it will result in a zero on your assignment.
Please see http://www.ucalgary.ca/honesty/plagiarism
In general, half problem solving, half content. With the exception of today, work periods and demo periods
Question: What are the principles of agile development?
12 principles of agile development (wikipedia):
Customer satisfaction by rapid delivery of useful software
Welcome changing requirements, even late in development
Working software is delivered frequently (weeks rather than months)
Close, daily cooperation between business people and developers
Projects are built around motivated individuals, who should be trusted
Face-to-face conversation is the best form of communication (co-location)
Working software is the principal measure of progress
Sustainable development, able to maintain a constant pace
Continuous attention to technical excellence and good design
Simplicity - the art of maximizing the amount of work done - is essential
Self-organizing teams
Regular adaptation to changing circumstance
Very short, daily updates, covering:
Problem solving sections?
mkdir ~/cpsc453/
cd ~/Downloads/ # The file might actually be in /tmp/
unzip src.zip -d ~/cpsc453/
qmake -project QT+=widgets # Makes a .pro file for your project
qmake # Generates a makefile
make # Builds the project
# You may also need to add
export PATH=/usr/lib64/qt5/bin:$PATH
export QTDIR=/usr/lib64/qt5
export QTINC=/usr/include/qt5
# to ~/.bashrc, which you can do via
gedit ~/.bashrc &
source ~/.bashrc # reload the file
# If you're still having issues, add
setenv PATH /usr/lib64/qt5/bin:$PATH
setenv QTDIR /usr/lib64/qt5
setenv QTINC /usr/include/qt5
# To ~/.login via
gedit ~/.login & Getting Started
Variables:
int width = 400;
int height = width + 100;
bool check = (width < height);
float pi = 3.14f;
int start; // What is this initialized to?
const int start; // What is this initialized to?Strings in C++ are a bit different from those in Java, more on that soon
Functions:
void aCoolFunction(int param1) {
param1 = param1 + 1;
}
void aCoolerFunction(int ¶m1) { // pass param1 by reference
param1 = param1 + 1;
}
// Example of a main function
int main(int argc, char *argv[]) {
int value = 1;
aCoolFunction(value); // What is the number in "value"
aCoolerFunction(value); // What is the number in "value" now?
return 0; // 0 - normal exit
}
/*
* CPSC 453 - Introduction to Computer Graphics
* Assignment 0
*
* Entry point for execution into the program
*/
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
{
// Allocate and construct a QApplication instance on the stack
QApplication a(argc, argv);
// Allocate and construct a Window instance (default constructor)
Window w;
w.show();
return a.exec();
}
Classes:
/*
* window.h
* h/hpp files generally contain the specification for a class.
*/
class Window : public QMainWindow // Window inherits from QMainWindow (defined elsewhere)
{
// Don't worry about this, this isn't standard C++
// but rather a macro that Qt uses to know that this
// add some members to this class.
Q_OBJECT
public:
// Constructor, with argument parent (default value = 0, or NULL)
Window(QWidget *parent = 0);
// Deconstructor, these are used in C++ to free resources your object
// may be holding on to
~Window();
Classes:
window.h
class Window : public QMainWindow
{
Q_OBJECT
public:
Window(QWidget *parent = 0);
~Window();
private slots: // this also isn't standard C++!
/* Here, "private slots:" is a Qt specific way of exporting
"slot" class methods, more on these next tutorial */
// Activates the given drawing mode
void setDrawMode(QAction * action);
private:
// Main widget for drawing
Renderer *renderer; // This is a pointer to an object, more on those later
// ...
}; // <- Don't forget the ";"! Classes:
window.cpp
#include "window.h"
#include "renderer.h"
// after ":" is a call to a parent constructor
Window::Window(QWidget *parent) : QMainWindow(parent) {
// Do stuff
}
// Method function implementations are declared by
// returnType ClassName::Method(parameters) { ... }
// Activates the given drawing mode
void Window::setDrawMode(QAction * action) {
if (action == mLineAction)
this->renderer->setDrawMode(Renderer::DRAW_LINE);
else if (action == mOvalAction)
this->renderer->setDrawMode(Renderer::DRAW_OVAL);
else // (action == mRecAction)
this->renderer->setDrawMode(Renderer::DRAW_RECTANGLE);
}In general, header files (.h) contain class and function declarations (ala Java's interface)
Implementation files (.cpp) contain the respective implementations. This is why we must use :: to identify the functions to the classes (eg. void SampleClass::SampleFunction() { ... } )
Pointers:
Window *myWindow = new Window();
// Memory is allocated for myWindow, then the constructor is called
// with the address of that memory as its base
// Question, what happens when comparing these, for example
myWindow == anotherWindow;
*myWindow == *anotherWindow; // Better?
// The & operator will return the address of variable on the stack
int variable = 100;
int *ptrToVariable = &variable;
// The * dereferences the data at an address
printf("ptrToVariable points to the value %d\n", *ptrToVariable);
// What happens if you do this
ptrToVariable = 100;
// Question, what will the line above do if we do the following right before it
variable = 200;
Memory Management (Stack vs Heap):
void aCoolFunction(int nLength) {
// Stack allocation
int intList[100]; // How do we allocate nLength of these on the stack?
// Some code...
}
void anotherCoolFunction(int nLength)
{
// Heap allocation
int * intList = new int[nLength]; // Dynamically allocate memory
// Some code...
delete intList; // You need to delete (free) the pointer when you're done
}
Window *gWindow = NULL;
void aCoolFunction() {
Window w; // Constructor called
gWindow = &w;
return; // What happens here?
}
gWindow->show();
// ---------
Window *gWindow = NULL;
void anotherCoolFunction() {
Window *w = new Window();
gWindow = w;
return;
}
gWindow->show();
delete gWindow;Memory Management (Stack vs Heap):
Preprocessor:
When defining classes, you must only define it once. If it needs to be included in several files, a redeclaration will take place, causing compiler issues. For good practise, be sure to use the following to prevent redeclaration,
#ifndef SAMPLE_CLASS_H
#define SAMPLE_CLASS_H
class SampleClass
{
SampleClass();
~SampleClass();
...
};
#endifC++ has a large collection of data structures and libraries within its Standard Template Library. This will help us for I/O. See Google for other examples (or cplusplus.com)
#include <iostream.h> // not all systems require the .h (ie. <iostream>)
using std::cout;
using std::cin;
using std::endl;
...
int myVar = 5;
cout << "The value of my variable is: " << myVar << endl;
cin >> myVar;
cout << "The new value of my variable is: " << myVar << endl;cout sends output to the console. cin gets input from the console. This information is sent in the form of streams. << pushes information into a stream. >> retrieves information from a stream.
Compilation
To compile from the command line, you would do:
g++ -c Point2D.cpp
g++ -c example.cpp
# The above creates separate output files.
# They are then linked together to form a cohesive executable:
g++ -o Output Point2D.o example.oLuckily, Qt offers a way to automatically:
Compilation
# To reiterate!
# Create the .pro file
qmake -project QT+=widgets
# Generate a makefile from that .pro file
qmake
# Build
make
# Run the code
./a0
Compilation
Or you can make your own Makefile
MyProgram: Point2D.o example.o
g++ -o MyProgram Point2D.o example.o
example.o: example.cpp
g++ -c example.cpp
Point2D.o: Point2D.cpp Point2D.h
g++ -c Point2D.cpp
clean:
rm MyProgram Point2D.o example.o
# At least one blank line must be added at the end of the Makefile
Now, to compile, all we need to do is type make (Look at man make for more details). To clean things up, we type make clean, based on the target we created.
Stuff you should checkout:
Remember, Google is your friend : )
Questions? Come speak to me, or e-mail me