C++ Tutorial: Rob Jagnow
C++ Tutorial: Rob Jagnow
Rob Jagnow
This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
Overview
Pointers
Arrays and strings
Parameter passing
Class basics
Constructors & destructors
Class Hierarchy
Virtual Functions
Coding tips
Advanced topics
Advanced topics: friends, protected, inline functions, const, static, virtual inheritance, pure virtual function (e.g. Intersect(ray, hit) = 0), class hierarchy.
Pointers
int *intPtr;
Create a pointer
Allocate memory
*intPtr = 6837;
*intPtr
intPtr
0x0050
delete intPtr;
Deallocate memory
int otherVal = 5;
intPtr = &otherVal;
*intPtr
intPtr
5
0x0054
otherVal
&otherVal
Arrays
Stack allocation
int intArray[10];
intArray[0] = 6837;
Heap allocation
int *intArray;
intArray = new int[10];
intArray[0] = 6837;
...
delete[] intArray;
Strings
A string in C++ is an array of characters
char myString[20];
strcpy(myString, "Hello World");
output: Hi
Parameter Passing
pass by value
int add(int a, int b) {
return a+b;
}
int a, b, sum;
sum = add(a, b);
pass by reference
int add(int *a, int *b) {
return *a + *b;
}
int a, b, sum;
sum = add(&a, &b);
Parameter Passing
pass by reference alternate notation
int add(int &a, int &b) {
return a+b;
}
int a, b, sum;
sum = add(a, b);
Class Basics
#ifndef _IMAGE_H_
#define _IMAGE_H_
#include <assert.h>
#include "vectors.h
class Image {
public:
...
private:
...
};
#endif
Note that private: is the default
Creating an instance
Stack allocation
Image myImage;
myImage.SetAllPixels(ClearColor);
Heap allocation
Image *imagePtr;
imagePtr = new Image();
imagePtr->SetAllPixels(ClearColor);
...
delete imagePtr;
Stack allocation: Constructor and destructor called automatically when the function is entered and exited.
Heap allocation: Constructor and destructor must be called explicitly.
Organizational Strategy
image.h
image.C
myImage.SetAllPixels(clearColor);
Constructor:
Called whenever a new
instance is created
Destructor:
Called whenever an
instance is deleted
Constructors
Constructors can also take parameters
Image(int w, int h) {
width = w;
height = h;
data = new Vec3f[w*h];
}
stack allocation
Image *imagePtr;
imagePtr = new Image(10, 10);
heap allocation
Warning: if you do not create a default (void parameter) or copy constructor explicitly, they are created for you.
Computationally expensive
or
bool IsImageGreen(Image &img);
Class Hierarchy
Child classes inherit parent attributes
Object3D
class Object3D {
Vec3f color;
};
class Sphere : public Object3D {
float radius;
};
class Cone : public Object3D {
float base;
float height;
};
Sphere
Cone
Class Hierarchy
Child classes can call parent functions
Sphere::Sphere() : Object3D() {
radius = 1.0;
Call the parent constructor
}
Virtual Functions
A superclass pointer can reference a subclass object
Sphere *mySphere = new Sphere();
Object3D *myObject = mySphere;
Superclass
class Object3D {
virtual void intersect(Vec3f *ray, Vec3f *hit);
};
Subclass
Actually calls
Sphere::intersect
Number of
arguments
argv[0]
argv[1]
Array of
strings
Coding tips
Use the #define compiler directive for constants
#define PI 3.14159265
#define sinf sin
Access outside of
array bounds
Image *img;
img->SetAllPixels(ClearColor);
Attempt to access
a NULL or previously
deleted pointer
Advanced topics
Lots of advanced topics, but few will be required for this course
compiler directives
operator overloading
Vec3f& operator+(Vec3f &a, Vec3f &b);