Giải pháp: Hàm ảo class Rectangle { ..... 7.4 Ví dụ thư viện khối chức năng Bài toán: — Xây dựng một thư viện các khối chức năng phục vụ tính toán và mô phỏng tương tự trong SIMULINK
Trang 1Chương trình minh họa 2
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ản lý các ₫ối tượng chung trong một danh sách nhờ cơ chế dẫn xuất!
Kết quả: các hàm thành viên của lớp dẫn xuất
cũng không ₫ược gọi
Rectangle: [(0,0)(50,100)]
Rectangle: [(0,0)(50,50)]
Rectangle: [(0,0)(10,10)]
Trang 2Giải pháp: Hàm ảo
class Rectangle {
public:
virtual void draw();
}
Trang 3Rectangle: [(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
Trang 4Hà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() { }
};
Trang 5Ví 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();
};
Trang 6class 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;
}
Trang 7void 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ả:
Trang 87.4 Ví dụ thư viện khối chức năng
Bài toán:
— Xây dựng một thư viện các khối chức năng phục vụ tính toán và
mô phỏng tương tự trong SIMULINK
— Viết chương trình minh họa sử dụng ₫ơn giản
Ví dụ một sơ ₫ồ khối
Sum 0