CPP Tutorial JWFILES
CPP Tutorial JWFILES
Rob Jagnow
Overview
Pointers Arrays and strings Parameter passing Class basics Constructors & destructors Class Hierarchy Virtual Functions Coding tips Advanced topics
Pointers
int *intPtr; intPtr = new int; *intPtr = 6837;
Create a pointer
Allocate memory
Set value at given address
*intPtr intPtr 6837
0x0050
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");
printf("%s", myString);
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);
Pass pointers that reference a and b. Changes made to a or b will be reflected outside the add routine
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
Variables and functions accessible only from within this classs functions
Creating an instance
Stack allocation
Image myImage; myImage.SetAllPixels(ClearColor);
Heap allocation
Image *imagePtr; imagePtr = new Image(); imagePtr->SetAllPixels(ClearColor); ... delete imagePtr;
Organizational Strategy
image.h
image.C
void Image::SetAllPixels(const Vec3f &color) { for (int i = 0; i < width*height; i++) data[i] = color; } main.C
myImage.SetAllPixels(clearColor);
Constructors
Constructors can also take parameters
Image(int w, int h) { width = w; height = h; data = new Vec3f[w*h]; }
A default copy constructor is created automatically, but it is often not what you want:
Image(Image *img) { width = img->width; height = img->height; data = img->data; }
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;
If a superclass has virtual functions, the correct subclass version will automatically be selected
Superclass Subclass
class Object3D { virtual void intersect(Ray *r, Hit *h); }; class Sphere : public Object3D { virtual void intersect(Ray *r, Hit *h); };
myObject->intersect(ray, hit);
Actually calls
Sphere::intersect
A class with a pure virtual function is called a pure virtual class and cannot be instantiated. (However, its subclasses can).
Number of arguments
Array of strings
argv[0] is the program name argv[1] through argv[argc-1] are command-line input
Coding tips
Use the #define compiler directive for constants
#define PI 3.14159265 #define MAX_ARRAY_SIZE 20
Coding tips
After you delete an object, also set its value to NULL (This is not done for you automatically)
delete myObject; myObject = NULL;
These errors are often very difficult to catch and can cause erratic, unpredictable behavior.
Common Pitfalls
void setToRed(Vec3f v) { v = RED; }
Since v is passed by value, it will not get updated outside of The set function
The fix:
void setToRed(Vec3f &v) { v = RED; }
or
void setToRed(Vec3f *v) { *v = RED; }
Common Pitfalls
Sphere* getRedSphere() { Sphere s = Sphere(1.0); s.setColor(RED); return &s; }
C++ automatically deallocates stack memory when the function exits, so the returned pointer is invalid. The fix:
Sphere* getRedSphere() { Sphere *s = new Sphere(1.0); s->setColor(RED); return s; }
Advanced topics
Lots of advanced topics, but few will be required for this course
inline functions const or static functions and variables compiler directives operator overloading
Vec3f& operator+(Vec3f &a, Vec3f &b);