Băi tập chương

Một phần của tài liệu LẬP TRÌNH ĐỐI TƯỢNG CHƯƠNG 6 (Trang 38 - 43)

VI/ Lớp cơ sở ảo (virtual base class)

Băi tập chương

1. Hêy tạo lớp cơ sở chung building để lưu trữ số tầng nhă mă một toă nhă có số phòng vă số tổng diện tích dưới dạng protected. Hêy tạo lớp dẫn xuất house kế thừa building vă lưu trữ số phòng ngũ vă phòng tắm. Cũng vậy, tạo lớp dẫn xuất office kế thừa public lớp building vă lưu trữ số bình cứu hoả vă số mây điện thoại. Cả hai lớp dẫn xuất đề có hăm tạo vă hăm show. Viết chương trình thực hiện yíu cầu trín ? 2. Cho đoạn chương trình sau, hêy bổ sung câc chi tiết theo chỉ dẫn trong câc lời chú giải

#include <iostream.h> class planet {

protected:

double distance; // miles from the sun int revolve; // in days public:

planet(double d, int r) { distance = d; revolve = r; } };

class earth : public planet {

double circumference; // circumference of orbit public:

// Create earth(double d, int r). Have it pass the distance and days of revolution back

// to planet. Have it compute the circumference of the orbit. // (Hint : circumference = 2r*3.1416.)

// Create a function called show() that displays the information. }; int main() { earth ob(93000000, 365); ob.show(); return 0; }

// Overload the +, -, and = relative to coord class. Then, use coord as a base for quad.

#include <iostream.h> class coord {

public:

int x, y; // coordinate values coord() { x=0; y=0; }

coord(int i, int j) { x=i; y=j; }

void get_xy(int &i, int &j) { i=x; j=y; } coord operator+(coord ob2);

coord operator-(coord ob2); coord operator=(coord ob2); };

// Overload + relative to coord class. coord coord::operator+(coord ob2) {

coord temp;

cout << "Using coord operator+()\n"; temp.x = x + ob2.x;

temp.y = y + ob2.y; return temp;

}

// Overload - relative to coord class. coord coord::operator-(coord ob2) {

coord temp;

cout << "Using coord operator-()\n"; temp.x = x - ob2.x;

temp.y = y - ob2.y; return temp;

}

// Overload = relative to coord. coord coord::operator=(coord ob2) {

cout << "Using coord operator=()\n"; x = ob2.x;

y = ob2.y;

return *this; // return the object that is assigned to } (adsbygoogle = window.adsbygoogle || []).push({});

class quad : public coord {

int quadrant; public:

quad() { x = 0; y = 0; quadrant = 0; } quad(int x, int y) : coord(x, y) {

if(x>=0 && y>=0) quadrant = 1; else if(x<0 && y>=0) quadrant = 2;

else if(x<0 && y<0) quadrant = 3; else quadrant = 4;

}

void showq() {

cout << "Point in Quadrant: " << quadrant << '\n'; }

quad operator=(coord ob2); };

quad quad::operator=(coord ob2) {

cout << "Using quad operator=()\n"; x = ob2.x;

if(x>=0 && y>=0) quadrant = 1; else if(x<0 && y>=0) quadrant = 2;

else if(x<0 && y<0) quadrant = 3; else quadrant = 4; return *this; } int main() {

quad o1(10, 10), o2(15, 3), o3; int x, y;

o3 = o1 + o2; // add two objects - this calls operator+() o3.get_xy(x, y);

o3.showq();

cout << "(o1+o2) X: " << x << ", Y: " << y << "\n"; o3 = o1 - o2; // subtract two objects o3.get_xy(x, y);

o3.showq();

cout << "(o1-o2) X: " << x << ", Y: " << y << "\n"; o3 = o1; // assign an object

o3.get_xy(x, y); o3.showq();

cout << "(o3=o1) X: " << x << ", Y: " << y << "\n"; return 0;

}

4. Hêy sửa đổi chương trình trín sao cho nó sử dụng câc hăm toân tử friend ?

5. Hêy tạo một phđn cấp lớp để lưu trữ thông tin về mây bay. Hêy bắt đầu bằng một lớp cơ sở tín lă airship chứa thông tin về số lượng hănh khâch tối đa vă trọng lượng hăng hoâ tối đa (đơn vị tính lă pound) mă mây bay có thể chở được.

Tạo hai lớp dẫn xuất airplane vă balloons. Lớp airplane lưu kiểu của động cơ (gồm động cơ cânh quạt vă động cơ phản lực), tầm xa (đơn vị tính lă mile). Lớp balloon lưu thông tin về loại nhiín liệu sử dụng chi khí cầu (gồm hai loại lă hydrogen vă helium), độ cao tối đa (đơn vị tính lă feed). Viết chương trình minh hoạ hoạt động của chúng.

Một phần của tài liệu LẬP TRÌNH ĐỐI TƯỢNG CHƯƠNG 6 (Trang 38 - 43)