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 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"}};

#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;


//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]);

}

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