Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Inheritance

One of the key features of C++ classes, and certainly one of the most useful for WCM, is inheritance . Inheritance allows creating classes which are derived from other classes, so that they automatically include its "parent's" member functions, character definitions and decals, plus its own, which may overwrite or supersede the definitions in the parent class. We will examine ways to do this in the next few sections, but first we will look at modifying the example presented in the previous section.

For example if we want to create an advanced callout balloon class based on implemented in previous section (to support scaling for example) we can do this by deriving a new class from CalloutBalloon :

// see the notes after this example for an explainatin of the ":" notation

class CalloutBalloonEx : public CalloutBalloon

{

public :

CalloutBalloonEx ( string sText ) : CalloutBalloon ( sText )

{

}


void SetPos ( double dX, double dY )

{

BalloonImage.SetPos ( dX, dY );

BalloonText.SetPos ( dX,

dY - 20 * BalloonImage.GetYScale () );

}


void SetScale ( double dScale )

{

BalloonImage.SetScale ( dScale );

BalloonText.SetScale ( dScale );

}

};

Let's explain some things in the sample code :

  1. Line class CalloutBalloonEx : public CalloutBalloon means that we declare a new sub-class CalloutBalloonEx which is derived from CalloutBalloon . Deriving means that all member functions and character definitions of CalloutBalloon will be accessible the same way in CalloutBalloonEx

  2. Constructors are not inherited, however! We need to create a new constructor! Line CalloutBalloonEx ( string sText ) : CalloutBalloon ( sText ) declares a new constructor for class CalloutBalloonEx with one string parameter sText. Before we add any additional initialization, however, we want to call the constructor of inherited class CalloutBalloon by calling it with parameter sText . In this case, CalloutBalloonEx does not require any additional initialization so it has an empty body. But there are more complex examples possible where additional initialization is required.

  3. It is possible to add new member functions to the new sib-class. For example, SetScale is a new member function.

  4. It is also possible to overwrite the member functions of the parent class. SetPos was already declared in CalloutBalloon but here we define it to behave a little differently in CalloutBalloonEx in order to take into account the current scale factor.

The new sub-class can be used in the same way as the parent class:

void Scene1 ()

{


Boy Max;

Max.SetVisible ( true );

Max.SetPos ( 300,290 );

Max.Says ( "I am going" );


CalloutBalloonEx SpeechBalloon ( "I am going" );

SpeechBalloon.SetVisible ( true );

SpeechBalloon.SetPos ( 150, 0 );

SpeechBalloon.SetScale ( 0.7 );


Max.GoesTo ( -300,290 , 5 );


SpeechBalloon.SetPos ( - 450, 0 );

}


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll