Professional Documents
Culture Documents
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;
};
#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