| | |
What is the point in virtual things???
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
When you mark a method of your class as virtual you give the user of your class the possibility to override that method. If you don't, the metod can't be overridden.
The point of virtual member functions is that they allow functionality to be specified in a base class, and specialised in a derived class. For example, a generic Shape class might declare a virtual function named Area() to compute it's area, and another virtual function called Name(). Derived classes (which represent particular shapes) override those virtual function to give appropriate results for particular shapes.
C++ Syntax (Toggle Plain Text)
class Shape { public: Shape(); virtual double Area() const = 0; virtual std::string Name() const = 0; }; class Triangle: public Shape { public: Triangle(double h, double w) : height(h), width(w) {}; double Area() const {return 0.5*height*width;}; std::string Name() const {return "Triangle";} private: double height, width; }; class Circle : public Shape { public: Circle(double r): radius(r) {}; virtual double Area() const {return 4.0*atan(1.0)*radius*radius;}; virtual std::string Name() const {return "Circle";} private: double radius; };
C++ Syntax (Toggle Plain Text)
void PrintArea(const Shape &s) { std::cout << s.Name() << " - Area = " << s.Area() << '\n'; }
This is described as polymorphic behaviour: the class Shape is a polymorphic base class, derived classes modify it's behaviour, but the interface seen by the PrintArea() function is the same for all the classes. The PrintArea() function only receives generic information about a Shape (i.e. it provides a Name() and Area() member function) but, at run time, the behaviours correspond to the actual type of the object passed.
For example;
C++ Syntax (Toggle Plain Text)
int main() { Triangle tri(2.0, 4.0); Circle cir(5.0); PrintArea(tri); // will print "Triangle - Area = 4" PrintArea(cir); // will print "Circle - Area = 78.5" (possibly higher precision value) Shape *s[2]; s[1] = ○ s[0] = &tri; // this loop will print out the above lines of output in reverse order for (int i = 0; i < 2; ++i) PrintArea(s[i]); }
Last edited by grumpier; Dec 15th, 2008 at 4:07 am.
![]() |
Similar Threads
- C++ Performance Tips (C++)
- Please help with Pointers, Classes, Virtual Functions, and Abstract Classes (C++)
- Virtual reailty (Geeks' Lounge)
- What type of linux to use?? (Getting Started and Choosing a Distro)
- C++ tips (C++)
- Please Help!!! MOD function (ASP)
- Network over the internet (Networking Hardware Configuration)
- Graphics In Pixel: Part II B (C)
- Browser's Language Interpreters (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: deactivate JIT debugger
- Next Thread: C++ help :(
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






