[ create a new paste ] login | about

Link: http://codepad.org/16WP8ar5    [ raw code | output | fork ]

C++, pasted on Feb 20:
#include <iostream>
#include <algorithm>
 
#include <cstring>	


//定義一個Baseball的類別
class BaseballPlayer 
{
    private: enum
    {
        MAX_STRING_LENGTH = 20    
    }; 
    //定義私用資料及函式成員
    private: char name[ MAX_STRING_LENGTH + 1 ];         //打擊者姓名         
    private: int fires;                                  //打擊次數
    private: int safes;                                  //安打次數
    //宣告私用的函數成員countsafe的原型用以計算打擊者打擊率
    
    // 新加上的預設建構子, 並以靜態成員作為初值來源 
    public:  BaseballPlayer()
	: fires(),  safes() 
    { 
       name[ 0 ] = '\0';           
    } 
	
    //定義公用資函數成員
    public: BaseballPlayer( char const *theName, int theFires, int theSafes )
    : fires( theFires ),  safes( theSafes ) 
    {
       ::strncpy( name, theName, MAX_STRING_LENGTH );
    }
	
	public: BaseballPlayer& operator=( BaseballPlayer const &player )
    {     
       ::strncpy( name, player.name, MAX_STRING_LENGTH );
       fires = player.fires;
       safes = player.safes; 
       return *this;        
    } 
	
    public: std::ostream& show( std::ostream &outputStream );
    //宣告公用的函式數成員inputplayer的原型用以顯示打擊者資料

};

std::ostream& BaseballPlayer::show( std::ostream &outputStream )          //類別外實現showplayer函式成員
{
	
    //透過countsafe 函式成員計算並傳回打擊率
    outputStream << "================================" << std::endl;
    outputStream << "打擊者:" << name << std::endl;     //顯示打擊者姓名
    outputStream << "打擊次數:" << fires << std::endl;    //顯示打擊次數
    outputStream << "安打次數:" << safes << std::endl;    //顯示安打次數
    
    return outputStream; 
}


int main()
{
    using namespace std;
    
    BaseballPlayer b[ 2 ]; 
    std::fill( b, b + 2, BaseballPlayer( "qq", 100, 2 ) ); 
     
    cout << "================================" << endl;
    cout << "顯示資料" << endl;
    for ( int i = 0; i < 2; ++i )  
    {
        b[ i ].show( cout );
    }
	
    cin.get();
}


Output:
1
2
3
4
5
6
7
8
9
10
================================
顯示資料
================================
打擊者:qq
打擊次數:100
安打次數:2
================================
打擊者:qq
打擊次數:100
安打次數:2


Create a new paste based on this one


Comments: