You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that this does not use C++, not sure why github claims so
#include<stdio.h>#include"lib.h"typedefstruct
{
intheight;
intwidth;
includes(constructor(Room));
uses_methods();
} Class(Room);
uint64_textends(Room, area)(void)
{
methodfor(Room);
return (this->height*this->width);
}
uint64_textends(Room, volume)(intvertical)
{
methodfor(Room);
return (this->height*this->width*vertical);
}
methods_for(Room) = {
(method_t)extends(Room, area),
(method_t)extends(Room, volume),
};
#definearea 0
#definevolume 1
Room_tconstructor(Room)(intwidth, intheight)
{
initiates(Room);
this.width=width;
this.height=height;
returnthis;
}
asClass(Room, livingRoom);
asClass(Room, bedroom);
intmain()
{
New(Room, livingRoom, 10, 10);
New(Room, bedroom, 15, 5);
printf("The area of the bedroom is: %i \n", (int) livingRoom.method(area));
printf("The area of the livingroom is: %i \n", (int) bedroom.method(area));
printf("The volume of the bedroom is: %i \n", (int) livingRoom.method(volume, 23));
}
Complex:
#include<stdio.h>#include<string.h>#include"lib.h"// Create our classtypedefstruct
{
// Create any propertieschar*contents;
// Include the constructor and specify that you ar eusing methodsincludes(constructor(String));
uses_methods();
} Class(String);
// All methods must return uint64uint64_textends(String, length)(void)
{
// Pull data from the classmethodfor(String);
// "this" is a pointer to the objectreturn (int)strlen(this->contents);
}
uint64_textends(String, charAt)(intindex)
{
methodfor(String);
return (char)this->contents[index];
}
uint64_textends(String, indexOf)(charcharacter)
{
methodfor(String);
intlen=strlen(this->contents);
intindex;
while (index<len)
{
if (this->contents[index] ==character)
returnindex;
index++;
}
return-1;
}
// You can only pass one argument, so use an arrayuint64_textends(String, substring)(intargs[2])
{
methodfor(String);
intstart=args[0];
intend=args[1];
if (end<start||end>strlen(this->contents))
return0;
char*newString=malloc(end-start+1);
intj=0;
for (inti=start; i<end; i++, j++)
{
newString[j] =this->contents[i];
}
newString[++j] ='\0';
// Cast to RPOINTER to return a pointerreturnRPOINTERnewString;
}
// Attached each function to the classmethods_for(String) = {
(method_t)extends(String, length),
(method_t)extends(String, charAt),
(method_t)extends(String, indexOf),
(method_t)extends(String, substring)
};
// for easy use#definelength 0 // index 0
#definecharAt 1 // index 1
#defineindexOf 2 // index 2
#definesubstring 3 // index 3
// Create the constructor for the class// Return <class>_tString_tconstructor(String)(char*contents)
{
initiates(String); // Initialize the objectthis.contents=contents;
returnthis; // return the class
}
asClass(String, myString);
intmain()
{
New(String, myString, "Hello, World!");
printf("The length of \"%s\" is %llu \n", myString.contents, myString.method(length));
printf("The 0'th character of \"%s\" is '%c' \n", myString.contents, (char)myString.method(charAt, 0));
char*substr= (char*)myString.method(substring, (int[]){7, 12});
printf("Characters 7-12 are %s \n", substr);
free(substr);
}
About
A library that adds support for Classes and methods in C