LAB REPORT NO.
10
INTERFACES IN C++
PREPARED BY
HURR ABBAS
SUBMITTED TO
Ms. AYESHA
ROLL No. 5111323030
FIELD: BS-AI-II
SUBJECT: OOP
IBADAT INTERNATIONAL UNIVERSITY, ISLAMABAD
Department of Computer Science & Information Technology
Tasks:
1. Create an interface named Shape with a method getArea().Implement two classes, Circle and
Rectangle, both of which must implement the Shape interface. Assume appropriate data members for the
circle and rectangle.
#include <iostream>
#include <cmath>
using namespace std;
// Define the Shape interface
class Shape {
public:
virtual double getArea() const = 0; // Pure virtual function
};
// Implement the Circle class
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double getArea() const override {
return 3.14 * radius * radius;
}
};
// Implement the Rectangle class
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double getArea() const override {
return length * width;
}
};
int main() {
Shape* shapes[2];
shapes[0] = new Circle(5.0);
shapes[1] = new Rectangle(4.0, 6.0);
for (int i = 0; i < 2; ++i) {
cout << "Shape " << i + 1 << " area: " << shapes[i]->getArea() << endl;
}
// Clean up
delete shapes[0];
delete shapes[1];
return 0;
}
2. Define an interface named Payable with a method getPaymentAmount() returning a
double.Implement a hierarchy including classes Invoice, Employee, and SalariedEmployee, all
implementing the Payable interface. In the main program, call the getPaymentAmount() method
polymorphically. Implement the following hierarchy
Payable:
Double getPaymenyAmount();
Invoice:
Employee:
Salaried Employee:
In the runner , call the getPaymentAmount() method polymorphically.
#include <iostream>
#include <string>
using namespace std;
// Define the Payable interface
class Payable {
public:
virtual double getPaymentAmount() const = 0; // Pure virtual function
};
// Implement the Invoice class
class Invoice : public Payable {
private:
double amount;
public:
Invoice(double amt) : amount(amt) {}
double getPaymentAmount() const override {
return amount;
}
};
// Implement the Employee class
class Employee : public Payable {
protected:
string name;
double salary;
public:
Employee(const string& n, double s) : name(n), salary(s) {}
double getPaymentAmount() const override {
return salary;
}
};
// Implement the SalariedEmployee class
class SalariedEmployee : public Employee {
public:
SalariedEmployee(const string& n, double s) : Employee(n, s) {}
double getPaymentAmount() const override {
return salary;
}
};
int main() {
Payable* payables[3];
payables[0] = new Invoice(1500.75);
payables[1] = new Employee("Hurr Abbas", 2500.50);
payables[2] = new SalariedEmployee("Adeel Abbas", 3200.00);
for (int i = 0; i < 3; ++i) {
cout << "Payment amount " << i + 1 << ": " << payables[i]->getPaymentAmount() << endl;
}
// Clean up
delete payables[0];
delete payables[1];
delete payables[2];
return 0;
}
3. Create a class that implements the compare interface with a method compareObjects(Object o) re-
turning a boolean.
#include <iostream>
using namespace std;
// Define the Compare interface
class Compare {
public:
virtual bool compareObjects(const void* o1, const void* o2) const = 0; // Pure virtual function
};
// Implement a class that compares integers
class CompareIntegers : public Compare {
public:
bool compareObjects(const void* o1, const void* o2) const override {
return *(static_cast<const int*>(o1)) == *(static_cast<const int*>(o2));
}
};
int main() {
CompareIntegers comparer;
int a = 5, b = 5, c = 10;
cout << "a and b are " << (comparer.compareObjects(&a, &b) ? "equal" : "not equal") << endl;
cout << "a and c are " << (comparer.compareObjects(&a, &c) ? "equal" : "not equal") << endl;
return 0;
}
4. Define an interface called Enumeration with methods hasNext(int index) and getNext(int
index).Create a class NameCollection that implements Enumeration to store a collection of names using
an array of strings. Write constructors and methods for the NameCollection class to iterate through the
collection using the Enumeration interface. Write a main method to demonstrate this functionality. in C++
#include <iostream>
#include <string>
using namespace std;
// Define the Enumeration interface
class Enumeration {
public:
virtual bool hasNext(int index) const = 0; // Pure virtual function
virtual string getNext(int index) const = 0; // Pure virtual function
};
// Implement the NameCollection class
class NameCollection : public Enumeration {
private:
string names[10];
int size;
public:
NameCollection() : size(0) {}
void addName(const string& name) {
if (size < 10) {
names[size++] = name;
}
}
bool hasNext(int index) const override {
return index < size;
}
string getNext(int index) const override {
if (index < size) {
return names[index];
}
return "";
}
};
int main() {
NameCollection collection;
collection.addName("Ayesha");
collection.addName("Ali");
collection.addName("Hurr");
for (int i = 0; i < 10; ++i) {
if (collection.hasNext(i)) {
cout << "Name " << i + 1 << ": " << collection.getNext(i) << endl;
}
}
return 0;
}