Define a class batsman with the following specifications: Note for user understanding purposes
you should write comment with each line of code.
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 on the screen.
#include <iostream>
using namespace std;
class batsman
{
//Private members:
private:
//bcode 4 digits code number
int bcode;
//bname 20 characters
char bname[20];
//innings, notout, runs integer type
int innings;
int notout;
int runs;
float batavg;
//calcavg() Function to compute batavg
void calcavg()
{
//batavg it is calculated according to the formula – batavg =runs/(innings-notout)
batavg=(float)runs/(float)(innings-notout);
}
public :
//readdata() Function to accept value from bcode, name, innings, notout and invoke the function calcavg()
void readdata (){
//read batsman code from a user
cout<<"Enter batsman code: ";
cin>> bcode;
cin.ignore();
//read batsman name from a user
cout<<"Enter batsman name: ";
gets(bname);
//read batsman innings from a user
cout<<"Enter innings: ";
cin>>innings;
//read batsman notout from a user
cout<<"Enter notout: ";
cin>>notout;
//read batsman runs from a user
cout<<"Enter runs: ";
cin>>runs;
//call the function calcavg
calcavg();
}
//displaydata() Function to display the data members on the screen.
void displaydata(){
//Display batsman code
cout<<"Batsman code: "<<bcode<<"\n";
//Display batsman innings
cout<<"Batsman name: "<<bname<<"\n";
//Display batsman innings
cout<<"Innings: "<<innings<<"\n";
//Display batsman notout
cout<<"Not out: "<<notout<<"\n";
//Display batsman runs
cout<<"Runs: "<<runs<<"\n";
//Display Batting Average
cout<<"Batting Average: "<<batavg;
}
};
//The start point of the program
int main()
{
//Create object of batsman
batsman batsmanObj;
//call the method readdata
batsmanObj.readdata();
//call the method displaydata
batsmanObj.displaydata();
cout<<"\n\n";
return 0;
}
Comments
Leave a comment