22 Mart 2016 Salı

Class Application "Soccer Player" örneği

Kod :





#include <iostream>
#include <string>

using namespace std;

class SoccerPlayer{

private :

        const string name;
        int no;
        static int numberofPlayers;


public :

        void setNo(int numara)
        {
        no=numara;
        }

        int getNo (void) const
        {
            return no;
        }
        string getName() const
        {
            return name;
        }

        static int getnumberofPlayers()
        {
            return numberofPlayers;
        }

        SoccerPlayer() : no(0), name("NoName")
        {
            incrementNumberofPlayers();

            cout << "Default constructor cagrildi" << endl;


        }

        SoccerPlayer(int myNo, string myName) : name(myName)
        {
            no=myNo;
            incrementNumberofPlayers();
            cout << "Parametre yapici fonksiyon " <<endl;
        }

        SoccerPlayer(const SoccerPlayer &sp) : name(sp.getName()) //!Kopya Yapici
        {
            setNo(sp.getNo());
            incrementNumberofPlayers();
            cout << "Kopya constructor" << endl;
        }


        ~SoccerPlayer() // Destructor (Böylece oyuncu sayısı azaltıldı)
        {
        decrementNumberofPlayers();
        }


private :

    void incrementNumberofPlayers()
    {
        numberofPlayers++;
    }
    void decrementNumberofPlayers()
    {

        numberofPlayers--;
    }


public :

    friend void displayPlayers(SoccerPlayer &sp); // Friend


};



int SoccerPlayer::numberofPlayers=0; // Statik Veri

void displayPlayers(SoccerPlayer &sp)  // Friend Fonksiyon
{
    cout << "Soccer Player info :"  << endl ;
    cout << "Name :    " << sp.name << "     No :  " << sp.no << endl;


}


int main(void)
{

    cout << "Number of Players  :   " << SoccerPlayer::getnumberofPlayers() << endl;

    SoccerPlayer *sptr1=new SoccerPlayer();
    displayPlayers(*sptr1); //!!!Non Member Function is called

    cout << "\nNumber of Players :   " << SoccerPlayer::getnumberofPlayers() << "\n" ;

    SoccerPlayer *ptr2 = new SoccerPlayer(10,"Messi");

    displayPlayers(*ptr2);
    cout << "\nNumber of Players :   " << SoccerPlayer::getnumberofPlayers() << "\n";

    SoccerPlayer sp1(11, "Ronaldo"); //!!new data with parametrize
    displayPlayers(sp1);
    cout << "\nNumber of Players :   " << SoccerPlayer::getnumberofPlayers() << "\n";

    SoccerPlayer sp2(sp1); //!!Kopya constructor
    cout << "\nNumber of Players :   " << SoccerPlayer::getnumberofPlayers() << "\n";

    delete ptr2;
    cout <<"We delete ptr2(10, Messi) " << endl;
    cout << "\nNumber of Players :   " << SoccerPlayer::getnumberofPlayers() << "\n";



    displayPlayers(sp1);
    displayPlayers(sp2);

    return 0;
}


Ekran Çıktısı : 


1 yorum:

  1. Hotel Las Vegas, NV - Goyang Hotel and Casino
    The only non-smoking room 업소 추천 with free Wi-Fi is the The room rate of 토토 사이트 추천 this hotel is the same as the room rate 스포츠사이트 for all standard rooms. 블랙 잭 사이트 The rooms 샌즈바카라 were

    YanıtlaSil