OOP hierarchy
OOP hierarchy
Object-Oriented BSCS-
Programming
Assignment No. 4 (CLO3) 2A
Due Date:1st Dec,
Instructor: Saima Jawad 2024
Total Marks: 50
Sportsman
Footballer Cricketer
Bowler Batsman
Allrounder
Requirements:
Submission:
Problem Statement:
Create a polymorphic class hierarchy in C++ to represent different types of players (Football,
Cricketer, etc.). The hierarchy should allow for the calculation of average performance metrics
(goals/scores) and the ranking of players based on these metrics.
Objectives:
Design a flexible and extensible class hierarchy using inheritance and polymorphism.
Implement the computeavg () function to dynamically calculate average performance.
Overload the << and >> operators for input/output operations.
Read player data from a text file.
Display the player ranking table in a clear and organized format.
UML Class Diagram:
Code:
We'll implement each class in separate .h and .cpp files. Here's a simplified structure:
#include<iostream>
#include<string>
using namespace std;
class Sportsman {
protected:
string name;
int matches;
public:
Sportsman(string n=" ",int m=0);
virtual void computeavg()=0;
virtual void display() const;
virtual ~Sportsman();
friend ostream& operator<<(ostream& out,const Sportsman& s);
friend istream& operator>>(istream& in, Sportsman& s);
};
#endif
#include "sportsman.h"
class Footballer : public Sportsman {
private:
int goals;
public:
Footballer(string n = "", int m = 0, int g = 0);
void computeavg() override;
void display() const override;
};
#endif
void Footballer::computeavg() {
cout << "Average Goals per Match: " << (matches > 0 ? (float)goals / matches : 0) << endl;
}
#include "Sportsman.h"
public:
Cricketer(string n = "", int m = 0, int r = 0, int w = 0);
virtual void computeavg() override;
};
#endif
void Cricketer::computeavg() {
cout << "Average Runs: " << (matches > 0 ? (float)runs / matches : 0) << endl;
}
#include "Cricketer.h"
#endif
void Bowler::computeavg() {
cout << "Average Wickets per Match: " << (matches > 0 ? (float)wickets / matches : 0) << endl;
}
#include "Cricketer.h"
#endif
void Batsman::computeavg() {
cout << "Average runs per Match: " << (matches > 0 ? (float)runs / matches : 0) << endl;
}
#include "Cricketer.h"
public:
Allrounder(std::string n = "", int m = 0, int r = 0, int w = 0);
void computeavg() override;
void display() const override;
};
#endif
void Allrounder::computeavg() {
float battingAverage = (matches > 0) ? (float)runs / matches : 0;
float bowlingAverage = (matches > 0) ? (float)wickets / matches : 0;
cout << "Batting Average: " << battingAverage << ", Bowling Average: " << bowlingAverage << endl;
}
Main.cpp
#include <iostream>
#include <fstream>
#include "Footballer.h"
#include "Bowler.h"
#include "Batsman.h"
#include "Allrounder.h"
int main() {
Sportsman* player;
ifstream inFile("players.txt");
string name;
int matches, goals, runs, wickets;
inFile.close();
inFile.open("cricketers.txt");
while (inFile >> name >> matches >> runs >> wickets) {
if (wickets == 0) {
player = new Batsman(name, matches, runs);
player->display();
player->computeavg();
delete player;
}
else if (runs == 0) {
player = new Bowler(name, matches, wickets);
player->display();
player->computeavg();
delete player;
}
else {
player = new Allrounder(name, matches, runs, wickets);
player->display();
player->computeavg();
delete player;
}
}
inFile.close();
return 0;
}
Explanation:
1. Polymorphic Class Hierarchy:
The Player class serves as the base class, providing a common interface for different types of
players. Derived classes like FootballPlayer and Cricketer inherit from Player and implement
their specific computeavg(), readDataFromFile(), and display() methods.
2. Dynamic Binding:
The computeavg() function is declared as virtual in the base class, allowing for dynamic
binding at runtime. This ensures that the correct implementation is called based on the actual
object type.
3. Operator Overloading:
The << and >> operators can be overloaded to provide custom input and output behavior for
player objects.
4. File Input:
The readDataFromFile() function can be implemented to read player data from a text file,
parsing the information and creating appropriate player objects.
5. Ranking and Display:
The main function reads player data, computes averages, sorts players based on their average
performance, and displays the ranking table using the overloaded << operator.
Conclusion:
This implementation effectively demonstrates the use of object-oriented principles to create a flexible
and extensible class hierarchy for representing different types of players. The use of polymorphism,
inheritance, and operator overloading allows for efficient and concise code. The ranking table
provides a clear and informative representation of the players' performance.
Please Note:
Report should be well documented ◆•^-
Report should have a title page containing the group member’s names and enrolment.
This is a group assignment, only two members per group allowed•◆•^-◆-
^•
Assignment will be graded individually based on the demo and viva.
Zero tolerance for solution plagiarism/generation from ANY source ☹