C++ Tutorial Class Basics

Class Basics

#ifndef _IMAGE_H_ Prevents multiple references
#define _IMAGE_H_

#include Include a library file
#include « vectors.h“ Include a local file

class Image {

public:

Variables and functions accessible from anywhere

private:
… Variables and functions accessible only from within this class’s functions

};

#endif

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 Header file: Class definition & function prototypes void SetAllPixels(const Vec3f &color);
image.C .C file: Full function definitions
void Image::SetAllPixels(const Vec3f &color) {
for (int i = 0; i < width*height; i++)
data[i] = color;
}
main.C Main code: Function references
myImage.SetAllPixels(clearColor);

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *