Main Menu


Sponsored Links

  


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

Operations on Objects

Most of the operators we have been using on other types, like mathematical operators ( +, -, etc.) and comparison operators (==, >, etc.), generally do not work on objects. It is possible to define the meaning of these operators for the new type (this is called operator overloading), but we won’t cover that in this book.

On the other hand, the assignment operator does work for objects. It can be used in two ways: (1) to initialize the instance variables of an object or (2) to copy the instance variables from one object to another. An initialization looks like this:

Point MyPoint = { 3.0, 4.0 };

The values in squiggly braces get assigned to the instance variables of the classes one by one, in order. So in this case, dX gets the first value and dY gets the second.

Unfortunately, this syntax can be used only in an initialization, not in an assignment statement. So the following is illegal:

Point MyPoint;

MyPoint = { 3.0, 4.0 }; // WRONG !!

It is legal to assign one object to another. For example:

Boy Max;

Point MyPoint = { 3.0, 4.0 };

Point AnotherPoint = MyPoint;

Max.SetPos ( AnotherPoint.dX, AnotherPoint.dY );

sets the character's coordinates to (3.0, 4.0).

Note/Caution: While assigning one object to another works fine for simple classes and is legal syntax, it is not a good idea to do this with more complex ones. Sometimes it is really not clear what to expect from such assignment. Here is one basic rule - please do not assign internal WCM objects (Image, Text and characters) to each other. While the following will compile:

Image First ( "backgrounds/landscape.svg" );

Image Second ( "sport/beach_ball.svg" );

First = Second;

it is not quite clear what the result will do in your script, because internal WCM C++ objects of this kind are just interfaces to the actual objects in your cartoon. After the assignment on third line we will have two interface definitions referring to the same actual image "sport/beach_ball.svg" and there will be no interface to work with first actual image "backgrounds/landscape.svg" anymore. This is probably not what you want to accomplish with the assignment!

As another example, we can write a function GetDistance which takes two point objects as parameters:

double GetDistance ( Point First, Point Second )

{

double dDX = Second.dX – First.dX;

double dDY = Second.dY – First.dY;

return Sqrt ( dDX * dDX + dDY * dDY );

}

Let's compile a small com plete example to demonstrate this. The example below will print the distance of a walking character from the starting point every second:

#include <boy.h>


class Point

{

public :

double dX, dY;

};


double GetDistance ( Point First, Point Second )

{

double dDX = Second.dX - First.dX;

double dDY = Second.dY - First.dY;

return Sqrt ( dDX * dDX + dDY * dDY );

}


// Note that the classes and function are defined before Scene1


void Scene1 ()

{

Point Start = { 300, 290 };

Point End = { -300, 290 };

Point Current;


Boy Max;

Max.SetVisible ();

Max.SetPos ( Start.dX, Start.dY );

Max.GoesTo ( End.dX, End.dY, 10 );


SetTime ( 0 );

Text Distance; // text will be assigned later

Distance.SetVisible ();

Distance.SetPos ( 0, -150 );


for ( int i=0; i< 10; i++ )

{

Current.dX = Max.GetX ();

Current.dY = Max.GetY ();


Distance.SetText ( "Distance: " +

GetDistance ( Start, Current ) );

Sleep ( 1 );

}

}

Here is what you should see after compilation:




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