   Newb c++ inheritance question
  i'm trying to model the human body(simplistically not fully) i just want to be able to make a class body and subclasses of it as body parts eg head,uppertorso,lowertorso. and i want a main class of person to be able to manage the parts. example i want my person class to check to see if the body part i have lower torso is missing. i want to be able to do: void CheckStatus(){if(lowertorso.has()){cout<< "you have a torso!\n";}else{cout<< "you are torsoless!\n";}i tried to code it and its not working its telling me that left of creat and has there must be a struct or union type but i do have it :'(can someone please help a newbie out.heres some source hope it helps solve my problem:// People.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<iostream>using namespace std;class cBody{private:bool haspart;public:cBody(){haspart = true;}cBody(bool doeshave){haspart = doeshave;}bool has(){return haspart;}void create(bool doeshave){haspart = doeshave;}};class cUpperBody:public cBody{private:int strength;public:cUpperBody(){strength = 10;}};class CPeople{private:cUpperBody ubody(bool doeshave=true);//cLowerBody lbody;//cHead head;public:CPeople(){ubody.create(false);}void CheckStatus(){if(ubody.has()){cout<<"has upper body\n";}else{cout<<"upper bodyless\n";}//if(lbody.has()){cout<<"has lower  body\n";}//else{cout<<"lower bodyless\n";}//if(head.has()){cout<<"has head\n";}//else{cout<<"headless\n";}}};int _tmain(int argc, _TCHAR* argv[]){CPeople people;people.CheckStatus();return 0;}
class CPeople{private:cUpperBody ubody(bool doeshave=true);public:CPeople(){ubody.create(false);}should be:class CPeople{private:cUpperBody ubody;public:CPeople() : ubody(){ubody.create(false);}Your code is defining a function named "ubody" which returns a "cUpperBody" and takes a "bool" with the default value of "true".This is why it had an error at "ubody.has()" - it thought "ubody" was a function, not an object, so it complained that you tried to use the "." operator on it like an object.I've changed it so now its creating a variable (object) of type "cUpperBody" and initializing it using the default constructor in "CPeople"'s constructor.   ;Quote:Original post by GotenRulezUi'm trying to model the human body(simplistically not fully) i just want to be able to make a class body and subclasses of it as body parts eg head,uppertorso,lowertorso. and i want a main class of person to be able to manage the parts.Before you get too far, you need to understand that you are going down the wrong path. Inheritance is not the correct relationship to use in this case.Inheritance expresses an "is-a" relationship. That is, if an upper body is a body, then cUpperBody should be derived from cBody. But it's not. An upper body is not a body. Your relationship is a "has-a" relationship. If a body has an upper body, then cBody should contain a cUpperBody.Your cBody class should contain the various body parts. They should not be derived from cBody. Now, you could have a cBodyPart class and then derive all the body part classes from that, since each body part type is a body part. Then, the cBody class could contain either cBodyParts or the individual sub-classes of cBodyPart.   ;  I don't mean to be pedantic, but in the line:CPeople() : ubody(){ubody.create(false);the ubody() bit is not needed since the default constructor will be called for a class member of a class type if no constructor is specified.However, since create is trivial, it would be better expressed like this:class CBodyPart{private:    bool DoesHave;public:    CBodyPart(bool Has) : DoesHave(Has) { }    // etc};class CUpperBody : public CBodyPart{public:    CUpperBody(bool Has) : CBodyPart(Has) { }};class CPerson{private:    CUpperBody Upper;public:    CPerson(bool HasUpper) : Upper(HasUpper) { }};