answersLogoWhite

0


Best Answer

The interface of a class is defined as being the methods that allow the outside world to interact with objects instantiated from that particular class type. That interface may be declared within the class itself or it may be inherited from base classes, or it may be some combination of the two. The interface may also be virtual or even pure-virtual, either in whole or in part.

The implementation determines how that interface operates. Although the interface and the implementation go hand-in-hand they are treated separately because a derived class can override the generic interface exposed by the base class in order to provide a more specialised implementation of the base class interface, as well as provide new interfaces of its own.

The generic interface needn't be virtual in order to be overridden, but it must be virtual in order to allow polymorphic behaviour. Where the base class cannot provide an implementation because it is intentionally abstract and therefore relies solely upon a derived implementation, it will provide a pure-virtual interface that absolutely must be implemented by all derivatives. If a class declares or inherits a pure-virtual interface with no implementation, that class automatically becomes abstract. Only classes that provide a complete implementation for all pure-virtual interfaces (including any generic implementations inherited from base classes derived from the base class), can actually be instantiated.

When calling a base class method from a non-member function, one would rightly expect the implementation of the derived class to be executed. In order to achieve this, the generic interface must be virtual and the virtual table ensures the call is routed to the correct implementation. Without this, only the base class method can be called, which may result in unexpected behaviour in the derived class since the base class would be unaware of its specific behaviour.

While it is possible to allow a base class to gain access to a derived class interface without exposing a virtual interface, this can only be done when the base class already knows exactly what type of derivative to expect. Knowing the type of its derivative allows the base class to make an explicit dynamic cast of itself in order to call the appropriate interface. But it cannot predict the future: if you derive a new class of object that it knowns nothing about, it cannot gain access to its specialised interface. Even dynamic casting is impossible without knowing what type of object to cast to. Virtual interfaces overcome this problem by ensuring that no matter what derivatives are created either now or in the future, the base class can access each of their specific implementations through just one generic interface, without the need for any dynamic casting (which is almost always a sign of poor design).

When a base class interface is routed to a derived class implementation, that specific implementation has complete access to the derived class' interface. Thus interfaces that the base class doesn't even know exist can be implemented via calls through the base class' virtual interface which is the only interface the base class really needs to know anything about. To put it another way, all derivatives of the same base class can be treated just as if they were the base class itself, with a common, generic interface, and yet each instance can exhibit entirely different behaviour, according to the specific implementations defined by each derivative. That is, one interface/multiple implementations.

Derived class implementations may also, optionally, call any base class method and access any base class variable (other than the base class' private members, of course). This makes it possible to provide a generic implementation of the virtual interface that can then be augmented or replaced completely by the more specific implementations of the derived interface. Even pure-virtual methods, which are normally not implemented in an abstract base class, can provide a generic implementation where appropriate. Although the implementation cannot be inherited, the interface must be inherited because it is virtual. However, once a derived class implements a pure-virtual interface, that implementation can then be inherited by its derivatives, to be further specialised and or augmented as necessary.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is meant by interface of a class and implementation of a class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Interface in C plus plus?

Unless by "interface" you mean a user interface...C++ does not have interfaces per se, at least not in the same sense as, say, Java. In a C++ class, a function declared pure virtual makes the class non-instantiable and forces derived classes that want to be instantiable to provide an implementation. This has exactly the same effect as the interface concept of Java. So in C++, interface is just a synonym for abstract base class.


What is the term that describes the hiding of implementation details of objects from each other in a C plus plus class?

Encapsulation.


What are the two parts of class specification in c plus plus programming?

There are more than two, but the class name and interface would be the minimum specification for any class. The interface can be further divided into public, protected and private access, each of which may contain member methods and/or member variables, which may themselves be static and/or non-static. The last part of the specification are the friend declarations, if required.


Write a c plus plus program to create a class car and aggregate it with wheel and engine parts?

You'll need to fill in the details, but the following framework will get you started. class Car { private: class Engine {}; // implementation omitted for brevity class Wheel {}; // implementation omitted for brevity Wheel m_front_wheel[2]; // array of two front wheels Wheel m_rear_wheel[2]; // array of two rear wheels Engine m_engine; // an engine }; int main() { Car ferrari; Car bmw; Car smart_car; return( 0 ); }


How do you create a class complex in c plus plus and overload the following operators plus minus multiplication and division?

The C++ standard library provides a class complex in <complex>, including all the standard arithmetic operator overloads. It is highly unlikely that a user-defined implementation will improve upon any of the existing implementations, however the source code is worthy of study, particularly if you wish to extend the implementation in order to cater for a user-defined scalar; all the built-in scalars are already catered for.

Related questions

When you define a c plus plus class what items are considered part of the interface?

The interface of a C++ class is the public methods and attributes that are exposed by the class. In a pure abstract base class, the interface is enforced by the compiler in each of the derived child classes.


Interface in C plus plus?

Unless by "interface" you mean a user interface...C++ does not have interfaces per se, at least not in the same sense as, say, Java. In a C++ class, a function declared pure virtual makes the class non-instantiable and forces derived classes that want to be instantiable to provide an implementation. This has exactly the same effect as the interface concept of Java. So in C++, interface is just a synonym for abstract base class.


Why you use public mode in c plus plus?

To expose an interface to the class members. Without an interface of some kind, an object would be useless.


What is the term that describes the hiding of implementation details of objects from each other in a C plus plus class?

Encapsulation.


When does c plus plus use generic function implicitly?

C++ uses the generic function implicitly whenever the base class implementation (the generic method) is also the most-derived implementation.


In C plus plus Interface is also known as?

I guess you mean Java, there is no interface in C++.


Difference between SQL plus and ISQL plus?

SQLPlus is a command-line interface program used to access and manage Oracle databases, while iSQLPlus is a web-based version of SQLPlus that allows users to access Oracle databases through a web browser. iSQLPlus provides a more user-friendly interface compared to the command-line SQL*Plus.


What are the two parts of class specification in c plus plus programming?

There are more than two, but the class name and interface would be the minimum specification for any class. The interface can be further divided into public, protected and private access, each of which may contain member methods and/or member variables, which may themselves be static and/or non-static. The last part of the specification are the friend declarations, if required.


Write a c plus plus program to create a class car and aggregate it with wheel and engine parts?

You'll need to fill in the details, but the following framework will get you started. class Car { private: class Engine {}; // implementation omitted for brevity class Wheel {}; // implementation omitted for brevity Wheel m_front_wheel[2]; // array of two front wheels Wheel m_rear_wheel[2]; // array of two rear wheels Engine m_engine; // an engine }; int main() { Car ferrari; Car bmw; Car smart_car; return( 0 ); }


How can you hide the data in a class in C plus plus?

If this is a homework assignment, please consider trying to answer it yourself first, otherwise the value of the reinforcement of the lesson offered by the assignment will be lost on you.To hide the data in a class in C++, simply declare the data private, and then manipulate the data using the public interface of the class.


How do you create a class complex in c plus plus and overload the following operators plus minus multiplication and division?

The C++ standard library provides a class complex in <complex>, including all the standard arithmetic operator overloads. It is highly unlikely that a user-defined implementation will improve upon any of the existing implementations, however the source code is worthy of study, particularly if you wish to extend the implementation in order to cater for a user-defined scalar; all the built-in scalars are already catered for.


How much does c plus plus cost in al?

The cost depends on which implementation you intend to purchase. There are several free implemenations, including the generic gcc implementation from SourceForge.