C++ 基础练习 - Chapter 9 (英文版)

Review Questions:

9.1 What does polymorphism (多态性) mean in C++?

Answer:

Using the same name of operators or functions in different ways.

9.2 How is polymorphism achieved at (a) compile time, and (b) run time?

Answer:

Polymorphism can be achieved at compile time by early binding. Early time means an object is bound to its function call at compile time.

We can achieve run time polymorphism by mechanism known as virtual function.

9.3 Discuss the different ways by which we can access public member functions of an object.

Answer:

We can access public member functions of an object by:

(1). Object name and dot membership operator;

(2). Pointer to object and function name.

9.4 Explain, with an example, how you would create space for an array of objects using pointers.

Answer:

We can also create an array of objects using pointers. Example:

item *ptr = new item[10]; // array of 10 objects

create memory space for an array of 10 objects of item.

9.5 What does this pointer point to?

Answer:

“this” pointer points to invoking object.

9.6 What are the applications of “this” pointer?

Answer:

One important application of the pointer “this” is to return the object it points to. Example:

return *this;

inside a member function will return the object that invoked the function.

9.7 What is a virtual junction?

Answer:

When we use the same fuction name in both the base and derived classes the function in the base class is declared as virtual using the keyword “virtual” preceding its normal declaration.

9.8 Why do we need virtual function?

Answer:

When we need same function name at base class and derived class, we need virtual function in base class.

9.9 When do we make a virtual function “pure”? What are the implications of making a function a pure virtual function?

Answer:

When a function is defined as empty, then this function is called do nothing function. The implications of making a function a pure virtual function is to achieve run time polymorphism.

9.10 State which of the following statements are TRUE or FALSE.

a. Virtual functions are used to create pointers to base classes. (TRUE)

b. Virtual functions allow us to use the same junction call to invoke member functions of objects of different classes. (TRUE)

c. A pointer to a base class cannot be made to point to objects of derived class. (FALSE)

d. “this” pointer points to the object that is currently used to invoke a function. (TRUE)

e. “this” pointer can be used like any other pointer to access the members of the object it points to. (TRUE)

f. “this” pointer can be made to point to any object by assigning the address of the object. (TRUE)

g. Pure virtual fucntions force the programmer to redefine the virtual function inside the derived classes. (TRUE)

Debugging Exercises:

9.1 Identify the error in the following program.

#include <iostream>
using namespace std;

class Info
{
private:
    char* name;
    int number;

public:
    void getInfo()
    {
        cout << "Info::getInfo";
        getName();
    }
    
    void getName()
    {
        cout << "Info::getName";
    }
};

class Name: public Info
{
private:
    char* name;
public:
    void getName()
    {
        cout << "Name::getName";
    }
};

int main()
{
    Info *p;
    Name n;
    p = n;
    p->getInfo();
    
    return 0;
}

Answer:

error: cannot convert ‘Name’ to ‘Info’ in assignment.*

Here p = n; will replace with p = &n; in the main() function. Because p is a pointer.

Programming Exercises:

9.1 Create a base class called shape. Use this class to store two double-type values that could be used to compute the area of figures. Derive two specific classes called triangle and rectangle from the base shape. Add to the base class, a member function get_data() to initialize base class data members and another member function display_area() to compute and display the area of figures. Make display_area() as a virtual function and redefine this function in the derived classes to suit their requirements.

Using these three classes, design a program that will accept the dimensions of a triangle or a rectangle interactively, and display the area.

Remember the two values given as input will be treated as lengths of two sides in the case of rectangles and as base and height in the case of triangle, and used as follows:

Area of rectangle = x * y

Area of triangle = 0.5 *x * y

#include <iostream>
using namespace std;

class shape
{
public:
    double x, y;
public:
    void get_data() {cin>>x>>y;}
    virtual void display_area(){}
    double get_x() {return x;}
    double get_y() {return y;}
};

class triangle: public shape
{
public:
    void display_area()
    {
        double a;
        a = x * y * 0.5;
        cout << " Area of triangle  = " << a << endl;
    }
};

class rectangle: public shape
{
public:
    void display_area()
    {
        double a;
        a = x * y;
        cout << " Area of rectangle = " << a << endl;
    }
};

int main()
{
    shape *s[2];
    triangle t;
    s[0] = &t;
    rectangle r;
    s[1] = &r;

    cout << " Enter the value of x & Y for triangle : " << endl;
    s[0]->get_data();

    cout << " Enter the value of x & Y for rectangle : " << endl;
    s[1]->get_data();


    s[0]->display_area();
    s[1]->display_area();

    return 0;
}

9.2 Extend the above program to display the area of circles. This requires addition of new derived class “circle” that computes the area of a circle. Remember, for a circle we need only one value, its radius, but the get_data() function in base class requires two values to be passed. (Hint: Make the second argument of get_data() function as a default one with zero value.)

#include <iostream>
using namespace std;

#define PI 3.1416

class shape
{
public:
    double x, y;
public:
    void get_data(double a, double b) {x = a; y = b;}
    virtual void display_area(){}
    double get_x() {return x;}
    double get_y() {return y;}
};

class triangle: public shape
{
public:
    void display_area()
    {
        double a;
        a = x * y * 0.5;
        cout << " Area of triangle  = " << a << endl;
    }
};

class rectangle: public shape
{
public:
    void display_area()
    {
        double a;
        a = x * y;
        cout << " Area of rectangle = " << a << endl;
    }
};

class circle: public shape
{
public:
    void display_area()
    {
        double a;
        a = PI * x * x;
        cout << " Area of circle = " << a << endl;
    }
};

int main()
{
    shape *s[3];
    triangle t;
    s[0] = &t;
    double x1, y1;
    cout << " Enter the value of x1 & y1 for triangle : ";
    cin >> x1 >> y1;
    s[0]->get_data(x1, y1);

    rectangle r;
    s[1] = &r;
    cout << " Enter the value of x2 & y2 for rectangle : ";
    double x2, y2;
    cin >> x2 >> y2;
    s[1]->get_data(x2, y2);

    circle c;
    double rd;
    s[2] = &c;
    cout << " Enter the radius of circle : ";
    cin >> rd;
    s[2]->get_data(rd, 0);

    s[0]->display_area();
    s[1]->display_area();
    s[2]->display_area();

    return 0;
}
  • 22
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值