Professional Documents
Culture Documents
(Inheritance)
Khi nim
qun l nhn s ca cng ty, ta c th nh ngha cc lp tng ng vi
cc v tr lm vic ca cng ty:
class Worker {
private:
string name;
float salary;
int level;
public:
string getName() {...}
void pay() {...}
void doWork() {...}
...
};
class Manager {
private:
string name;
float salary;
int dept;
public:
string getName() {...}
void pay() {...}
void doWork() {...}
...
};
class Director {
private:
string name;
float salary;
public:
string getName() {...}
void pay() {...}
void doWork() {...}
...
};
S dng li code
D bo tr, sa i v sau
Worker
Employee
Manager
Director
K tha public
class Employee {
private:
string name;
float salary;
public:
...
string getName() {...}
void pay() {...}
};
class Worker : public Employee {
private:
int level;
public:
...
void doWork() {...}
void show() {
cout << getName()
<< salary; // li
}
};
Worker w;
w.getName();
w.doWork();
w.pay();
w.salary = 10; // li
w.show();
Employee e = w; // OK
Worker w2 = e;
// li
Worker w3 = (Worker)e; // li
K tha private
class LinkedList {
private:
...
public:
void insertTail(int
void insertHead(int
void deleteHead() {
void deleteTail() {
int getHead() { ...
int getTail() { ...
...
};
x) { ... }
x) { ... }
... }
... }
}
}
int pop() {
int x = getHead();
deleteHead();
return x;
}
...
};
Stack s;
s.push(10);
s.push(20);
s.pop();
s.insertTail(30); // li
s.getTail();
// li
class Employee {
protected:
string name;
float rate;
int hours;
int getSalary()
{ return rate*hours; }
public:
void setName(const char* s)
{ name = s; }
string getName()
{ return name; }
void pay() { ... }
...
};
6
Phm vi
private
protected
public
private
(khng)
(khng)
(khng)
protected
private
protected
protected
public
private
protected
public
class Pet {
public:
Pet() {...}
Pet(string name) {...}
};
class Dog: public Pet {
public:
Dog() {...} // Pet()
Dog(string name): Pet(name) {...}
};
class Bird {
public:
Bird(bool canFly) {...}
};
class Eagle: public Bird {
public:
// sai: Eagle() {...}
Eagle(): Bird(true) {...}
};
~Dog() ~Pet()
~Eagle() ~Bird()
EE3490: K thut lp trnh HK1 2013/2014
TS. o Trung Kin H Bch khoa H Ni
class Shape {
void main() {
public:
Circle c;
virtual void draw()
Shape s1 = c;
{ cout<<"Shape::draw\n"; }
Shape& s2 = c;
Kt qu chy:
void erase()
Shape* s3 = &c;
Circle::erase
}
bt buc { cout<<"Shape::erase\n";
Circle::draw
void redraw()
c.draw();
Shape::erase c.erase();
{ erase(); draw(); }
s1.erase();
s1.draw();
Shape::draw
};
s2.draw();
Shape::erase s2.erase();
Circle::draw s3->erase(); s3->draw();
c th b
class Circle: public Shape {Shape::erase
public:
Circle::draw c.redraw();
Shape::erase s1.redraw();
virtual void draw()
Circle::draw
{ cout<<"Circle::draw\n";
}
s2.redraw();
Shape::erase s3->redraw();
void erase()
Shape::draw
{ cout<<"Circle::erase\n";
}
}
Shape::erase
};
10
Circle::draw
Shape::erase
Circle::draw
class Shape {
public:
virtual void draw() = 0;
virtual void erase() = 0;
virtual void area() = 0;
void redraw() { ... }
};
class Circle: public Shape {
public:
...
virtual void draw() { ... }
virtual void erase() { ... }
11
// li
Circle c;
Shape p2 = c;
Shape& p3 = c;
Shape* p4 = &c;
// li
// OK
// OK
class Pet {
public:
virtual void say() = 0;
};
class Cat: public Pet {
public:
virtual void say()
{ cout << "miao\n"; }
};
class Dog: public Pet {
public:
virtual void say()
{ cout << "gruh\n"; }
};
12
Pet* p[3] = {
new Dog(), new Cat(), new Cat() };
for (int i=0; i<3; i++)
p[i]->say();
// ...
// Th ny khng c:
// Pet p2[2] = { Dog(), Cat() };
// ...
Kt qu chy:
gruh
miao
miao
EE3490: K thut lp trnh HK1 2013/2014
TS. o Trung Kin H Bch khoa H Ni
Destructor o
class ClassA {
public:
ClassA() { ... }
~ClassA() { ... }
};
class ClassA {
public:
ClassA() { ... }
virtual ~ClassA() { ... }
};
delete b;
delete a;
delete b;
delete a;
// ~ClassB, ~ClassA
// ~ClassA
// ~ClassB, ~ClassA
// ~ClassB, ~ClassA
double z;
void f3();
virtual void fv2();
virtual void fv3();
};
V3 v3;
V2& v2 = v3;
14
vtable
vtable
fv2()
fv3()
V3
Kt qu chy:
1245000 20
1245000 28
1245004 1245012 1245020
Thnh Kch
phn thc
V2
class V2 {
public:
double x, y;
static int i;
void f2();
virtual void fv2();
};
class Camera {
public:
void takePicture();
...
};
class FMDevice {
public:
void turnOn();
void turnOff();
void setFreq(float f);
...
};
class Phone {
public:
void call(string num);
...
};
15
class CellPhone:
public Camera,
protected FMDevice,
public Phone
{
public:
void turnFMOn();
void turnFMOff();
void setFMFreq(float f);
...
};
CellPhone p;
p.takePicture();
p.turnOn();
// li
p.turnFMOn();
p.call("0912345678");
EE3490: K thut lp trnh HK1 2013/2014
TS. o Trung Kin H Bch khoa H Ni
// li
p1.Legged::move();
p1.Winged::move();
((Legged&)p1).move();
((Winged&)p1).move();
//
//
//
//
Legged
Winged
Legged
Winged
class B1
double a, b;
virtual void fb1();
D d;
B1& b1 = d;
B2& b2 = d;
printf("%d %d\n", &d, sizeof(d));
printf("%d %d\n", &b1, sizeof(b1));
printf("%d %d\n", &b2, sizeof(b2));
17
fb1()
vtable
fd()
vtable
vtable
fb2()
B2
Cc thnh phn ca cc lp c s
nm ni tip nhau trong b nh
Lp k tha o: t tm hiu thm
vtable
Thnh Kch
phn thc
class D
float d;
virtual void fb1();
virtual void fb2();
virtual void fd();
B1
Kt qu chy:
1244996 32
1244996 20
1245016 8
class B2
int c;
virtual void fb2();
Bi tp
1.
2.
ln chy sau:
3.
4.
5.
M rng v sa bi tp trn:
Thm quan h v cng vic gia cc nhn vin. VD: mi Worker c 1 Manager,...
6.
18