Answer to Question #203730 in C++ for python

Question #203730

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.


1
Expert's answer
2021-06-05T23:55:24-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog