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

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

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

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

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

Ответ
 
Опции темы Поиск в этой теме
Старый 19.05.2014, 18:48   #1
cj3678
Новичок
Джуниор
 
Регистрация: 19.05.2014
Сообщений: 1
По умолчанию Перегрузка оператора вывода в классе.

[PHP]Здравствуйте. Столкнулся с проблемой при работе с классом (Время), что выводит или чушь или вообще ничего не выводит. Прописал (вроде бы) 1 функцию и 1 оператор.
Код C++.
Файл main.cpp
PHP код:
[CODE]//---------------------------------------------------------------------------

#include <iostream>
#include "ctime.h"


using namespace std;

using namespace std;


int main()
{
    
int h,m,s;
    
cout << "Enter hour: ";
    
cin >> h;
    
cout << "Enter minute: ";
    
cin >> m;
    
cout << "Enter seconds: ";
    
cin >> s;
    
CTime D(1,2,3);
    
cout<<"A:\n";
    
CTime A(h,m,s);
    
cout<< A<<endl;
    
cout<<"\nB:\n";
    
CTime B (151620);
    
cout << B;
;
    
CTime C B;
    
cout<<"\nC:\n";
    
cout << C;
    
B;
    
cout<<"\nC:\n";
    
cout << C;
    
A;
    
cout<<"\nC:\n";
    
cout << C;
    if (
A.isNight()) cout<<"\nNight\n";
    if (
A.isDay()) cout<<"\nDay\n";
    if (
A.isMorning()) cout<<"\nMorning\n";
    if (
A.isEvening()) cout<<"\nEvening\n";
    
cout << "\n" ;
    if (
== Bcout<< "\nA==B\n"
       else 
cout<<"\n A!=B \n";
    
CTime PrintT(A);
        return 
0;
}
//---------------------------------------------------------------------------
 
 
[/CODE
И заголовочный файл:
PHP код:
 [CODE]// ctime.h

#include <iostream.h>

class CTime {

    private:
        
int hour;      //?anu
        
int minute;    //ieioou
        
int seconds;   //naeoiau

    
public:
        
CTimeintintint );
        ~
CTime();

        
CTime operator+ ( CTime );   //iia?aoi? nei?aiey
        
CTime operator- ( CTime );   //iia?aoi? au?eoaiey
        
bool  operator==( CTime );  //iia?aoi? ?aaainoaa
        
bool  operator> ( CTime );   //iia?aoi? n?aaiaiey
        
bool  operator< ( CTime );   //iia?aoi? n?aaiaiey
        
friend ostreamoperator<<(ostream&, const CTime&);    
        
friend istreamoperator>>(istream&, CTime&);          
        
        
bool isNight ();        //i?iaa?ea ia a?aiy aiy: Ii?u, Aaiu, Oo?i, Aa?a?
        
bool isDay ();
        
bool isMorning ();
        
bool isEvening ();
        
void PrintT ();
        
void Convert();
};

//////////////////////////////////////////////////////////

CTime :: CTimeint nHourint nMunuteint nSeconds)
{
}

//////////////////////////////////////////////////////////

CTime :: ~CTime()
{
}

//////////////////////////////////////////////////////////

CTime CTime :: operator+( CTime tm )
{
    
CTime tthis -> hourthis -> minute,this -> seconds );

    
t.hour    += tm.hour;
    
t.minute  += tm.minute;
    
t.seconds += tm.seconds;

    if (
t.seconds >= 60) {
        do {
            
t.seconds -= 60;
            
t.minute  += 1;
        } while (
t.seconds >= 60);
    }

    if (
t.minute >= 60) {
        do {
            
t.minute -= 60;
            
t.hour += 1;
        } while(
t.minute >= 60);
    }

    if(
t.hour >= 24) {
        
t.hour t.hour 24;
    }

    return 
t;
}

//////////////////////////////////////////////////////////

CTime CTime :: operator-( CTime tm )
{
    
CTime t(this->hourthis->minutethis->seconds);

    
t.hour    -= tm.hour;
    
t.minute  -= tm.minute;
    
t.seconds -= tm.seconds;

    if(
t.seconds 0) {
        
t.seconds += 60;
        
t.minute  -= 1;
    }

    if(
t.minute 0) {
        
t.minute += 60;
        
t.hour   -= 1;
    }

    if(
t.hour 0) {
        
t.hour = -t.hour;
    }

    return 
t;
}

//////////////////////////////////////////////////////////

bool CTime :: operator==( CTime t )
{
    return (
this -> hour == t.hour && this -> minute == t.minute && this -> seconds == t.seconds);
}

//////////////////////////////////////////////////////////

bool CTime :: operator>( CTime t )
{
    return (
this -> hour t.hour);
}

//////////////////////////////////////////////////////////

bool CTime :: operator<( CTime t )
{
    return (
this -> hour t.hour);
}

//////////////////////////////////////////////////////////

bool CTime :: isNight()
{
    return (
this -> hour 6);
}

//////////////////////////////////////////////////////////

bool CTime :: isMorning()
{
    return (
this -> hour >= && this -> hour 12);
}

//////////////////////////////////////////////////////////

bool CTime :: isDay()
{
    return (
this -> hour >= 12 && this -> hour 18);
}

//////////////////////////////////////////////////////////

bool CTime :: isEvening ()
{
    return (
this -> hour >= 18 && (this -> hour 23 && this -> minute 59 && this -> seconds 59));
}
//Вот я попытался функцией
void CTime :: PrintT ()
{
    
std::cout<<this -> hour<<":"<<this -> minute<<":"<<this -> seconds<<endl;
}

//Не работает :(
istreamoperator>>(istream &inputCTime &t){

    
cout << "Enter hour: ";
    
input >> t.hour;
    
cout << "Enter minute: ";
    
input >> t.minute;
    
cout << "Enter seconds: ";
    
input >> t.seconds;
    return 
input;
}
 
//А вот оператор
ostreamoperator<<(ostream &output, const CTime &t){

    
    
    
output<<t.hour<<":"<<t.minute<<":"<<t.seconds<<endl;
    
    return 
output;
}



void CTime :: Convert()
{
} [/
CODE
Заранее спасибо
cj3678 вне форума Ответить с цитированием
Ответ


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



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Перегрузка оператора вывода и дружественные функции nidaime Общие вопросы C/C++ 1 19.11.2013 06:18
перегрузка оператора вывода в шаблонном классе Anubys Общие вопросы C/C++ 3 14.03.2012 15:15
перегрузка оператора + nesss Помощь студентам 0 01.11.2011 02:23
Переопределение оператора в классе Rekky Общие вопросы C/C++ 1 04.05.2011 16:44
Перегрузка оператора в шаблонном классе alex_alpha Общие вопросы C/C++ 0 12.08.2010 21:37