Define a class Batsman with the following specifications:
Private members:
bcode 4 digits code number bname 20 characters innings, notout, runs integer type batavg it is calculated according to the formula – batavg =runs/(innings-notout) calcavg() Function to compute batavg
Public members:
readdata() Function to accept value from bcode, name, innings, notout and invoke the function calcavg(). displaydata() Function to display the data members.
#include <iostream>
using namespace std;
class Batsman {
private:
//bcode 4 digits code number bname 20 characters innings, notout, runs integer type batavg
int bcode;
char bname[20];
int innings,notout,runs,batavg;
//calculated according to the formula – batavg =runs/(innings-notout) calcavg() Function to compute batavg
void calcavg(){
if((innings-notout)<=0){
batavg=0;
}
else{
batavg = runs/(innings-notout);
}
}
public:
//Public members:
//readdata() Function to accept value from bcode, name, innings, notout and invoke the function calcavg().
void readdata (){
cout<<"Enter Batsman code: ";
cin>> bcode;
cout<<"Enter Batsman name: ";
cin.ignore();
cin.getline(bname, 20);
cout<<"Enter innings: ";
cin >> innings;
cout<<"Enter notout: ";
cin >> notout;
cout<<"Enter runs: ";
cin >> runs;
calcavg();
}
//displaydata() Function to display the data members.
void displaydata() {
cout<<"\nBatsman code: "<<bcode<<"\n";
cout<<"Batsman name: "<<bname<<"\n";
cout<<"Innings: "<<innings<<"\n";
cout<<"Notout: "<<notout<<"\n";
cout<<"Runs: "<<runs<<"\n";
cout<<"Batting Average: "<<batavg<<"\n";
}
};
int main(){
Batsman batsmanObject;
batsmanObject.readdata();
batsmanObject.displaydata();
return 0;
}
Comments
Leave a comment