22 Mart 2016 Salı

Class Application "Box" Örnek Soru

KOD : 





#include <iostream>
#include <string>
using namespace std;

class Box {

private :

    string color;
    int length;
    int volume;

public:

    Box()
    {
        color ="No Color";
        length=1;
        volume =1;
        incrementBox();
    }

    Box(string col, int uzunluk)
    {
        color=col;
        length=uzunluk;
        incrementBox();

    }

    ~Box()
    {
        cout << "Destructor Time..." << endl;
        decrementBox();

    }

    void setColour(string col)
    {
        color=col;

    }

    void setLength(int uzun)
    {
        if(uzun <0)
            length=1;
        else
        length=uzun;

    }

    string getColour (void) const
    {

        return color;
    }

    int getLength(void) const
    {

        return length;

    }

    int calculateVolume(int length)
    {

       volume= length*length*length;

    }

    static int getnumberofBoxes(void)
    {
        return numberofBoxes;


    }

private :

    static int numberofBoxes;

public :

    void incrementBox()
    {
        numberofBoxes++;
    }
    void decrementBox()
    {
        numberofBoxes--;
    }

    friend void printBox(Box &bx);
};

int Box::numberofBoxes=0;

void printBox(Box &bx) //!!Non Member Function (Global Function)---Box'un arkadaşı Box:: şeklinde gösterilmez
{

    cout << "Printing..." << endl;
    cout <<"Colour :  " << bx.color << endl;
    cout <<"Length :  " << bx.length << endl;
    cout << "Volume : " << bx.volume << endl;
    /*Arkadas fonksiyon get'e ihtiyac olmadan private'a erisebilir */

}

int main()
{

 cout << "Veri girilmeden Box Sayisi  :  " << Box::getnumberofBoxes() << endl;

    Box test[3];
    int i;
    string tmpColor;
    int tmpLength;

    for(i=0 ; i<3 ; i++){
        cout << i+1 << "icin  sirasiyla Color ve Length giriniz " << endl;
        cin>>tmpColor;
        test[i].setColour(tmpColor);
        cin>>tmpLength;
        test[i].setLength(tmpLength);

    }

    for(i=0 ; i<3 ; i++)
    {

        test[i].calculateVolume(test[i].getLength());


    }

    for(i=0 ; i<3 ; i++)
    {
        printBox(test[i]);


    }


cout << "Veri girildikten sonraki Box Sayisi  :  " << Box::getnumberofBoxes() << endl;



}



Ekran Çıktısı : 



Hiç yorum yok:

Yorum Gönder