Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Inheritance Question

Name: Anonymous 2011-11-21 12:54

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 !!mJCwdV5J0Xy2A21 2011-11-22 13:41

//minor fix so it loops forever, instead of exiting.
    #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(".\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;}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List