Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 26.12.2011, 20:33   #1
Anubys
Форумчанин
 
Регистрация: 08.05.2010
Сообщений: 177
По умолчанию Полиморфизм

Теперь все работает кроме сортировки массива

Код:
#include <iostream>
#include <conio>
#include <vcl.h>
#include <typeinfo>
#pragma hdrstop

class Intarface
{
    public:
        virtual void Show()           = 0;
        virtual string _toString()    = 0;
        virtual int getTotalObjects() = 0;
        virtual void setCurrentNumberObject()     = 0;
        virtual int getCurrentNumberObject()      = 0;
        virtual bool operator>(Intarface& obj)    = 0;
        virtual bool operator<(Intarface& obj)    = 0;
        virtual Intarface* _toObject(string temp) = 0;
        virtual Intarface& operator=(Intarface* obj) = 0
};


class Steblo : public Intarface
{
    private:
        float length;
        string title;
        static int totalObjects;
        int currentNumber;
    public:
        Steblo();
        Steblo(float _length, string _title);
        ~Steblo();
        virtual void Show() {
            cout << this->length << " " << this->title;
        }
        virtual Intarface* _toObject(string temp);
        virtual string _toString();
        virtual Intarface& operator=(Intarface* obj);
        virtual bool operator>(Intarface& obj);
        virtual bool operator<(Intarface& obj);
        virtual int getTotalObjects();
        virtual void setCurrentNumberObject();
        virtual int getCurrentNumberObject();
        

};

int Steblo::totalObjects = 0;

Steblo::Steblo()
{
    this->length = 25.5;
    this->title  = "Lilya";
    this->setCurrentNumberObject();
}
Steblo::Steblo(float _length, string _title)
{
    this->length = _length;
    this->title  = _title;
    this->setCurrentNumberObject();
}

Intarface* Steblo::_toObject(string temp)
{
    float fg;
    int found = 0;
    int totalVhod = 0;
    string temp1;
    string temp2[2];
    int i = 0;
    found = temp.find("_", 0);


    do
    {
        temp1 = temp.substr(found + 1);

        temp2[i] = temp.substr(0, found);
        temp = temp1;
        found     = temp.find("_", 0);
        totalVhod = temp.rfind("_", 0);

        if(found == totalVhod) temp2[i + 1] = temp;

        i++;
    }while(found > 0);

    fg = atof(temp2[0].c_str()); //Ïåðåòâîðåííÿ ðÿäêà â ÷èñëî ç ïëàâàþ÷îþ êîìîþ
    Intarface *pInter;
    Steblo Steblo1(fg, temp2[1]);
    pInter = &Steblo1;
    return pInter;
}
string Steblo::_toString()
{
    string temp;
    char buf[10];
    sprintf(buf,"%2.1f",this->length);
    temp.append(buf);
    temp.append("_");
    temp.append(this->title);

    return temp;
}
Intarface& Steblo::operator=(Intarface* obj)
{
    string temp = obj->_toString();
    obj = obj->_toObject(temp);

    this->operator =(obj);

    return *this;
}

bool Steblo::operator>(Intarface& obj)
{
    if(this->currentNumber > obj.getCurrentNumberObject()) return true;
    else return false;
}

bool Steblo::operator<(Intarface& obj)
{
    if(this->currentNumber < obj.getCurrentNumberObject()) return true;
    else return false;
}

int Steblo::getTotalObjects()
{
    return this->totalObjects;
}
void Steblo::setCurrentNumberObject()
{
    this->totalObjects++;
    this->currentNumber = this->totalObjects;
}
int Steblo::getCurrentNumberObject()
{
    return this->currentNumber;
}
Steblo::~Steblo()
{
    this->length = NULL;
    this->title.clear();
    this->totalObjects--;
    this->currentNumber = NULL;
}

void SortingBibles(Intarface* arr, int SIZE)
{
    Intarface *pTemp;
    //pTemp = &arr[0];

    for(int x = SIZE; x > 0; x--)
    {
        for(int i = 0; i < SIZE - 1; i++)
        {
            if(arr[i] > arr[i + 1])
            {
                pTemp = arr[i];
                arr[i] = arr[i+1];
                arr[i + 1] = pTemp;
            }
        }
    }

}



#pragma argsused
int main(int argc, char* argv[])
{
    Intarface *pIntarface;
    string temp23 = "5.5_Beladonna";
    Steblo pSteblo;
    Steblo *pSteblo1;
    pSteblo1 = new Steblo[10];
    pIntarface = &pSteblo;
    pSteblo1[0] = createObject();
    

    pSteblo1[1].Show();
    cout << endl;
    pSteblo1[0].Show();
    cout << endl;
    pIntarface->Show();
    cout << endl;
    pSteblo1[0] = pSteblo1[3];
    pSteblo1[0].Show();

    cout << endl;
    pSteblo1[0] = pSteblo1[1];
    cout << pSteblo1[0].getTotalObjects() << endl;

    delete[]pSteblo1;

    getch();
    return 0;
}

Последний раз редактировалось Anubys; 26.12.2011 в 21:36.
Anubys вне форума Ответить с цитированием
Старый 26.12.2011, 20:42   #2
Anubys
Форумчанин
 
Регистрация: 08.05.2010
Сообщений: 177
По умолчанию

Теперь вылетает такое предупреждение

Ему не нравится этот оператор (Выделил черным)
this->operator =(obj);
В операторе присваивания

Последний раз редактировалось Anubys; 26.12.2011 в 21:32.
Anubys вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Полиморфизм Zorgan Visual C++ 22 29.08.2011 12:23
Полиморфизм MasterSporta Общие вопросы C/C++ 3 10.04.2011 23:46
полиморфизм slayerblya Общие вопросы C/C++ 1 27.02.2011 01:43
Полиморфизм mister2010 Общие вопросы C/C++ 30 24.05.2010 01:07
Полиморфизм. Пример. Scratch Общие вопросы C/C++ 53 28.09.2008 18:46