Separation of Interface & Implementation: Dr. Abdul Haleem Butt

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Separation of interface &

implementation
Dr. Abdul Haleem Butt
Advantages
• User is only concerned about ways of accessing
data (interface)
• User has no concern about the internal
representation and implementation of the class
Separation of interface and implementation
• Usually functions are defined in implementation
files (.cpp) while the class definition is given in
header file (.h)
• Some authors also consider this as separation of
interface and implementation
Student.h
class Student{
int rollNo;
public:
void setRollNo(int aRollNo);
int getRollNo();

};
Student.cpp
#include “student.h”

void Student::setRollNo(int aNo){



}
int Student::getRollNo(){

}
Driver.cpp
#include “student.h”

int main(){
Student aStudent;
}
Abstract Data Type and Classes
• Separating the design details (that is, how the car’s engine works)
from its use is called abstraction.
• In other words, abstraction focuses on what the engine does and not
on how it works.
• Thus, abstraction is the process of separating the logical properties
from the implementation details.
• Driving the car is a logical property; the construction of the engine
constitutes the implementation details.
• We have an abstract view of what the engine does but are not
interested in the engine’s actual implementation.
Abstract Data Type
• A data type that separate's the logical properties from the
implementation details.
• Like any other data type, an ADT has three things associated with it:
• the name of the ADT, called the type name;
• the set of values belonging to the ADT, called the domain;
• and the set of operations on the data
Information Hiding
• The user is not concerned with the implementation details, we must
put those details in a separate file called an implementation file.
• Also, because the specification details can be too long, we must free
the user from having to include them directly in the program.
• The user must be able to look at the specification details so that he or
she can correctly call the functions, and so forth
• We must, therefore, put the specification details in a separate file.
The file that contains the specification details is called the header file
(or interface file).
Continue…
• We produce what is called the object code from the implementation
file.
• The user links the object code produced by the implementation file
with the object code of the program that uses the class to create the
final executable code.
• Finally, the header file has an extension .h, whereas the
implementation file has an extension .cpp.
• User then links the object code produced by the implementation file
with the object code of the program that uses the class to create the
final executable code
• Pre-condition: A statement specifying the condition(s) that must be
true before the function is called.
• Post-condition: A statement specifying what is true after the function
call is completed.
Executable Code
Example
Continue..

You might also like