Sup /prog/, self-learning programmer here. I have a question with inheritance. Say that I have a class called Food with an attribute "name" and "weight". Do I have to make subclasses with each type of food I will use (ie. apple is-a food, hence I need to make an apple subclass) or do I just instantiate a new food and call it apple? Note that I won't specifically give "apple" new attributes/methods, but I will make entities of it (ie. a person has 3 apples and 2 oranges blabla)
I realize this is a simple question but I'm a fucking baffled moron and can't seem to solve it.
Name:
F r o z e n V o i d !!mJCwdV5J0Xy2A212011-11-22 0:50
>>6
enums are not useful for a "proper OOP" abstraction here. The fruit_list is just an array, you can't just declare another apple in the middle of code without changing the array.
what if you need food apple; in a another function? or returned from function? taking pointer to a struct is straightforward,as well as dereferencing it, unlike a single global array which would be aliased to dozen of functions and reused as some object pool(used for speed).
Name:
F r o z e n V o i d !!mJCwdV5J0Xy2A212011-11-22 1:48
>>8
You can easily add fields to a struct if you provide it with expansion capability(extra ten void* fields for example, whicha re unused byt can be reused for anything that comes in the future)
Name:
Anonymous2011-11-22 4:43
>>7
It may not be ``proper OOP'' (i.e. enterprise bullshit) but it is what >>1 wanted.
ONE WORD LISKOV SUBSTITUTION PRINCIPLE THREAD OVER
Name:
F r o z e n V o i d !!mJCwdV5J0Xy2A212011-11-22 9:33
>frozen_void.favourite_fruit = LEMON;
Thats blatantly false, i prefer mangoes or at least oranges(when mangoes are out of season).
Your design is too convoluted, and its as "enterprise bullshit" as it gets init_person(),print_person()?
Name:
F r o z e n V o i d !!mJCwdV5J0Xy2A212011-11-22 11:22
Here is fixed version
#include <stdio.h>
enum fruit_type {APPLE, ORANGE, LEMON, RASPBERRY,MANGO, NB_FRUIT_TYPES};
typedef size_t fruit_list[NB_FRUIT_TYPES];
static const char* fruit_names[][2] =
{{"apple", "apples"},
{"orange", "oranges"},
{"lemon", "lemons"},
{"raspberry", "raspberries"},{"mango", "mangoes"}};
//touhou makes people that..retarded?
void fruits(person* pn){
printf("This is %s. %s has ",pn->name,pn->flags&PERSON_HAS_PROPER_SRY_GENE?"He":"She");
int fruit,i,fruitcount=0;
for (i = 0; i < NB_FRUIT_TYPES; i++){
fruit=pn->owned_fruits[i];fruitcount+=fruit;
if(fruit){
printf("%s ",fruit>1?(char*)itoa(fruit):"one");
printf(" %s,",fruit_names[i][fruit>1]);}
}
if(fruitcount){printf("Total:%d fruits",fruitcount);}else{printf("no fruits");}
printf(". %s favourite fruit is the %s\n",pn->flags&PERSON_HAS_PROPER_SRY_GENE?"His":"Her", fruit_names[pn->favourite_fruit][0]);
}
enum persons{reimu,me,you,sussman,frozen_void,pnumber}
main(void)
{
person pn[pnumber]={
{.name="Hakurei Reimu",.owned_fruits[RASPBERRY] = 5},{.name="Anonymous",.favourite_fruit = RASPBERRY},
{.name = "VIPPER",.owned_fruits={[APPLE]= 2,[ORANGE]= 1}},
{.name = "Gerald Jay Sussman",.flags=0| PERSON_HAS_PROPER_SRY_GENE,.favourite_fruit = APPLE},
{.name = "F r o z e n V o i d",.owned_fruits={[LEMON] = 2},.favourite_fruit = MANGO}};
for(int i=0;i<pnumber;i++){fruits(&pn[i]);}
puts("Reimu Hakurei gives one raspberry to Anonymous.\n");
pn[reimu].owned_fruits[RASPBERRY] -= 1;
pn[me].owned_fruits[RASPBERRY] += 1;
fruits(&pn[reimu]);
fruits(&pn[me]);
//touhou makes people that..retarded?
void fruits(person * pn)
{
printf("This is %s. %s has ", pn->name, pn->flags & PERSON_HAS_PROPER_SRY_GENE ? "He" : "She");
int fruit, i, fruitcount = 0;
for (i = 0; i < NB_FRUIT_TYPES; i++) {
fruit = pn->owned_fruits[i];
fruitcount += fruit;
if (fruit) {
printf("%s ", fruit > 1 ? (char *) itoa(fruit) : "one");
printf(" %s,", fruit_names[i][fruit > 1]);
}
}
if (fruitcount) {
printf("Total:%d fruits", fruitcount);
} else {
printf("no fruits");
}
printf(". %s favourite fruit is the %sn", pn->flags & PERSON_HAS_PROPER_SRY_GENE ? "His" : "Her", fruit_names[pn->favourite_fruit][0]);
}
enum persons { reimu, me, you, sussman, frozen_void, pnumber } main(void)
{
person pn[pnumber] = {
{.name = "Hakurei Reimu",.owned_fruits[RASPBERRY] = 5}, {.name = "Anonymous",.favourite_fruit = RASPBERRY},
{.name = "VIPPER",.owned_fruits = {[APPLE] = 2,[ORANGE] = 1}},
{.name = "Gerald Jay Sussman",.flags = 0 | PERSON_HAS_PROPER_SRY_GENE,.favourite_fruit = APPLE},
{.name = "F r o z e n V o i d",.owned_fruits = {[LEMON] = 2},.favourite_fruit = MANGO}};
for (int i = 0; i < pnumber; i++) {
fruits(&pn[i]);
}
puts("Reimu Hakurei gives one raspberry to Anonymous.n");
pn[reimu].owned_fruits[RASPBERRY] -= 1;
pn[me].owned_fruits[RASPBERRY] += 1;
fruits(&pn[reimu]);
fruits(&pn[me]);
}
Name:
Anonymous2011-11-22 11:50
>>13
1. itoa isn't standard.
2. main not returning a value of type int isn't standard.
3. Of course, it's simpler to initialize structures with C99 structure litterals.
4. Your formatting style is horrendous.
5. The number of persons is much more likely to vary than the numbers of different fruit types. I don't think it's a good idea to use enum-array style for that.
6. Your code's output doesn't follow proper English punctuation.
Name:
Anonymous2011-11-22 12:01
Gksolrk
Name:
F r o z e n V o i d !!mJCwdV5J0Xy2A212011-11-22 12:13
class (Food) {
extends(Object);
char * name;
double weight;
}
class (Apple) {
extends(Food);
}
/* ... */
Name:
F r o z e n V o i d !!mJCwdV5J0Xy2A212011-11-22 13:27
//now with interactive features so you can practice your autism.
#include <stdio.h>
#include <string.h>
enum persons { reimu, me, you, sussman, frozen_void, pnumber };
enum fruit_type { APPLE, ORANGE, LEMON, RASPBERRY, MANGO, NB_FRUIT_TYPES };
typedef size_t fruit_list[NB_FRUIT_TYPES];
char *fruit_names[][2] = { {"apple", "apples"},
{"orange", "oranges"},
{"lemon", "lemons"},
{"raspberry", "raspberries"}, {"mango", "mangoes"}
};
#define PERSON_HAS_PROPER_SRY_GENE 1
#define IS_MALE(x) !!((x).flags & PERSON_HAS_PROPER_SRY_GENE)
typedef struct {
const char *name;
unsigned int flags;
fruit_list owned_fruits;
enum fruit_type favourite_fruit;
} person;
person pn[pnumber] = {
{.name = "Hakurei Reimu",.owned_fruits[RASPBERRY] = 5}, {.name = "Anonymous",.favourite_fruit = RASPBERRY},
{.name = "VIPPER",.owned_fruits = {[APPLE] = 2,[ORANGE] = 1}},
{.name = "Gerald Jay Sussman",.flags = 0 | PERSON_HAS_PROPER_SRY_GENE,.favourite_fruit = APPLE},
{.name = "F r o z e n V o i d",.owned_fruits = {[LEMON] = 2},.favourite_fruit = MANGO}};
//touhou makes people that..retarded?
void fruits(person * pn){
printf("This is %s. %s has ", pn->name, pn->flags & PERSON_HAS_PROPER_SRY_GENE ? "He" : "She");
int fruit, i, fruitcount = 0;
for (i = 0; i < NB_FRUIT_TYPES; i++) {
fruit = pn->owned_fruits[i];
fruitcount += fruit;
if (fruit) { printf("%d %s ", fruit,fruit_names[i][fruit > 1]); }
}
if (fruitcount) {
printf("Total:%d fruits", fruitcount);
} else {
printf("no fruits");
}
printf(". %s favourite fruit is the %s\n", pn->flags & PERSON_HAS_PROPER_SRY_GENE ? "His" : "Her", fruit_names[pn->favourite_fruit][0]);
}
void transact(person* pn ,int sender,int receiver,int fruittype,int amount){
if(sender>pnumber){printf("Sender:%d out of range",sender);return;}
if(sender<0){printf("Sender:%d does not exist",sender);return;}
if(receiver>pnumber){printf("receiver:%d out of range",receiver);return;}
if(receiver<0){printf("receiver:%d does not exist",receiver);return;}
if(fruittype<0){
printf("Error:Fruit type %d does not exist",fruittype);return;}
if(fruittype>NB_FRUIT_TYPES){
printf("Fruit:%d out of range",fruittype);return;}
if(pn[sender].owned_fruits[fruittype]<amount||amount<0){printf("\nError: sending %d %s cannot be done, too few present. Please purchase more fruits\n",amount,fruit_names[fruittype][amount>1]);}else{
pn[sender].owned_fruits[fruittype]-=amount;
pn[receiver].owned_fruits[fruittype]+=amount;
printf("\n%s gives %d %s to %s\n",pn[sender].name,amount, fruit_names[fruittype][amount > 1],pn[receiver].name);
fruits(&pn[sender]);
fruits(&pn[receiver]);}
}
int selectu(person* db,char*name){int i;
for( i=0;i<pnumber;i++){
if(!strcmp(pn[i].name,name)){return i;}}}
int selectf(char* name){int i;
for( i=0;i<NB_FRUIT_TYPES;i++){
if(!strcmp(fruit_names[i][1],name)){return i;}}}
main(void) {
printf("Available fruits:");
for (int i = 0; i < NB_FRUIT_TYPES; i++) {
printf("#%d:%s,",i,fruit_names[i][0]);
};printf("\n");
for (int i = 0; i < pnumber; i++) {
printf("User#%d :",i);fruits(&pn[i]);
};
char name[255];
int amount,sender,receiver,fruittype;
printf("\nEnter numbers for:amount, fruittype,sender number,receiver number:");
fgets((char*)&name,sizeof(name), stdin);
sscanf(name, "%d,%d,%d,%d",&amount,&fruittype,&sender,&receiver);
#define PERSON_HAS_PROPER_SRY_GENE 1
#define IS_MALE(x) !!((x).flags & PERSON_HAS_PROPER_SRY_GENE)
typedef struct {
const char *name;
unsigned int flags;
fruit_list owned_fruits;
enum fruit_type favourite_fruit;
} person;
person pn[pnumber] = {
{.name = "Hakurei Reimu",.owned_fruits[RASPBERRY] = 5}, {.name = "Anonymous",.favourite_fruit = RASPBERRY},
{.name = "VIPPER",.owned_fruits = {[APPLE] = 2,[ORANGE] = 1}},
{.name = "Gerald Jay Sussman",.flags = 0 | PERSON_HAS_PROPER_SRY_GENE,.favourite_fruit = APPLE},
{.name = "F r o z e n V o i d",.owned_fruits = {[LEMON] = 2},.favourite_fruit = MANGO}};
//touhou makes people that..retarded?
void fruits(person * pn){
printf("This is %s. %s has ", pn->name, pn->flags & PERSON_HAS_PROPER_SRY_GENE ? "He" : "She");
int fruit, i, fruitcount = 0;
for (i = 0; i < NB_FRUIT_TYPES; i++) {
fruit = pn->owned_fruits[i];
fruitcount += fruit;
if (fruit) { printf("%d %s ", fruit,fruit_names[i][fruit > 1]); }
}
if (fruitcount) {
printf("Total:%d fruits", fruitcount);
} else {
printf("no fruits");
}
printf(".\n %s favourite fruit is the %s\n", pn->flags & PERSON_HAS_PROPER_SRY_GENE ? "His" : "Her", fruit_names[pn->favourite_fruit][0]);
}
void transact(person* pn ,int sender,int receiver,int fruittype,int amount){
if(sender>pnumber){printf("Sender:%d out of range",sender);return;}
if(sender<0){printf("Sender:%d does not exist",sender);return;}
if(receiver>pnumber){printf("receiver:%d out of range",receiver);return;}
if(receiver<0){printf("receiver:%d does not exist",receiver);return;}
if(fruittype<0){
printf("Error:Fruit type %d does not exist",fruittype);return;}
if(fruittype>NB_FRUIT_TYPES){
printf("Fruit:%d out of range",fruittype);return;}
if(pn[sender].owned_fruits[fruittype]<amount||amount<0){printf("\nError: sending %d %s cannot be done, too few present. Please purchase more fruits\n",amount,fruit_names[fruittype][amount>1]);}else{
pn[sender].owned_fruits[fruittype]-=amount;
pn[receiver].owned_fruits[fruittype]+=amount;
printf("\n%s gives %d %s to %s\n",pn[sender].name,amount, fruit_names[fruittype][amount > 1],pn[receiver].name);
fruits(&pn[sender]);
fruits(&pn[receiver]);}
}
int selectu(person* db,char*name){int i;
for( i=0;i<pnumber;i++){
if(!strcmp(pn[i].name,name)){return i;}}}
int selectf(char* name){int i;
for( i=0;i<NB_FRUIT_TYPES;i++){
if(!strcmp(fruit_names[i][1],name)){return i;}}}
main(void) {
printf("Available fruits:");
for (int i = 0; i < NB_FRUIT_TYPES; i++) {
printf("#%d:%s,",i,fruit_names[i][0]);
};printf("\n");
for (int i = 0; i < pnumber; i++) {
printf("User#%d :",i);fruits(&pn[i]);
};
char name[255];
int amount,sender,receiver,fruittype;
while(1){
printf("\nEnter numbers for:amount, fruittype,sender number,receiver number:");
fgets((char*)&name,sizeof(name), stdin);
sscanf(name, "%d,%d,%d,%d",&amount,&fruittype,&sender,&receiver);
transact(pn,sender,receiver,fruittype,amount);
}
return 1;}
Name:
Anonymous2011-11-22 14:56
>>20
Lacks MS SQL bindings and remote network access.