LINUX.ORG.RU

История изменений

Исправление soomrack, (текущая версия) :


#include <iostream>

class Parent {
public:
    int id;
public:
    Parent() { id = 1; };
    Parent(int number) { id = number; };
    Parent& operator=(Parent &source) { id = source.id; return *this; };
};


class Child : public Parent {
public:
    Child() {};
    Child& operator=(Parent &source) { Parent::operator=(source); return *this; };
};



int main() {
    Parent P(10);
    std::cout << "Parent.id = " << P.id << std::endl;

    Child C;
    std::cout << "Child.id = " << C.id << std::endl;

    C = P;
    std::cout << "Child.id = " << C.id << std::endl;
}

const, virtual и пр. в соотв. с задачей…

Исходная версия soomrack, :


#include <iostream>

class Parent {
public:
    int id;
public:
    Parent() { id = 1; };
    Parent(int number) { id = number; };
    Parent& operator=(Parent &source) { id = source.id; return *this; };
};


class Child : public Parent {
public:
    Child() {};
    Child& operator=(Parent &source) { this->Parent::operator=(source); return *this; };
};



int main() {
    Parent P(10);
    std::cout << "Parent.id = " << P.id << std::endl;

    Child C;
    std::cout << "Child.id = " << C.id << std::endl;

    C = P;
    std::cout << "Child.id = " << C.id << std::endl;
}

const, virtual и пр. в соотв. с задачей…