Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Subclasses
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SilverSprite




PostPosted: Fri Jul 11, 2003 2:37 pm   Post subject: Subclasses

hm.. how could i create a subclass called predators of a class called animals?
Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Fri Jul 11, 2003 2:52 pm   Post subject: (No subject)

code:

class animal
{
   public:

   int x;

};

class predator:public animal
{
   public:

   int y;


};
SilverSprite




PostPosted: Fri Jul 11, 2003 8:31 pm   Post subject: (No subject)

thx..
Tony




PostPosted: Fri Jul 11, 2003 11:48 pm   Post subject: (No subject)

how would I call something like that then?

animal::predator::y ?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
SilverSprite




PostPosted: Sat Jul 12, 2003 1:08 am   Post subject: (No subject)

? no you just do it normally..
code:
predator.y

at least thast how it is in java.. sortof lool
Tony




PostPosted: Sat Jul 12, 2003 1:37 am   Post subject: (No subject)

then if you still get to use it as a class, whats the point of declearing it as a "subclass"... does it make anything different?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
UBC_Wiskatos




PostPosted: Sat Jul 12, 2003 11:46 am   Post subject: (No subject)

tony wrote:
then if you still get to use it as a class, whats the point of declearing it as a "subclass"... does it make anything different?


The class Predator is a class derived from the class Animal, and inherits its methods and whatnot. This is one of the things with OOP, inheritance. So if the class animal had basic animal behaviour like Run() and Eat(), and the class Predator inherited it, Run() and Eat() would automatically be in the class Predator, and you wouldn't have to recode them. As well, you could access the class Predator through the class Animal because the pattern of the functions are the same, but that's a bit more complicated (that is, pointer to Animal could access Predator's Animal functions no problem).
Tony




PostPosted: Sat Jul 12, 2003 3:07 pm   Post subject: (No subject)

so then would it be something like:

code:

class animal
{
   public:

   void run()
   {}

};

class predator:public animal
{
   public:

   void EatMeet()
   {}


};

class hervavor:public animal
{
   public:

   void EatGrass()
   {}


}; 


?

And I would be able to access
predator.run()
and hervavor.EatGrass() ?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
SilverSprite




PostPosted: Sat Jul 12, 2003 3:25 pm   Post subject: (No subject)

yep thast right! so predator and herbivore each get to use all the functions and whatnot inside animal (mind you the converse isnt true) as well as the functions inside themselves..
Tony




PostPosted: Sat Jul 12, 2003 3:32 pm   Post subject: (No subject)

awesome, thx guys Smile Not that I need this at the moment, but I learn more about programming from compsci.ca then from any teacher Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
UBC_Wiskatos




PostPosted: Sat Jul 12, 2003 3:35 pm   Post subject: (No subject)

tony wrote:
so then would it be something like:

code:

class animal
{
   public:

   void run()
   {}

};

class predator:public animal
{
   public:

   void EatMeet()
   {}


};

class hervavor:public animal
{
   public:

   void EatGrass()
   {}


}; 


?

And I would be able to access
predator.run()
and hervavor.EatGrass() ?


Correct. Perhaps, a good example of real-world usage of this is in a simpler game engine (by that I mean non-COM because then it becomes a bit more complicated). Say you had a function called Render() in version 1.0... Well, you came out with version 2.0 and it now has a function RenderCoolness() that everyone is supposed to use. If people use the old Render() that function doesn't exist. However, if you were to inherit your old engine 1.0 to the new 2.0, people could still access Render(), and it wouldn't break their older games. Do you see what I mean?
Mazer




PostPosted: Sat Jul 12, 2003 7:28 pm   Post subject: (No subject)

i understand the concept but not the practicality in your example. if everybody is supposed to use RenderCoolness() then why not dump the old Render() completely and name the new one Render()
UBC_Wiskatos




PostPosted: Sat Jul 12, 2003 9:01 pm   Post subject: (No subject)

Mazer wrote:
i understand the concept but not the practicality in your example. if everybody is supposed to use RenderCoolness() then why not dump the old Render() completely and name the new one Render()


If the new Render takes different parameters and whatnot, old games based on that old function will be broken, because they'll call Render() and this one is very different. This is important if you have a large graphics DLL you made, and several games are installed on your computer that use it. If you want to upgrade the version, all the games will be affected, some newer games will know to use the new function, but older games may not. This is the way DirectX works. Even the DirectX 3.0 functions are present, so that old games can still run.
rizzix




PostPosted: Sat Jul 12, 2003 11:08 pm   Post subject: (No subject)

that could be one reason to use inheritance.. but usually its for a proper oop implementation...

While explaining classes, i mentioned that this concept is not easy to explain. It relies on three other important points. (the four concepts are interrelated. u break one u broke the rest!)

maybe when i come out with a Java Tutorial, it might shed some light on this philosophy. (if i do a good job that is)
Tony




PostPosted: Sat Jul 12, 2003 11:21 pm   Post subject: (No subject)

rizzix wrote:

maybe when i come out with a Java Tutorial, it might shed some light on this philosophy. (if i do a good job that is)


plz do Smile
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: