0% found this document useful (0 votes)
11 views9 pages

OOP hierarchy

Class notes on oop hierarchy

Uploaded by

johnmurad53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

OOP hierarchy

Class notes on oop hierarchy

Uploaded by

johnmurad53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment-3

Submitted To: Respect Ma’am, Saima Javad


Section: 2-A
Course: Object Oriented Programming
Submitted By: Muhammad Shamyl Hassan
Enrollment: 01-134232-133
BAHRIA UNIVERSITY
Islamabad Campus
Department of Computer Sciences

Object-Oriented BSCS-
Programming
Assignment No. 4 (CLO3) 2A
Due Date:1st Dec,
Instructor: Saima Jawad 2024
Total Marks: 50

Polymorphic Player Class


Hierarchy
Implement the following polymorphic class hierarchy in C++. Use the class
hierarchy to display the players ranking table.

Sportsman

Footballer Cricketer

Bowler Batsman

Allrounder
Requirements:

 Program should be well designed using object-oriented concepts learned so far.


 Draw a UML class diagram first and then convert it into C++ code.
 Implement each class as a separate header (.h) and source (.cpp) file.
 computeAverage() function should be used to calculate the average
goals/score of the players using dynamic binding.
 Data should be displayed by overloading << and >> operators.
 You are allowed to add appropriate data members and functions in the
classes as required.
 Use a text file to read data of your favorite 10-15 players.
 The final output should be in the form of a ranking table.

Submission:

Please submit a report containing Problem Statement, Objectives, UML Class


Diagram, Source code, data file, sample Program Outputs and Conclusion.

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:

Header File: Sportsman.h


#ifndef SPORTSMAN_H
#define SPORTSMAN_H

#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

Class File: Sportsman.cpp


#include "Sportsman.h"

Sportsman::Sportsman(string n,int m):name(n),matches(m){}


void Sportsman::display() const {
cout << "Name: "<<name<<",matches: "<<matches;
}
Sportsman::~Sportsman() {}

ostream& operator<<(ostream& out, const Sportsman& s) {


out << "Name: " << s.name << ", Matches: " << s.matches;
return out;
}

istream& operator>>(istream& in, Sportsman& s) {


in >> s.name >> s.matches;
return in;
}

Header File: Footballer.h


#ifndef FOOTBALLER_H
#define FOOTBALLER_H

#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

Class File: Footballer.cpp


#include "Footballer.h"
#include <iostream>

Footballer::Footballer(string n, int m, int g) : Sportsman(n, m), goals(g) {}

void Footballer::computeavg() {
cout << "Average Goals per Match: " << (matches > 0 ? (float)goals / matches : 0) << endl;
}

void Footballer::display() const {


cout << "Footballer: " << name << ", Matches: " << matches << ", Goals: " << goals << endl;
}

Header File: Cricketer.h


#ifndef CRICKETER_H
#define CRICKETER_H

#include "Sportsman.h"

class Cricketer : public Sportsman {


protected:
int runs;
int wickets;

public:
Cricketer(string n = "", int m = 0, int r = 0, int w = 0);
virtual void computeavg() override;
};

#endif

Class File: Cricketer cpp


#include "Cricketer.h"
#include <iostream>

Cricketer::Cricketer(string n, int m, int r, int w) : Sportsman(n, m), runs(r), wickets(w) {}

void Cricketer::computeavg() {
cout << "Average Runs: " << (matches > 0 ? (float)runs / matches : 0) << endl;
}

Header File: Bowler.h


#ifndef BOWLER_H
#define BOWLER_H

#include "Cricketer.h"

class Bowler : public Cricketer {


public:
Bowler(string n = "", int m = 0, int w = 0);
void computeavg() override;
};

#endif

Class File: Bowler.cpp


#include "Bowler.h"
#include <iostream>

Bowler::Bowler(string n, int m, int w) : Cricketer(n, m, 0, w) {}

void Bowler::computeavg() {
cout << "Average Wickets per Match: " << (matches > 0 ? (float)wickets / matches : 0) << endl;
}

Header File: Batsman.h


#ifndef BATSMAN_H
#define BATSMAN_H

#include "Cricketer.h"

class Batsman : public Cricketer {


public:
Batsman(string n = "", int m = 0, int r = 0);
void computeavg() override;
};

#endif

Class File: Batsman.cpp


#include "Batsman.h"
#include <iostream>

Batsman::Batsman(string n, int m, int r) : Cricketer(n, m, 0, r) {}

void Batsman::computeavg() {
cout << "Average runs per Match: " << (matches > 0 ? (float)runs / matches : 0) << endl;
}

Header File: Allrounder.h


#ifndef ALLROUNDER_H
#define ALLROUNDER_H

#include "Cricketer.h"

class Allrounder : public Cricketer {


private:
int wickets;

public:
Allrounder(std::string n = "", int m = 0, int r = 0, int w = 0);
void computeavg() override;
void display() const override;
};

#endif

Class File: Allrounder.cpp


#include "Allrounder.h"
#include <iostream>

Allrounder::Allrounder(string n, int m, int r, int w) : Cricketer(n, m, r, w) {}

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;
}

void Allrounder::display() const {


cout << "Allrounder: " << name << ", Matches: " << matches << ", Runs: " << runs << ", Wickets: " <<
wickets << 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;

while (inFile >> name >> matches >> goals) {


if (goals >= 0) {
player = new Footballer(name, matches, goals);
player->display();
player->computeavg();
delete player;
}
}

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 ☹

You might also like