Tải bản đầy đủ (.pdf) (8 trang)

Kỹ thuật lập trình: Quan hệ lớp là gì? phần 4 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (232.33 KB, 8 trang )

25
© 2004, HOÀNG MINH SƠN
Chương 7: Quan hệ lớp
©
2007 AC - HUT
Chương trình minh họa2
void main()
{
const N =3;
Rectangle rect(0,0,50,100);
Square square(0,0,50);
TextBox text("Hello");
Rectangle* shapes[N] = {&rect, &square, &text};
for (int i = 0; i < N; ++i)
shapes[i]->draw();
getch();
}
Quảnlýcác₫ốitượng
chung trong một danh sách
nhờ cơ chế dẫnxuất!
Kếtquả: các hàm thành viên củalớpdẫnxuất
cũng không ₫ượcgọi
Rectangle: [(0,0)(50,100)]
Rectangle: [(0,0)(50,50)]
Rectangle: [(0,0)(10,10)]
26
© 2004, HOÀNG MINH SƠN
Chương 7: Quan hệ lớp
©
2007 AC - HUT
Giảipháp: Hàmảo


class Rectangle {

public:

virtual void draw();
}
27
© 2004, HOÀNG MINH SƠN
Chương 7: Quan hệ lớp
©
2007 AC - HUT
Kếtquả: Như mong muốn!
Rectangle: [(0,0)(50,100)]
Square: [(0,0)(50,50)]
Textbox: [(0,0)(10,10) Hello]
Now they are moved
Rectangle: [(10,20)(60,120)]
Square: [(10,20)(60,70)]
Textbox: [(10,20)(20,30) Hello]
Now they are resized
Rectangle: [(20,40)(120,240)]
Square: [(20,40)(120,140)]
Textbox: [(20,40)(40,60) Hello]
Chương trình 1
Rectangle: [(0,0)(50,100)]
Square: [(0,0)(50,50)]
Textbox: [(0,0)(10,10) Hello]
Chương trình 2
28
© 2004, HOÀNG MINH SƠN

Chương 7: Quan hệ lớp
©
2007 AC - HUT
Hàm ảo
class X {

public:
virtual void f1() { }
virtual void f2() { }
virtual void f3() { }
void f4() { }
};
void function()
{
Y y;
X* px = &y; //Typ-Convert Y* to X*
px->f1(); //virtual function Y::f1()
px->f2(); //virtual function X::f2()
px->f3(); //virtual function X::f3()
px->f4(); //function X::f4()
}
class Y:public X {

public:
void f1() { }
void f2(int a) { }
char f3() { }
void f4() { }
};
29

© 2004, HOÀNG MINH SƠN
Chương 7: Quan hệ lớp
©
2007 AC - HUT
Ví dụ hàm ảo
class X {
protected:
int x;
public:
X(int x_init) { x = x_init;}
virtual void print();
};
class Y:public X {
protected:
int y;
public:
Y(int x_init, int y_init):X(x_init) {y = y_init;}
void print();
};
class Z:public Y {
protected:
int z;
public:
Z(int x_init, int y_init, int z_init):Y(x_init, y_init)
{z = z_init;}
void print();
};
30
© 2004, HOÀNG MINH SƠN
Chương 7: Quan hệ lớp

©
2007 AC - HUT
class U:public Y {
protected:
int u;
public:
Z(int x_init, int y_init, int u_init):Y(x_init, y_init)
{u = u_init;}
void print();
};
void X::print()
{
cout << “Data of Class X: “ << x << endl;
}
void Y::print()
{
cout << “Data of Class X+Y: “ << x + y << endl;
}
void Z::print()
{
cout << “Data of Class X+Y+Z: “ << x + y + z << endl;
}
void U::print()
{
cout << “Data of Class X+Y+U: “ << x + y + u << endl;
}
31
© 2004, HOÀNG MINH SƠN
Chương 7: Quan hệ lớp
©

2007 AC - HUT
void print_data(X* px)
{
px->print();
}
main()
{
X* pobjX = new X(1);
Y* pobjY = new Y(10, 20);
Z* pobjZ = new Z(100, 200, 300);
U* pobjU = new U(1000, 2000, 3000);
print_data(pobjX);
print_data(pobjY);
print_data(pobjZ);
print_data(pobjU);
delete pobjX;
delete pobjY;
delete pobjZ;
delete pobjU;
}
main()
{
int x;
X *pobj[4];
pobj[0] = new X(1);
pobj[1] = new Y(10, 20);
pobj[2] = new Z(100, 200, 300);
pobj[3] = new U(1000, 2000,
3000);
for(x = 0; x < 4; x++)

print_data(pobj[x]);
delete[4] pobj;
}
Data of Class X: 1
Data of Class X+Y: 30
Data of Class X+Y+Z: 600
Data of Class X+Y+U: 6000
Kết quả:
32
© 2004, HOÀNG MINH SƠN
Chương 7: Quan hệ lớp
©
2007 AC - HUT
7.4 Ví dụ thư viện khốichứcnăng
 Bài toán:
—Xâydựng mộtthư việncáckhốichứcnăng phụcvụ tính toán và
mô phỏng tương tự trong SIMULINK
—Viếtchương trình minh họasử dụng ₫ơngiản
 Ví dụ mộtsơ₫ồkhối
StaticGain Limiter
Integrator
Sum
0

×