Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Funkcje zaprzyjaznione

Ostatnio zmodyfikowano 2015-12-13 13:55
Autor Wiadomość
pepe450
Temat założony przez niniejszego użytkownika
Funkcje zaprzyjaznione
» 2015-12-13 12:46:35
Jak zamienić aby podane funkcje aby nie były metodami klasy a funkcjami zaprzyjaznionymi?
C/C++
#ifndef MYTIME3_H_
#define MYTIME_H_
#include<iostream>
using namespace std;
class Time
{
    int hours;
    int minutes;
public:
    Time();
    Time( int h, int m = 0 );
    void AddMin( int m );
    void AddHr( int h );
    void Reset( int h = 0, int m = 0 );
    Time operator +( const Time & t ) const;
    Time operator -( const Time & t ) const;
    Time operator *( double n ) const;
    friend Time operator *( double m, const Time & t )
    {
        return t * m; //difinicja miejscowa inline
    }
    friend ostream & operator <<( ostream & os, const Time & t );
   
};
#endif // MYTIME3_H_
C/C++
#include "mytime3.h"
Time::Time()
{
    hours = minutes = 0;
}
Time::Time( int h, int m )
{
    hours = h;
    minutes = m;
}
void Time::AddMin( int m )
{
    minutes += m;
    hours += minutes / 60;
    minutes %= 60;
}
void Time::AddHr( int h )
{
    hours += h;
}
void Time::Reset( int h, int m )
{
    hours = h;
    minutes = m;
}
Time Time::operator +( const Time & t ) const
{
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}
Time Time::operator -( const Time & t ) const
{
    Time diff;
    int tot1, tot2;
    tot1 = t.minutes + 60 * t.hours;
    tot2 = minutes + 60 * hours;
    diff.minutes =( tot2 - tot1 ) % 60;
    diff.hours =( tot2 - tot1 ) / 60;
    return diff;
}
Time Time::operator *( double mult ) const
{
    Time result;
    long totalminutes = hours * mult * 60 + minutes * mult;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}
ostream & operator <<( ostream & os, const Time & t )
{
    os << t.hours << " godzin, " << t.minutes << " minut";
    return os;
}
P-141881
carlosmay
» 2015-12-13 12:56:54
Jak zamienić aby podane funkcje aby nie były metodami klasy a funkcjami zaprzyjaznionymi?
 Hę?
Która funkcja? Konkret. Co chcesz zrobić?
Pewnie robisz z Praty? Przeczytaj cały rozdział o stosowaniu klas.
Jest tam wszystko czarno na białym co i jak i po co.
P-141882
carlosmay
» 2015-12-13 13:55:18
C/C++
Time operator +( const Time & t ) const; // zamiast tego piszesz
friend Time operator +( const Time & t1, const Time & t2 ); // ?? o to chodziło?
P-141887
« 1 »
  Strona 1 z 1