0% found this document useful (0 votes)
43 views

Lab12 - File Handling

This document describes a lab assignment to design a C++ class to represent a cricket team. The class defines attributes like team name, players, and captain index. It includes methods to print the team data, add and replace players, and swap player order. The class uses vectors instead of arrays and strings instead of character arrays. The code provided implements the team class with functions to print data, get the team name, add a player, replace a player, and swap player order.

Uploaded by

Arham Siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lab12 - File Handling

This document describes a lab assignment to design a C++ class to represent a cricket team. The class defines attributes like team name, players, and captain index. It includes methods to print the team data, add and replace players, and swap player order. The class uses vectors instead of arrays and strings instead of character arrays. The code provided implements the team class with functions to print data, get the team name, add a player, replace a player, and swap player order.

Uploaded by

Arham Siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab12: File Handling

Date: 5th December 2023


Lab Engineer: Mehwish Kiran

Name: Muhammad Arham Siddiqui


CMS: 428887
Section: BEE-14B
Task
Design a class describing a cricket team
❑ Define its attributes
❑ Use string instead of char []
❑ Use vectors instead of regular arrays
❑ Define its behavior (member functions)
❑ Besides other functions, define a member function print_data()

CODE:

team.h
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;

class Player {
public:

string name;
int matches;
int runs;
double strike_rate;
int fifties;
int hundreds;
int balls_bowled;
int wickets;

string getName() const { return name; }


void setName(const std::string& name) { this->name = name; }
int getMatches() const { return matches; }
void setMatches(int matches) { this->matches = matches; }
int getruns() const {return runs;}

};
class team {
public:
// Team attributes
string team_name;
vector<Player> players;
int captain_index;

// Team methods
void print_data() const;
void set_team_name(string& name);
string get_team_name() const;
bool addPlayer(const Player& player);
bool replacePlayer(int index, const Player& player);
bool swapPlayerOrder(int index1, int index2);
void build(const std::string& file_path);

private:
Player best_batsman;
Player best_bowler;

void parse_player_data(const std::string& line);


void write_player_data(const Player& player, const std::string& filename);

};

team.cpp
#include "team.h"

void team::print_data() const {


std::cout << "Team Name: " << team_name << "\n";
std::cout << "Players:\n";
for ( const auto& player : players) {
std::cout << " - " << player.getName() << " (" << player.getMatches() << "
matches, " << player.getruns() << " runs)" << endl;
}
vector<Player>player;
std::cout << "Captain: " << player[captain_index].getName() << "\n";
}

string team::get_team_name() const {


return team_name;
}

bool team::addPlayer(const Player& player) {


// Check for duplicates or other conditions before adding
players.push_back(player);
return true;
}

bool team::replacePlayer(int index, const Player& player) {


if (index < 0 || index >= players.size()) {
return false;
}
players[index] = player;
return true;
}

bool team::swapPlayerOrder(int index1, int index2) {


if (index1 < 0 || index1 >= players.size() || index2 < 0 || index2 >=
players.size()) {
return false;
}
swap(players[index1], players[index2]);
return true;
}

You might also like