Sunday, March 3, 2013

C++ Codes Of Classes And Objects 2



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 on the screen.



#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class batsman
{
 int bcode;
 char bname[20];
 int innings,notout,runs;
 int batavg;
 void calcavg()
 {
  batavg=runs/(innings-notout);
 }

public :
 void readdata ();
 void displaydata();
};

void batsman::readdata ()
{
 cout<<"Enter batsman code ";
 cin>> bcode;
 cout<<"Enter batsman name ";
 gets(bname);
 cout<<"enter innings,notout and runs ";
 cin>>innings>>notout>>runs;
 calcavg();
}

void batsman::displaydata()
{
 cout<<"Batsman code "<<bcode<<"\nBatsman name "<<bname<<"\nInnings "<<innings
 <<"\nNot out "<<notout<<"\nRuns "<<runs<<"\nBatting Average "<<batavg;
}

int main()
{
 batsman obj;
 obj.readdata();
 obj.displaydata();
 getch();
 return 0;
}

No comments:

Post a Comment