Talk:Classes: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎The point of the C code?: It only covers encapsulation, which is all the task explicitly requires. Perhaps the task should be deprecated.)
Line 14: Line 14:
Isn't the C sample here a bit useless? After all this work, one ends up with a blob of data, a few functions intended to operator on said data, a lot of ugly long identifiers but no way to do inheritance or polymorphism, because nothing is provided for method dispatching base on type or object. Where's the OO in this? --[[User:Ledrug|Ledrug]] 22:00, 19 June 2011 (UTC)
Isn't the C sample here a bit useless? After all this work, one ends up with a blob of data, a few functions intended to operator on said data, a lot of ugly long identifiers but no way to do inheritance or polymorphism, because nothing is provided for method dispatching base on type or object. Where's the OO in this? --[[User:Ledrug|Ledrug]] 22:00, 19 June 2011 (UTC)
: When I read the task, the requirements boil down to this sentence: "The purpose of this task is to create a basic class with a method, a constructor, an instance variable and how to instantiate it. ". It appears to only cover encapsulation, not polymorphism or inheritance. There's nothing in there about polymorphism or dispatching based on type. However, I think it may be perfectly appropriate to create multiple tasks, one to an aspect of OO, and deprecate this task in favor of them. That's likely to produce far better results. --[[User:Short Circuit|Michael Mol]] 10:30, 20 June 2011 (UTC)
: When I read the task, the requirements boil down to this sentence: "The purpose of this task is to create a basic class with a method, a constructor, an instance variable and how to instantiate it. ". It appears to only cover encapsulation, not polymorphism or inheritance. There's nothing in there about polymorphism or dispatching based on type. However, I think it may be perfectly appropriate to create multiple tasks, one to an aspect of OO, and deprecate this task in favor of them. That's likely to produce far better results. --[[User:Short Circuit|Michael Mol]] 10:30, 20 June 2011 (UTC)
: While C isn't OO, it can be used to create class-like things. If you stick some function pointers in a struct, it can start to act like a rudimentary class from any number of OO languages. With some work, one struct could also "inherit" from another by copying function pointers and data out of one class and into a second. While none of this would happen automatically, it could be made to happen. Consider:
<lang c>#include <stdio.h>
#include <malloc.h>

struct foo{
int some_int;
void (*new)(struct foo *, int);
void (*print)(struct foo *);
void (*clean)(struct foo *);
};

void foo_new( struct foo * self, int a ){
self->some_int = a;
}

void foo_print( struct foo * self ){
printf( "%d\n", self->some_int );
}

void foo_clean( struct foo * self ){
free( self );
}

struct foo * foo(){
struct foo * new = malloc( sizeof( struct foo ) );
new->new = &foo_new;
new->print = &foo_print;
new->clean = &foo_clean;
return new;
}

int main(){
struct foo * bar = foo();

bar->new( bar, 42 );
bar->print( bar );
bar->clean( bar );

return 0;
}</lang>

Now you could use some other "class" (struct + function pointers) to change the function pointers in some instance of struct foo, emulating some very basic polymorphism. While it's not the best practice, some_int could be a pointer, allowing you to put different types of data "in" the struct, allowing the rest of the code to call foo->print while remaining oblivious to what's really there.

Convoluted, sure, but it works and allows for some OO-like programming.

--[[User:Bnlott|Bnlott]] 22:59, 1 July 2011 (UTC)

Revision as of 22:59, 1 July 2011

The task here is not specific enough. The task should an object that does something very simple, for example, a person which has a first name and last name and a method to return full name or a shape returning its area, etc.

I think it still works. Adding comments is simple enough for explanation. --Mwn3d 16:45, 9 March 2009 (UTC)

Destructor

What do you guys think about adding a destructor requirement where appropriate? --Mwn3d 16:45, 9 March 2009 (UTC)

I think it's a good idea to show, but not worth putting as a requirement. —Donal Fellows 09:09, 3 September 2009 (UTC)

Classes as Objects

Some object systems have classes as being entities in the object system: there's a class of classes. Others do not. Is this worth mentioning here (possibly with a link to a task to show what's going on)? (Also, some of the object systems that have a class of classes also allow subclassing of that class...) —Donal Fellows 09:12, 3 September 2009 (UTC)

The point of the C code?

Isn't the C sample here a bit useless? After all this work, one ends up with a blob of data, a few functions intended to operator on said data, a lot of ugly long identifiers but no way to do inheritance or polymorphism, because nothing is provided for method dispatching base on type or object. Where's the OO in this? --Ledrug 22:00, 19 June 2011 (UTC)

When I read the task, the requirements boil down to this sentence: "The purpose of this task is to create a basic class with a method, a constructor, an instance variable and how to instantiate it. ". It appears to only cover encapsulation, not polymorphism or inheritance. There's nothing in there about polymorphism or dispatching based on type. However, I think it may be perfectly appropriate to create multiple tasks, one to an aspect of OO, and deprecate this task in favor of them. That's likely to produce far better results. --Michael Mol 10:30, 20 June 2011 (UTC)
While C isn't OO, it can be used to create class-like things. If you stick some function pointers in a struct, it can start to act like a rudimentary class from any number of OO languages. With some work, one struct could also "inherit" from another by copying function pointers and data out of one class and into a second. While none of this would happen automatically, it could be made to happen. Consider:

<lang c>#include <stdio.h>

  1. include <malloc.h>

struct foo{

 int some_int;
 void (*new)(struct foo *, int);
 void (*print)(struct foo *);
 void (*clean)(struct foo *);

};

void foo_new( struct foo * self, int a ){

 self->some_int = a;

}

void foo_print( struct foo * self ){

 printf( "%d\n", self->some_int );

}

void foo_clean( struct foo * self ){

 free( self );

}

struct foo * foo(){

 struct foo * new = malloc( sizeof( struct foo ) );
 new->new = &foo_new;
 new->print = &foo_print;
 new->clean = &foo_clean;
 return new;

}

int main(){

 struct foo * bar = foo();
 bar->new( bar, 42 );
 bar->print( bar );
 bar->clean( bar );
 return 0;

}</lang>

Now you could use some other "class" (struct + function pointers) to change the function pointers in some instance of struct foo, emulating some very basic polymorphism. While it's not the best practice, some_int could be a pointer, allowing you to put different types of data "in" the struct, allowing the rest of the code to call foo->print while remaining oblivious to what's really there.

Convoluted, sure, but it works and allows for some OO-like programming.

--Bnlott 22:59, 1 July 2011 (UTC)