View unanswered posts | View active topics It is currently Mon Jun 25, 2018 9:36 am

Reply to topic  [ 4 posts ] 
Author Message

Joined: Thu Nov 06, 2008 4:35 pm
Posts: 12
Post Class!?!?!?
I understand that ROBOTC is based of C programing, but dose it have the ability to make classes? I am asking this because i have tried to construct a class and "class" was undefined. Is there a ROBOTC include file to overcome this problem? i assume ROBOTC can make classes because class is highlighted like any other keyword and i am just having technical problems but i just want to make sure of it. Thank you for your response!

Palm Harbor University High School, Robotics team Pandara #506

Sun Mar 01, 2009 6:29 pm
User avatar

Joined: Tue Oct 14, 2008 7:16 pm
Posts: 171
Location: Investigating an unidentified ship sighted in Sector 31428
Post Re: Class!?!?!?
First of all, standard C, as a language, does not even have such a thing as a class. The class was introduced in C++, which was originally called C with Classes; thus, there is no class in C. There does exist the struct in C and RobotC but this is not the same thing as a in C++, C#, Java, etc. The C struct is simply a wrapper for a collection of basic data types or other structs. C structs can not contain functions and there is no such thing as inheritance (that I know of), overloading, etc. C structs also don't support static or constant members, or at least RobotC structs don't

Structs can be created using the typedef keyword, thusly,
typedef struct
    int foo
    int bar

The name for the struct is placed at the end rather than the beginning of the definition. In this regard RobotC dosen't really follow the rules.

For those unfamiliar with object oriented programming, here is how you could use a struct.
A struct is essentially your own data type; you create an instance of it just like you create a standard variable,
typedef struct
    int foo
    int bar
    bool x
}TMyStruct //Declare and define struct TMyStruct

TMyStruct instance; //Create an instance of your struct = 0; //Member fields are accessed by placing a . after the instance name. = 0;
instance.x = false; //It is a good idea to give your instance default values. In a C++ class this would be done automatically if so designed.
void fun1()
    if(instance.x) //You can use member fields in conditionals...
        for(int i=0; i <; //In loops...
            int a =; //and can be manipulated just like other variables.

Now RobotC does seem to define the class keyword, but I can't get it to work even using the typedef keyword. I'm not sure what to make of it as C isn't even supposed to define the class keyword. I'd stay away from it either way as you can't use the benefits of object orientation in C nor does the NXT support dynamic memory managagement, i.e. pointers; additionally RobotC doesn't seem to completely follow the rules of C for creating structs. Unfortunately C structs, in my C++ biased opinion, aren't very useful so it's ok to not use them, but if you want to knock yourself out.

Captain, Head programmer, School of the Arts, Silverbots Robtics Team #2890
using namespace System;
using namespace Genius;
using namespace Personality;
public ref class Nerd : Geek, IAnserable
    Geek::Type brainMode = Geek::Type::Programmer;

Sun Mar 01, 2009 9:41 pm

Joined: Fri Oct 24, 2008 8:58 am
Posts: 87
Post Re: Class!?!?!?
Actually, structs can be pretty useful in RobotC due to another limitation of pointers.
But fortunately you can have function parameters pass-by-reference.

So you can get some of the benefits of classes by having a bunch of functions (ie what would be the member functions of the class) all take a parameter which is a reference to a structure type (which makes up the data members of the class).

Sadly I don't know of any trick to do inheritance with just structs unless you have real pointers too. But that's probably not the most important thing that RobotC is lacking. :)

For those looking for something a little more advanced that structs can do check this out. With everything RobotC doesn't do I was shocked that this works...but it seems to.

There is also a C device known as a union that can be combined with an annonymous struct to give 2 simultaneous ways of accessing the same data. "Why bother" you ask...

Lets say you have whole bunch of sonar values. Front left, front center, front right, back left, back center, back right.

#define MAX_SONAR 5

typedef union
  struct {
     int FL;                 // Front - Left
     int FR;                 // Front - Right
     int BL;                 // Back  - Left
     int BC;                 // Back  - Center
     int BR;                 // Back  - Right
  int arr[MAX_SONAR];
} TSonarPos;

TSonarPos mysonar;

void empty_sonar(TSonarPos &sonar)
  int i;
  for (i=0;i<MAX_SONAR;i++)
    sonar.arr[i] = 0;

bool handle_sonar(TSonarPos &sonar)
  if (sonar.FL < sonar.FR)
    return true;
   return false;

So the example is pretty useless, but you get the point.

now you can either access them as "mysonar.arr[0]" or "mysonar.FL". If you need to do something in a loop the array access is easier. If you are using them in a conditional statement, the variable access is cleaner.

I know you could do the same thing with just an array with constant names for the index...but then you run into another bug of RobotC which doesn't allow arrays to be passed by reference to a function. But here we can pass the whole variable mysonar by reference into any function that needs to use or supply sonar values.

Mon Mar 02, 2009 12:16 pm

Joined: Sun Feb 27, 2011 4:54 pm
Posts: 1
Post Re: Class!?!?!?
We have come a long way from C haven't we ?

but it is still the basic language.

I just read that Phen375 is an appetite suppress. You can see the post if you want to read it too.

Sun Feb 27, 2011 4:56 pm
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.