Google
 

9/21/08

C++ Virtual function

Purpose

The concept of the virtual function solves the following problem:

In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous.

The distinction between virtual and not virtual resolves this ambiguity. If the function in question is designated "virtual" in the base class then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.

Virtual functions overcome the problems with the type-field solution by allowing the programmer to declare functions in a base class that can be redefined in each derived class.

Example

For example, a base class Animal could have a virtual function eat. Subclass Fish would implement eat() differently than subclass Wolf, but you can invoke eat() on any class instance referred to as Animal, and get the eat() behavior of the specific subclass.

This allows a programmer to process a list of objects of class Animal, telling each in turn to eat (by calling eat()), with no knowledge of what kind of animal may be in the list. You also do not need to have knowledge of how each animal eats, or what the complete set of possible animal types might be.

The following is an example in C++:

#include 
using namespace std;

class Animal
{
public:
virtual void eat() { cout << "I eat like a generic Animal." << class="br0">}
};

class Wolf : public Animal
{
public:
void eat() { cout << "I eat like a wolf!" << class="br0">}
};

class Fish : public Animal
{
public:
void eat() { cout << "I eat like a fish!" << class="br0">}
};

class GoldFish : public Fish
{
public:
void eat() { cout << "I eat like a goldfish!" << class="br0">}
};


class OtherAnimal : public Animal
{
};

int main()
{
Animal* anAnimal[5];

anAnimal[0] = new Animal();
anAnimal[1] = new Wolf();
anAnimal[2] = new Fish();
anAnimal[3] = new GoldFish();
anAnimal[4] = new OtherAnimal();

for (int i = 0; i < 5; i++) {
anAnimal[i]->eat();
delete anAnimal[i];
}

return 0;
}

Output with the virtual method Animal::eat():

I eat like a generic Animal.
I eat like a wolf!
I eat like a fish!
I eat like a goldfish!
I eat like a generic Animal.

Output if Animal::eat() were not declared as virtual:

I eat like a generic Animal.
I eat like a generic Animal.
I eat like a generic Animal.
I eat like a generic Animal.
I eat like a generic Animal.

Pure virtual function and virtual function
from here

Finally, we arrive at the end of our long journey through inheritance! This is the last topic we will cover on the subject. So congratulations in advance on making it through the hardest part of the language!

Pure virtual (abstract) functions and abstract base classes

So far, all of the virtual functions we have written have a body (a definition). However, C++ allows you to create a special kind of virtual function called a pure virtual function (or abstract function) that has no body at all! A pure virtual function simply acts as a placeholder that is meant to be redefined by derived classes.

To create a pure virtual function, rather than define a body for the function, we simply assign the function the value 0.

  1. class Base
  2. {
  3. public:
  4. const char* SayHi() { return "Hi"; } // a normal non-virtual function
  5. virtual const char* GetName() { return "Base"; } // a normal virtual function
  6. virtual int GetValue() = 0; // a pure virtual function
  7. };
class Base { public: const char* SayHi() { return "Hi"; } // a normal non-virtual function virtual const char* GetName() { return "Base"; } // a normal virtual function virtual int GetValue() = 0; // a pure virtual function };

When we add a pure virtual function to our class, we are effectively saying, “it is up to the derived classes to implement this function”.

Using a pure virtual function has two main consequences: First, any class with one or more virtual functions becomes an abstract base class, which means that it can not be instantiated! Consider what would happen if we could create an instance of Base:

  1. int main()
  2. {
  3. Base cBase; // pretend this was legal
  4. cBase.GetValue(); // what would this do?
  5. }
int main() { Base cBase; // pretend this was legal cBase.GetValue(); // what would this do? }

Second, any derived class must define a body for this function, or that derived class will be considered an abstract base class as well.

Let’s take a look at an example of a pure virtual function in action. In a previous lesson, we wrote a simple Animal base class and derived a Cat and a Dog class from it. Here’s the code as we left it:

  1. #include
  2. class Animal
  3. {
  4. protected:
  5. std::string m_strName;
  6. // We're making this constructor protected because
  7. // we don't want people creating Animal objects directly,
  8. // but we still want derived classes to be able to use it.
  9. Animal(std::string strName)
  10. : m_strName(strName)
  11. {
  12. }
  13. public:
  14. std::string GetName() { return m_strName; }
  15. virtual const char* Speak() { return "???"; }
  16. };
  17. class Cat: public Animal
  18. {
  19. public:
  20. Cat(std::string strName)
  21. : Animal(strName)
  22. {
  23. }
  24. virtual const char* Speak() { return "Meow"; }
  25. };
  26. class Dog: public Animal
  27. {
  28. public:
  29. Dog(std::string strName)
  30. : Animal(strName)
  31. {
  32. }
  33. virtual const char* Speak() { return "Woof"; }
  34. };
#include class Animal { protected: std::string m_strName; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal(std::string strName) : m_strName(strName) { } public: std::string GetName() { return m_strName; } virtual const char* Speak() { return "???"; } }; class Cat: public Animal { public: Cat(std::string strName) : Animal(strName) { } virtual const char* Speak() { return "Meow"; } }; class Dog: public Animal { public: Dog(std::string strName) : Animal(strName) { } virtual const char* Speak() { return "Woof"; } };

We’ve prevented people from allocating objects of type Animal by making the constructor protected. However, there’s one problem that has not been addressed. It is still possible to create derived classes that do not redefine Speak(). For example:

  1. class Cow: public Animal
  2. {
  3. public:
  4. Cow(std::string strName)
  5. : Animal(strName)
  6. {
  7. }
  8. // We forgot to redefine Speak
  9. };
  10. int main()
  11. {
  12. Cow cCow("Betsy");
  13. cout << class="string">" says " <<>
  14. }
class Cow: public Animal { public: Cow(std::string strName) : Animal(strName) { } // We forgot to redefine Speak }; int main() { Cow cCow("Betsy"); cout <<>This will print:

Betsy says ???

What happened? We forgot to redefine Speak, so cCow.Speak() resolved to Animal.Speak(), which isn’t what we wanted.

A better solution to this problem is to use a pure virtual function:

  1. #include
  2. class Animal
  3. {
  4. protected:
  5. std::string m_strName;
  6. public:
  7. Animal(std::string strName)
  8. : m_strName(strName)
  9. {
  10. }
  11. std::string GetName() { return m_strName; }
  12. virtual const char* Speak() = 0; // pure virtual function
  13. };
#include class Animal { protected: std::string m_strName; public: Animal(std::string strName) : m_strName(strName) { } std::string GetName() { return m_strName; } virtual const char* Speak() = 0; // pure virtual function };

There are a couple of things to note here. First, Speak() is now a pure virtual function. This means Animal is an abstract base class, and can not be instantiated. Consequently, we do not need to make the constructor protected any longer (though it doesn’t hurt). Second, because our Cow class was derived from Animal, but we did not define Cow::Speak(), Cow is also an abstract base class. Now when we try to compile this code:

  1. class Cow: public Animal
  2. {
  3. public:
  4. Cow(std::string strName)
  5. : Animal(strName)
  6. {
  7. }
  8. // We forgot to redefine Speak
  9. };
  10. int main()
  11. {
  12. Cow cCow("Betsy");
  13. cout << class="string">" says " <<>
  14. }
class Cow: public Animal { public: Cow(std::string strName) : Animal(strName) { } // We forgot to redefine Speak }; int main() { Cow cCow("Betsy"); cout <<>The compiler will give us a warning because Cow is an abstract base class and we can not create instances of abstract base classes:

C:\\Test.cpp(141) : error C2259: 'Cow' : cannot instantiate abstract class due to following members: C:\Test.cpp(128) : see declaration of 'Cow' C:\\Test.cpp(141) : warning C4259: 'const char *__thiscall Animal::Speak(void)' : pure virtual function was not defined

This tells us that we will only be able to instantiate Cow if Cow provides a body for Speak().

Let’s go ahead and do that:

  1. class Cow: public Animal
  2. {
  3. public:
  4. Cow(std::string strName)
  5. : Animal(strName)
  6. {
  7. }
  8. virtual const char* Speak() { return "Moo"; }
  9. };
  10. int main()
  11. {
  12. Cow cCow("Betsy");
  13. cout << class="string">" says " <<>
  14. }
class Cow: public Animal { public: Cow(std::string strName) : Animal(strName) { } virtual const char* Speak() { return "Moo"; } }; int main() { Cow cCow("Betsy"); cout <<>Now this program will compile and print:

Betsy says Moo

A pure virtual function is useful when we have a function that we want to put in the base class, but only the derived classes know what it should return. A pure virtual function makes it so the base class can not be instantiated, and the derived classes are forced to define these function before they can be instantiated. This helps ensure the derived classes do not forget to redefine functions that the base class was expecting them to.

Interface classes

An interface class is a class that has no members variables, and where all of the functions are pure virtual! In other words, the class is purely a definition, and has no actual implementation. Interfaces are useful when you want to define the functionality that derived classes must implement, but leave the details of how the derived class implements that functionality entirely up to the derived class.

Interface classes are often named beginning with an I. Here’s a sample interface class:

  1. class IErrorLog
  2. {
  3. virtual bool OpenLog(const char *strFilename) = 0;
  4. virtual bool CloseLog() = 0;
  5. virtual bool WriteError(const char *strErrorMessage) = 0;
  6. };
class IErrorLog { virtual bool OpenLog(const char *strFilename) = 0; virtual bool CloseLog() = 0; virtual bool WriteError(const char *strErrorMessage) = 0; };

Any class inheriting from IErrorLog must provide implementations for all three functions in order to be instantiated. You could derive a class named FileErrorLog, where OpenLog() opens a file on disk, CloseLog() closes it, and WriteError() writes the message to the file. You could derive another class called ScreenErrorLog, where OpenLog() and CloseLog() do nothing, and WriteError() prints the message in a pop-up message box on the screen.

Now, let’s say you need to write some code that uses an error log. If you write your code so it includes FileErrorLog or ScreenErrorLog directly, then you’re effectively stuck using that kind of error log. For example, the following function effectively forces callers of MySqrt() to use a FileErrorLog, which may or may not be what they want.

  1. double MySqrt(double dValue, FileErrorLog &cLog)
  2. {
  3. if (dValue <>
  4. {
  5. cLog.WriteError("Tried to take square root of value less than 0");
  6. return 0.0;
  7. }
  8. else
  9. return dValue;
  10. }
double MySqrt(double dValue, FileErrorLog &cLog) { if (dValue <>A much better way to implement this function is to use IErrorLog instead:

  1. double MySqrt(double dValue, IErrorLog &cLog)
  2. {
  3. if (dValue <>
  4. {
  5. cLog.WriteError("Tried to take square root of value less than 0");
  6. return 0.0;
  7. }
  8. else
  9. return dValue;
  10. }
double MySqrt(double dValue, IErrorLog &cLog) { if (dValue <>Now the caller can pass in any class that conforms to the IErrorLog interface. If they want the error to go to a file, they can pass in an instance of FileErrorLog. If they want it to go to the screen, they can pass in an instance of ScreenErrorLog. Or if they want to do something you haven’t even thought of, such as sending an email to someone when there’s an error, they can derive a new class from IErrorLog (eg. EmailErrorLog) and use an instance of that! By using IErrorLog, your function becomes more independent and flexible.

Interface classes have become extremely popular because they are easy to use, easy to extend, and easy to maintain. In fact, some modern languages, such as Java and C#, have added an “interface” keyword that allows programmers to directly define an interface class without having to explicitly mark all of the member functions as abstract. Furthermore, although Java and C# will not let you use multiple inheritance on normal classes, they will let you multiply inherit as many interfaces as you like. Because interfaces have no data and no function bodies, they avoid a lot of the traditional problems with multiple inheritance while still providing much of the flexibility.


Using rsync to synchronize the files.

rsync -autv username@xxx.xxx.xxx.edu:/source_directory/ ./dest_directory

Use "-autv" options. This will make sure that if the files in the dest_directory is newer, it will not be overwritten.
"t" will preserve the time stamp.
"a" archive
"u" update; skip files that are newer on the receiver
"v" print out information

Another thing needs to be careful is that "/" after source_directory is needed if you want to synchronize the files inside source_directory. If "/" is ignored, then source_directory will be copied to dest_directory.

For example: If you have source_directory inside which you have source1, source2 two files. Now you want to synchronize the files with your directory "dest_dir" which at this moment is empty.

> rsync -autv ./source_directory/ ./dest_dir
The above command will copy source1 and source2 to ./dest_dir (since at this moment ./dest_dir is still empty, so the files inside ./source_directory is newer).

What if you want to exclude ./tmp directory?
> rsync -autv --exclude "tmp/" ./source_directory/ ./dest_dir
temp
is relative to the directory you are trying to backup, which is ./source_directory/ here.


> rsync -autv ./source_directory ./dest_dir
However, the above command will copy ./source_directory to ./dest_dir. It copy it as a directory. After the command, inside your ./dest_dir you will have another folder called "source_directory".

9/16/08

42 of the Best Free Linux Graphics Software

42 of the Best Free Linux Graphics Software

The original post is here


Linux is a very strong platform for budding artists, photographers, animators, and designers. With inexpensive hardware, free software, and a modicum of talent and inspiration, anyone can create professional-looking computer graphics.

There is a huge range of Open Source software available to create, modify and convert 2D and 3D computer graphics. To provide an insight into the software that is available, we have compiled a list of 42 high quality Linux graphics applications. The vast majority of the software featured here sports an attractive graphical front-end, although we have not neglected console based applications.


Now, let's explore the 42 graphics applications at hand. For each title we have compiled its own portal page, providing a screenshot of the software in action, a full description with an in-depth analysis of its features, together with links to relevant resources and reviews.

3D Modeling and Rendering
Art of Illusion 3D-modeler, renderer and raytracer
Blender Very fast and versatile 3D modeler/renderer
K-3D 3D modeling and animation system
Pixie Photorealistic raytracing renderer for generating photorealistic images
POV-Ray Full-featured ray tracer
Radiance Suite of tools for performing lighting simulation
Sunflow Rendering system for photo-realistic image synthesis
YafRay Powerful raytracer


Raster Graphics Editor
CinePaint Painting, manipulation and image processing
GIMP GNU Image Manipulation Program
Gimpshop Fork of GIMP mimicing Adobe Photoshop's interface
Krita Edit and paint images


Vector Graphics Editor
Inkscape Vector-based drawing program
OpenOffice.org Draw Produces simple diagrams, flowcharts and 3-D artwork
Xara Xtreme Heavyweight vector graphics, illustration and DTP program


Animation
Pencil Create traditional hand-drawn animation using bitmap and vector graphics
Ktoon Produce professional cartoons
Synfig Create and edit 2D animations and compositions


Drawing
ArgoUML UML modelling tool
Dia Diagram editor
Kivio Flowcharting program for the KDE Office Suite
Skencil Interactive vector drawing program


Photo Management
blueMarine Digital photo manipulation software
F-Spot Personal photo management application


Viewers
Cornice Designed to be a free replacement of ACDSee
gThumb Advanced image viewer and browser
Gwenview Simple image viewer for KDE 4
Eye of Gnome Fast and functional image viewer
Imgv Powerful image viewer


Camera
Cheese Take pictures and videos from your webcam
digiKam Digital photo management application for KDE
gphoto2 Set of digital camera software applications
Rawstudio Raw-image converter and manipulation


Scanning
Kooka Scanner program for KDE
Tesseract Optical Character Recognition Engine
XSane Featureful graphical frontend for SANE


CAD
QCad Professional CAD System
BRL-CAD Constructive Solid Geometry (CSG) solid modeling system


Fractals
Gnofract 4D
GNOME based fractal generator
XaoS Real-time interactive fractal zoomer


Utilities
Agave Colorscheme designer for the GNOME desktop
ImageMagick Software suite to create, edit, and compose bitmap images