When a new class is derived from an existing class, the derived class member functions _____ have names that differ from base class function names

may
may if the two classes have the same name
must
must not

The correct answer is: A. may

A derived class member function may have names that differ from base class function names. This is because the derived class can override the base class function. When a derived class overrides a base class function, the derived class function has the same name as the base class function, but it has a different signature. The signature of a function is the combination of its name, its parameter list, and its return type.

For example, the following code shows a class named Base with a member function named f():

class Base {
public:
void f() {
std::cout << "Base::f()\n";
}
};

The following code shows a class named Derived that inherits from Base. The Derived class has a member function named f() that overrides the Base class’s f() function:

class Derived : public Base {
public:
void f() {
std::cout << "Derived::f()\n";
}
};

When you call the f() function on an object of type Derived, the Derived class’s f() function is called, not the Base class’s f() function.

Here is a brief explanation of each option:

  • A. may. A derived class member function may have names that differ from base class function names. This is because the derived class can override the base class function.
  • B. may if the two classes have the same name. This option is incorrect because the derived class’s member function name does not have to be the same as the base class’s member function name even if the two classes have the same name.
  • C. must. This option is incorrect because the derived class’s member function name does not have to be the same as the base class’s member function name.
  • D. must not. This option is incorrect because the derived class’s member function name can be the same as the base class’s member function name.
Exit mobile version