Main Menu


Sponsored Links

  


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

Protected Data

Sometimes you may want to hide implementation details from users or programmers that don’t need to know them. More importantly, you may even want to hide or protect the implementation details from yourself to avoid accidental access to data.

For example let's try to create a class representing a callout balloon to display together with a character speech . It should contain 2 objects as members – an image object containing balloon picture and a text object containing a text to display. We'll use a callout balloon image from Web Cartoon Maker's online library:

class CalloutBalloon

{

protected:

Image BalloonImage;

Text BalloonText;


public :


CalloutBalloon ( string sText )

{

BalloonImage.SetImage ( "wcm/callout.svg" );

BalloonText.SetText ( sText );

BalloonText.SetColor ( "000000" );

SetPos ( 0, 0 );

}


void SetVisible ( bool bVisible )

{

BalloonImage.SetVisible ( bVisible );

BalloonText.SetVisible ( bVisible );

}


void SetPos ( double dX, double dY )

{

BalloonImage.SetPos ( dX, dY );

BalloonText.SetPos ( dX, dY - 20 ); // little above

}

};

There are two sections of this definition, a protected part and a public part. The functions are public , which means that they can be invoked from other places. The instance variables are protected , which means that they can be read and written only by CalloutBalloon member functions. For example the following usage is wrong:

Scene1 ()

{

CalloutBalloon MyBalloon ( "Hello" );


// WRONG!! BalloonText is protected

MyBalloon.BalloonText.SetPos ( 0,400 );

}

This is good because it makes you difficult to access and change position of BalloonImage or BalloonText individually and you are forced to use combined SetPos method changing position of both objects in synch.

Finally lets a compile an example of using the CalloutBalloon class:

#include <boy.h>


class CalloutBalloon

{

protected:

Image BalloonImage;

Text BalloonText;


public :


CalloutBalloon ( string sText )

{

BalloonImage.SetImage ( "wcm/callout.svg" );

BalloonText.SetText ( sText );

BalloonText.SetColor ( "000000" );

SetPos ( 0, 0 );

}


void SetVisible ( bool bVisible )

{

BalloonImage.SetVisible ( bVisible );

BalloonText.SetVisible ( bVisible );

}


void SetPos ( double dX, double dY )

{

BalloonImage.SetPos ( dX, dY );

BalloonText.SetPos ( dX, dY - 20 ); // little above

}

};


void Scene1 ()

{

Image Back ( "backgrounds/ayersrock.svg" );

Back.SetVisible ();


Boy Max;

Max.SetVisible ( true );

Max.SetPos ( 300,290 );

Max.Says ( "I am going" );


CalloutBalloon SpeechBalloon ( "I am going" );

SpeechBalloon.SetVisible ( true );

SpeechBalloon.SetPos ( 150, 0 );


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


SpeechBalloon.SetPos ( - 450, 0 );

}

Try to compile the above cartoon to see the result:


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