Wskazówki do zadania

0

Uzupeªni¢ podane klasy i funkcje, tak aby zadziaªaªa podana ni»ej funkcja main:

#include <iostream>
#include <cstring>
using namespace std;
class Person {
    char* name;
public:
    friend class Couple;
    friend ostream& operator<<(ostream& str, const Person& os);
    Person(const char* n);
    Person(const Person& os);
    Person& operator=(const Person& os);
    ~Person();
};
class Couple {
    Person *husb, *wife;
public:
    friend ostream& operator<<(ostream& str, const Couple& p);
    Couple(const char* m, const char* z);
    Couple(const Couple& p);
    Couple& operator=(const Couple& p);
    ~Couple();
};
int main(void) {
    Couple *c1 = new Couple("John","Sue");
    Couple c2("Bert","Elsa");
    *c1 = c2;
    Couple c3(*c1);
    delete c1;
    cout << c3 << endl;
}

Program nie powinien pada¢ i wypisa¢ co± w rodzaju
He: Bert, She: Elsa

Moglibyście dać mi jakieś wskazówki do tego zadania?? Za bardzo nie wiem co wpisać w te metody, żeby otrzymać taki wynik..

0

Weź do ręki książkę o C++ i poczytaj. Masz tutaj wiele podstawowych rzeczy.

0

A jak wywołać z konstruktora
Couple::Couple(const char* m, const char* z)
{

};

ten konstruktor..
Person(const char* n)
{
*name=*n;
}

Jak to będę wiedział, to już z resztą bym sobie poradził..

0

Konstruktor z klasy Person wywołasz w konstruktorze klasy Couple korzystając z listy inicjalizacyjnej konstruktora Couple

0

Couple(const char* m, const char* z):wife(m)
{

}

???

0

@rzeźnik w normalnej sytuacji tak:

class Base {};
class Derived : public Base
{
  Derived(argumenty):Base(jakisargument)
  {
    //cos tam
  }
}

Ale w twoim przypadku jedyne pola klasy to wskaźniki więc powinieneś mieć:

Couple::Couple(const char* m, const char* z)
{
  husb = new Person(m);
  wife = new Person(z);
}
0

Przepraszam, że tak truję.. ale teraz mi się wywala program..

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class Person {
  
    char* name;
  public:
    friend class Couple;
    friend ostream& operator<<(ostream& str, const Person& os);
    Person(const char* n)
    {
        strcpy(name,n);
;    }
    Person(const Person& os)
    {

    }
    Person& operator=(const Person& os);
    ~Person();
};
class Couple {

    Person *husb, *wife;
public:
    friend ostream& operator<<(ostream& str, const Couple& p);
    Couple(const char* m, const char* z);
    Couple(const Couple& p);
    Couple& operator=(const Couple& p);
    ~Couple()
    {
     delete wife;
	 delete husb;
    }
};
Couple::Couple(const char* m, const char* z)
{
  husb = new Person(m);
  wife = new Person(z);
}
int main(void)
{
    Couple *c1 = new Couple("John","Sue");
	
    return 0;
}
0

To go może łaskawie NAPISZ POPRAWNIE?

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class Person
{
    char* name;
public:
    Person(const char* n)
    {
        name = new char[strlen(name)+1];
        strcpy(name,n);
    }
    Person(const Person& os)
    {
        name = new char[strlen(os.name)+1];
        strcpy(name,os.name);
    }
    Person& operator=(const Person& os)
    {
        if(&os != this)
        {
            delete[] name;
            name = new char[strlen(os.name)+1];
            strcpy(name,os.name);
        }
        return *this;
    }
    ~Person()
    {
        delete[] name;
    }

    friend ostream& operator<<(ostream& str, const Person& os)
    {
        str<<os.name;
        return str;
    }
};

class Couple
{
    Person husb;
    Person wife;
public:
    Couple(const char* m, const char* z):husb(m),wife(z){}
    Couple(const Couple& p):husb(p.husb),wife(p.wife){}
    Couple& operator=(const Couple& p)
    {
        husb = p.husb;
        wife = p.wife;
        return *this;
    }
    friend ostream& operator<<(ostream& str, const Couple& p)
    {
        str<<p.husb<<" "<<p.wife;
        return str;
    }
};

int main(void)
{
    Couple x("John","Sue");
    cout<<x;
    return 0;
}
0

To co napisałeś też się sypie..

0

Ok, mam:)
Sorka..
name = new char[strlen(name)];

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