C++ Tutorial
Overview
Pointers Arrays and take ins Parameter passing Class fundamental principle Constructors & destructors Class Hierarchy Virtual Functions Coding tips ripe 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; intPtr = stark naked int; *intPtr = 6837;
Create a pointer Allocate retrospect solidifying value at given address
*intPtr intPtr 6837 0x0050
delete intPtr; int otherVal = 5; intPtr = &otherVal; *intPtr intPtr
Deallocate memory Change intPtr to point to a new location
5 0x0054 otherVal &otherVal
Arrays
view allocation
int intArray[10]; intArray[0] = 6837;
Heap allocation
int *intArray; intArray = new int[10]; intArray[0] = 6837; ... delete[] intArray;
C++ arrays are zero-indexed.
Strings
A string in C++ is an array of characters
char myString[20]; strcpy(myString, Hello earth);
Strings are terminated with the NULL or character
myString[0] = H; myString[1] = i; myString[2] = ; printf(%s, myString);
output: Hi
Parameter Passing
pass by value
int add(int a, int b) { return a+b; } int a, b, pairing; sum = add(a, b);
Make a local anaesthetic copy of a & b
pass by citation
int add(int *a, int *b) { return *a + *b; } int a, b, sum; sum = add(&a, &b);
Pass pointers that reference a & 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 #include vectors.h class chain { public: ... private: ... };
No comments:
Post a Comment