Roman
Roman

Reputation: 131208

Inheritance syntax in C++

I understand the syntax of inheritance in C++:

class DerivedClassName : public BaseClassName {}

However, in a program I found a string like that:

class ComplexNumberTest : public CppUnit::TestCase {

and I do not understand what it means. It is clear that ComplexNumberTest is subclass of CppUnit but what TestCase does their?

I think that CppUnit::TestCase means TestCase method of CppUnit class but then DerivedClassName should be a subclass of a method?

Could anybody please help me with that?

Upvotes: 3

Views: 541

Answers (2)

billz
billz

Reputation: 45420

CppUnit is namespace, ComplexNumberTest is a derived class of TestCase from CppUnit namespace.

In your code, you have TestCase in this way:

namespace CppUnit
{
  class TestCase
  {
    // blah blah
  };
}

Or it TestCase could be a nested class(type) inside CppUnit with public access(thanks to PeterWood)

class CppUnit
{
public:
  class TestCase
  {
    // blah blah
  };
};

class ComplexNumberTest : public CppUnit::TestCase
{
   // also blah
};

Upvotes: 7

tuxtimo
tuxtimo

Reputation: 2790

CppUnit is a namepsace or TestCase is a nested class in CppUnit.

If it is a namespace: You can get rid of this syntax, by using the namespace:

using namespace CppUnit;

class ComplexNumberTest : public TestCase {

Although, you don't usually want to put using namespace in a header file. - Thank for comment @PeterWood

Upvotes: 2

Related Questions