class cpp code example

Example 1: c++ class method example

class Object {
public:
	int var;
	void setVar(int n) {
		var = n;
	}
	int getNum() {
		return var;
	}
};

int main() {
	Object obj;
	obj.setVar(13);
	std::cout << obj.getNum() << std::endl;
	return 0;
}

Example 2: how to write a class in c++

class Rectangle 
{
	int width, height;
public:
	void set_values (int,int);
    int area() {return width*height;}
};

void Rectangle::set_values (int x, int y)
{
	width = x;
	height = y;
}

Example 3: classes c++

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area (void);
} rect;

Example 4: classes c++

class class_name {
  access_specifier_1:
    member1;
  access_specifier_2:
    member2;
  ...
} object_names;

Example 5: class cpp

class Exemple
{
  private:
  	/* data */
  public:
  	Exemple(); //Constructor
  	~Exemple(); //Destructor
};

Example 6: C++ class

class Personnage
{
    
}; // N'oubliez pas le point-virgule à la fin !