Lesson 13 of 18

Classes

Classes

HolyC uses class instead of C's struct/typedef struct. A class is a value type that groups related data together.

Defining a Class

class Point {
  I64 x, y;
};

The semicolon after the closing brace is required (same as C structs).

Creating Instances

Declare a variable of the class type and set its members using the dot operator .:

Point p;
p.x = 10;
p.y = 20;
Print("x=%d y=%d\n", p.x, p.y);

Pointers to Classes

Use -> to access members through a pointer:

Point p;
p.x = 5;
p.y = 8;

Point *ptr = &p;
Print("x=%d y=%d\n", ptr->x, ptr->y);

Classes as Function Parameters

Pass instances by value or by pointer:

class Rectangle {
  I64 width, height;
};

I64 Area(Rectangle *r) {
  return r->width * r->height;
}

Rectangle rect;
rect.width  = 6;
rect.height = 4;
Print("Area: %d\n", Area(&rect));  // Area: 24

String Members

A U8 * member holds a pointer to a string:

class Person {
  U8 *name;
  I64 age;
};

Person p;
p.name = "Terry";
p.age  = 37;
Print("%s is %d years old\n", p.name, p.age);

Your Task

Define a class Circle with an F64 radius field. Write a function F64 Circumference(Circle *c) that returns 2 * 3.14159 * c->radius.

Create a circle with radius 5.0 and print the circumference with 2 decimal places.

Expected output: 31.42

Aiwnios HolyC loading...
Loading...
Click "Run" to execute your code.