Peter’s dog John’s dog




Дата канвертавання16.04.2016
Памер38.54 Kb.

Inheritance

This is a dog object. It contains the dog’s feature.


dog


However, Peter’s dog is different from John’s dog.


Peter’s dog

John’s dog

colour: black


colour: yellow

interest: sleeping

interest: eating

owner: Peter

eat_vegetable: yes




owner: John

But the dog feature is the same.

In OOP, Peter’s dog and John’s dog is a separate object but they can share the dog object for the same dog feature. This is inheritance in OOP.



dog

dog1 dog2

Overriding

According to the above diagram, dog 1 does not eat vegetable. It is because dog1 inherits her parent’s properties.


It is clear that dog2 eats vegetable, it is because it set the properties in its class again. That can override the parent’s properties.

#include

#include

#include


class dog {

public :


char tail[4];

int eye;


int legs;

char eat_vegetable[4];

dog();

};
dog::dog()



{ strcpy(tail,"Yes");

eye=2;


legs=4;

strcpy(eat_vegetable,"No");

}
class dog1:public dog {

public :


char colour[30];

char interest[30];

char owner[30];

dog1();


};
dog1::dog1()

{ strcpy(colour,"black");

strcpy(interest,"sleeping");

strcpy(owner,"peter");

}
class dog2:public dog {

public :


char colour[30];

char interest[30];

char eat_vegetable[30];

char owner[30];

dog2();

};
dog2::dog2()



{ strcpy(colour,"yellow");

strcpy(interest,"eating");

strcpy(eat_vegetable,"yes");

strcpy(owner,"john");

}
void main()

{ dog1 *d1;

dog2 *d2;
d1=new dog1();

d2=new dog2();


clrscr();

cout << "Dog 1" << endl;

cout << "tail:" << d1->tail << endl;

cout << "eye:" << d1->eye << endl;

cout << "legs:" << d1->legs << endl;

cout << "eat_vegetable:" << d1->eat_vegetable << endl;

cout << "colour:" << d1->colour << endl;

cout << "interest:" << d1->interest << endl;

cout << "owner:" << d1->owner << endl;
cout << endl << "Dog 2" << endl;

cout << "tail:" << d2->tail << endl;

cout << "eye:" << d2->eye << endl;

cout << "legs:" << d2->legs << endl;

cout << "eat_vegetable:" << d2->eat_vegetable << endl;

cout << "colour:" << d2->colour << endl;

cout << "interest:" << d2->interest << endl;

cout << "owner:" << d2->owner << endl;

delete d1;

delete d2;

}

Exercise:
Modify the above program, let the dog object inherits backbone object. The backbone object contains a property “bbone = Yes”.

Class member

There are three types of class member. “public, private and protected”. In the above example, the parent object’s properties fully disclose to its child object. Class member is used to restrict the access right of the child object or the access right outside object.







Object itself

Child object

Outside object

Public







Private









Protected











backbone

dog








dog1 dog2
main
#include

#include

#include
class backbone {

public:


char bbone[4];

backbone();

};
backbone::backbone()

{ strcpy(bbone,"Yes");

}
class dog:public backbone {

public:


dog();

char eat_vegetable[4];

protected:

int eye;


int legs;

private:


char tail[4];

};
dog::dog()

{ strcpy(tail,"Yes");

eye=2;


legs=4;

strcpy(eat_vegetable,"No");

}

class dog1:public dog {



public :

char colour[30];

char interest[30];

dog1();
private:

char owner[30];

};
dog1::dog1()

{ strcpy(colour,"black");

strcpy(interest,"sleeping");

strcpy(owner,"peter");

}
class dog2:public dog {

public :

char eat_vegetable[30];

char owner[30];

dog2();


protected:

char colour[30];

private:

char interest[30];

};
dog2::dog2()

{ strcpy(colour,"yellow");

strcpy(interest,"eating");

strcpy(eat_vegetable,"yes");

strcpy(owner,"john");

}
void main()

{ dog1 *d1;

dog2 *d2;


d1=new dog1();

d2=new dog2();

clrscr();

cout << "Dog 1" << endl;

cout << "backbone :" << d1->bbone << endl;

cout << "eat_vegetable:" << d1->eat_vegetable << endl;

cout << "colour:" << d1->colour << endl;

cout << "interest:" << d1->interest << endl;

cout << endl << "Dog 2" << endl;

cout << "backbone :" << d2->bbone << endl;

cout << "eat_vegetable:" << d2->eat_vegetable << endl;

cout << "owner:" << d2->owner << endl;

delete d1;

delete d2;

}

Return a private member by using a public member



In the main function, eye is not accessible. It is because it is a private member. However, a public function that is inside an object so it can access a private member. A public member can be accessed from outside. In this example, f_eye function can read the value of eye but it can’t change it.
#include
class dog

{ public:

dog();

int f_eye();


private:

int eye;


};
dog::dog()

{ eye=2;


}
int dog::f_eye()

{ return eye;

}
void main()

{ dog *d;


d=new dog();

cout << d->f_eye() << endl;

delete d;

}
A friend function can access a private variable that outside the class.

#include
class dog

{ friend void eye(dog *); // friend function

public: dog();
private:

int eye;


};
dog::dog()

{ eye=2;


}
void eye(dog *d) // friend function that output the class

{ cout << d->eye << endl;

}
void main()

{ dog *d;


d=new dog();

/* error, because of private member */

// cout << d->eye << endl;

eye(d);
delete d;



}


OOP Lecture 2, Page /


База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі

    Галоўная старонка