środa, 1 stycznia 2014

C++ inheritance basics / C++ dziedziczenie podstawy

Start inheritances
_________________________________________________________________
#include <iostream>
using namespace std;
class A
{
public:
    A(int X,int Y):x(X),y(Y){}
    A operator =(const A &wzor);
    void write();
protected:
private:
    int x,y;
};
void A::write()
{
    cout <<"Data"<<endl<< "x=" <<x<<endl<<"y="<<y<< endl;
}
A A::operator =(const A &wzor)
{
    x=wzor.x;
    y=wzor.y;
    return *this;
}
/*-----------------------------------------------------------------------------*/
class B:public A
{
public:
    B(int X, int Y, int Z):A(X,Y),z(Z){}
    B operator =(const B &wzor);
    void write();
protected:
private:
    int z;

};
void B::write()
{
    A::write();
    cout << "z=" <<z<<endl;
}
B B::operator =(const B &wzor)
{
    (*this).A::operator=(wzor);
    z=wzor.z;
    return *this;
}

int main()
{
    A o1(1,2);
    o1.write();
    B o2(12,44,66);
    o2.write();
    B o3=o2;
    o3.write();
    return 0;
}

Brak komentarzy:

Prześlij komentarz