You are on page 1of 43

Department IT

Submitted to Prof.Sir Naeem


Group#5
Bareera Anam 1497
Bilal raza 1494
Waqas 1492
Abu Bakkar 1499
Tamoor Akthar 1507

PRESENTATION
INTERFACE
 An interface is a description of the actions that an object can do.
 In OOP, an interface is a programming structure or syntax that allows the
computer to enforce certain properties on an object.
DEFINING AN INTERFACE
 An interface can contain one or multiple methods properties and events.
 But these are not implemented in the interface itself.
 These are defined in the class that implements the interface.
SYNTAX
Syntax for defining an interface:
Interface Interfacename
{
member declarations...
}

INTERFACE is a keyword.
EXTENDING AN INTERFACE
 Interfaces can be extended like classes.One interface can be sub interfaced from other interfaces.
 EXAMPLE:
 Interface addition
 {
 int add(int x,int y);
 }
 interface Compute:addition
 {
 int sub(int x,int y);
 }
SYNTAX
 Interface name2 extends name1
 {
 body;
 }
KEYPOINT:
 INTERFACES can extend only INTERFACES, they can't extend classes
PRESENTER#2
INTERFACE;
The c++ interfaces are implemented using abstract
classes .A class is made abstract by declaring at least one of its functions as
pural virtual function.
A pure virtual functions is specified by placing “=0” in its declaration.
Virtual double getVolume() =0;
INTERFACE AND ABSTRACT CLASS
• When there are some common features to be shared by all the objects then we go for
abstract class.
• When all the features are to be implemented differently for all the objects then we go for
tntefrace.
Class vehicle{
String wheel;
String petrol;
};

Class Twowheler Class three-Wheler


{ { Class four-Wheler
}; {
}; };
Class payment
{
Void payBil();
};

Class creditcard Class cheque


{ {
Class body; class body;
}; };
EXAMPLE:

#include<iostream>
using namespace std;
//base class
class moveable
{
public:
virtual void Show()=0;

};
//derived classes
class animal:public moveable
{
public:
void Show()
{
cout<<"animals has four legs...."<<endl;
}
};
class human:public moveable
{
public:
void Show()
{
cout<<"humans has two legs...."<<endl;
}
};
//main function
int main()
{
animal a;
human h;
a.Show();
h.Show();
return 0;
}
PRESENTER #3
ASSLAM-O-ALAIKUM
Name: Waqas Hussain
Roll No:bsf1601492
Bs(I.T)
3rd Semester
Topic of Presentation:
Explanation of interface with programs
EXAMPLE:
Lets see an example of interface in c++which has one abstract method draw().
Its implementation is provided by derived classes. Derived classes have different
Implementation.
#include<iostream>
using namespace std;
//base class
class shape
{
public:
virtual void draw()=0;
};
//derived class
class Rectangle:public shape
{
public:
void draw()
{
cout<<"Drawing Rectangle...."<<endl;
}
};
//2nd derived class
class circle:public shape
{
public:
void draw()
{
cout<<"drawing circle....."<<endl;
}
};
int main(){
//creating objects of derived classes
shape*obj;
Rectangle rec;
circle cir;
rec.draw();
cir.draw();
return 0;
}
PROGRAM:
Consider the following program where parent class provide an interface to the bass class to implement a function called getArea().
#include<iostream>
using namespace std;
//Base class
class Shape
{
protected:
int width;
int height;
public:
//pure virtual function providing interface framework.
virtual int getArea()=0;
void setWidth(int w)
{
void setWidth(int w)
{
width=w;
}
void setHeight(int h)
{
height=h;
}
};
//derived classes
class Rectangle:public Shape
{
public:
int getArea()
{
return(width*height);
}
};
class triangle:public Shape
{
public:
int getArea()
{
return(width*height)/2;
}
};
int main()
{
Rectangle rec;
triangle tri;
rec.setWidth(7);
rec.setHeight(5);
//print the area of the rectangle.
cout<<"total Rectangle area="<<rec.getArea()<<endl;
tri.setWidth(7);
tri.setHeight(5);
//print the area of the triangle.
cout<<"total Triangle area="<<tri.getArea()<<endl;
return 0;
}
PRESENER#4
NAME MUHAMMAD ABU BAKKAR
ROLL NO1499
BSIT 3RD SMESTER
BENEFITTS OF INTERFACES

Having explained the technique of hoisting a class


interface, I need to explain why developers should be
interested in doing this. There are three point
BENEFITS

1. Hoisting the (common) interface of classes in a run-time


polymorphic affords a clear separation of interface from
implementation. Further, doing so help to underpin the
use of abstraction, Because the interface class expresses
only the capabilities of the abstracted entity.
BENEFITS

2.It follows on from the above, that new implementations


can be added without changing exiting code. For example,
It is most likely that drawing will initially have only one
implementation class, But because other code is dependent
only on its interface class, new implementations can easily
be added in the future.
BENEFITS

3. Consider the physical structure of C++ code with regard to


the interface class, its implementation classes, and classes
(such as drawing) that use it. Assuming common C++ practice
is followed, the definition of shape will have a header file-
let’s assume it’s called shape. hpp - all to it self, as will
drawing (i.e. drawing . Hpp, using the same convention).
BENEFITS

Now, owing to the physical structure of C++ (that is, the


structure it inherited from C), if anything in the shape. Hpp
header file is changed, anything that depends on it-such as
drawing. Hpp – must recompile. In large systems where
build times are measured in hours (or even days), this can
be a significant overhead.
BENEFITS

However, because shape is an interface class, drawing


(For example) has no physical dependency on any of the
implementation detail, and it is in the implementation
detail that change is likely to occur ( assuming some
thought has been put into the design of shape’s interface).
 Presenter #5.
ASSALAMO-O-ALAIKUM
NAME: MIAN TAMOOR AKTHAR
ROLL NO: BSF1601507.
CLASS BS(I.T)
SEMESTER 3RD
TOPIC OF PRESENTATION:
GOALS OF INTERFACES
GOALS OF INTERFACES:
• They allow the programmer to be more abstract when refErencing
objects (for example, car vehicle: vehicle, can reference any car, truck
etc…anything that is vehicle(and not care what type it is.)
This occurs at “program time”.
• When the vehicle start engine () function is invoked, the correct function
is associated with the real object is actually used this occur at run time.
• They require the programmer to create specific function that are expected in
an implementing class when it implements an interface.
• Again this allow all the object in a “set” of like objects to be treated based on
the “high level” type of the set, rather than on the specific type of the
individual object.
PROGRAM:
#include<iostream>
using namespace std;
class vehicle
{
public:
virtual void Vehicle()=0;
};
//derived classes
class car:public vehicle
{
public:
void Vehicle()
{
cout<<"this is a vehicle...."<<endl;
}
};
class truck:public vehicle
{
public:
void Vehicle()
{
cout<<"this is also a veicle..."<<endl;
}
};
//main function
int main()
{
car c;
//main function
int main()
{
car c;
truck t;
c.Vehicle();
t.Vehicle();
return 0;
}

You might also like