Alex Rivera | Logout

How to write a simple class in C++?

Asked 2009-05-14T21:34:29.007
45

I have been reading a lot of tutorials on C++ class but they miss something that other tutorials include.

Can someone please show me how to write and use a very simple C++ class that uses visibility, methods and a simple constructor and destructor?

Edit
Report

1 Answer

10
class A
{
  public:
    // a simple constructor, anyone can see this
    A() {}
  protected:
    // a simple destructor. This class can only be deleted by objects that are derived from this class
    // probably also you will be unable to allocate an instance of this on the stack
    // the destructor is virtual, so this class is OK to be used as a base class
    virtual ~A() {}
  private:
    // a function that cannot be seen by anything outside this class
    void foo() {}
};
answered 2009-05-14T21:40:53.127

Your Answer