Klasa Ulamek i Para

0

Cześć, pilne to dla mnie, wiec z gory dzieki za pomoc

mam takiego maina, ktorego nie mozna modyfikowac i mam napisac reszte programu - to cale polecenie zadania
zakomentowalem czesc maina i wtedy z moim kodem wszystko dziala, jak jednak napisac ta reszte?

#include "Ulamek.h"
#include "Para.h"

int main()
{

        Ulamek u0, u1, u2(1), u5;
        const Ulamek u3(1, -2), u4(-0.7125);//nie wiem za bardzo jak to zamiane zrobic na ulamek zwykly (zrobilem taka atrape ze sztywnymi aymi w konstruktorze) ale raczej to nie przejdzie

        u0 = u2 + 3;
        //u1 = Ulamek::add(u2, 1);  //nie wiem jak zrobic ta metode add, wyglada to jak wywolanie jakiejsc metody z przestrzeni nazw, ale ta nazwa jest identyczna z nazwa klasy i mi sie to ciagle sypie

        Para t[2] = {{9,3}};

        std::cout << "Para t[0] = " <<  t[0].x << ", " <<  t[0].y << "\n";
        std::cout << "Para t[1] = " <<  t[1].x << ", " <<  t[1].y << "\n";

       u5 = t[0];

        u0.print(); u1.print(); u2.print(); u3.print(); u4.print(); u5.print();

        
       // t[1] = u4;  nie moge tego zrobic (jak zrobie 18 linijke to tego nie..)

        std::cout << "Para t[1] = " <<  t[1].x << ", " <<  t[1].y << "\n";

}

/*
Wyjście z programu (tylko adresy mają być wypełnione)

Para t[0] = 9, 3
Para t[1] = 0, 0
Obiekt pod adresem: 0xXXXXXXXXXXXX = 4
Obiekt pod adresem: 0xXXXXXXXXXXXX = 2
Obiekt pod adresem: 0xXXXXXXXXXXXX = 1
Obiekt pod adresem: 0xXXXXXXXXXXXX = -1/2
Obiekt pod adresem: 0xXXXXXXXXXXXX = -57/80
Obiekt pod adresem: 0xXXXXXXXXXXXX = 3
Para t[1] = -57, 80

*/
 

moj kod:
plik Ulamek.h

 
#ifndef _ULAMEK_H
#define	_ULAMEK_H
#include <iostream>
#include "Para.h"
/*class Ulamek;

namespace Ulamek
{

const Ulamek add(Ulamek & a, int x);

}*/

class Ulamek 
{
public:
    friend class Para;
    Ulamek(int ll = 0, int mm = 0) : l(ll), m(mm)
    {
    }

    Ulamek(double a) : l(-57), m(80)
    {
    } // jak inaczej, to zrobic, aby bylo uniwersalne
    const Ulamek operator+(int ll) const;
  
    void print() const;

 /*  friend const Ulamek add(Ulamek & a, int x)
    {


        return Ulamek(a.l + x, a.m);

    }*/

void operator=( const Para& a);
    

    int & get_l()
    {
        return l;
    }

    int & get_m()
    {
        return m;
    }

    const int & get_l()const
    {
        return l;
    }

    const int & get_m()const
    {
        return m;
    }
private:
    int l;
    int m;

};

 




#endif	/* _ULAMEK_H */

plik Ulamek.cpp

 
#include "Ulamek.h"


const Ulamek Ulamek::operator+(int ll) const
{
    return Ulamek(l + ll, m);
}

void Ulamek::print() const
{
    if (m < 0)
    {
        std::cout << "Obiekt pod adresem: " << (int*) this << " = " << -l;
        if (m != 0)
            std::cout << "/" << -m << std::endl;
        else
            std::cout << "\n";
    }
    else if (l > m && m != 0)
        std::cout << "Obiekt pod adresem: " << (int*) this << " = " << l / m << std::endl;
    else
    {
        std::cout << "Obiekt pod adresem: " << (int*) this << " = " << l;
        if (m != 0)
            std::cout << "/" << m << std::endl;
        else
            std::cout << "\n";
    }


}

void Ulamek::operator=(const Para& a)
    {
        l = a.x;
        m = a.y;
    }
 

plik Para.h

#ifndef _PARA_H
#define	_PARA_H

//#include "Ulamek.h"
class Para 
{
public:
    friend class Ulamek;
   
    

    // void operator=(const Ulamek& a);
    int x;
    int y;

    

    int & get_x()
    {
        return x;
    }

    int & get_y()
    {
        return y;
    }



    

};



#endif	/* _PARA_H */

 

plik Para.cpp

#include "Para.h"
//void Para::operator=(const Ulamek& a) {x=a.l; y=a.m;   }


 
0

ok, udalo mi sie zaprzyjaznic te klasy wzajemnie

ale mam jeden problem, co to jest i jak to zrobic ?

 u1 = Ulamek::add(u2, 1); 

wyglada to jak wywolanie funkcji zaprzyjaznionej z klasa, bedacej w przestrzeni wektor, ale jak to zrealizowac, jak przestrzen i kalsa sie tak samo nazywaja ?

0

jest to wywołanie metody statycznej add() klasy Ulamek.

class Ulamek {
   ...
   static Ulamek add(Ulamek u, int i) { ... }
   ...
};

1 użytkowników online, w tym zalogowanych: 0, gości: 1