简体   繁体   中英

Accessing member functions through pointers

Why am I getting the address in the output. Rather I should get the Output Length= (value input by user) , Width = (value input by user). As in the main body of program after getting input R1.getdata() , ptr->result() should display the result of Rectangle class.

#include <iostream>

using namespace std;

class Rectangle {
   protected:
    float length;
    float width;

   public:
    void getdata() {
        cout << "Enter length and width= ";
        cin >> length >> width;
    }
    void result() {
        cout << "Length = " << length << "\nWidth = " << width << endl;
    }
};
class Area : public Rectangle {
   private:
    float area;

   public:
    void calc_area() { area = length * width; }
    void result() { cout << "Area = " << area << endl; }
};
class Perimeter : public Rectangle {
   private:
    float perimeter;

   public:
    void calc_peri() { perimeter = 2 * (length + width); }
    void result() { cout << "Perimeter = " << perimeter << endl; }
};
void main() {
    Rectangle R1;
    Area A1;
    Perimeter P1;
    Rectangle *ptr;
    R1.getdata();
    ptr = &A1;
    ptr->result();
}

You are getting the wrong values, because you are calling ptr->result(); on a uninitialized Area object (A1), which has been upcasted from pointer to Rectangle object.

The values the user inputs though are used in the R1 object, which you then don't use anymore. Moreover, you should make the result() method virtual.

Lastly, the syntax for calling base class method on a pointer to an inheriting class is : ptr->Rectangle::result(); .

Below you will find your code with some fixes that demonstrate things I wrote about:

#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Rectangle {
   protected:
    float length;
    float width;

   public:
    void getdata() {
        cout << "Enter length and width= ";
        cin >> length >> width;
        std::cout << length << "  " << width << std::endl;
    }
    virtual void result() {
        cout << "(Rectangle) Length = " << length << "\nWidth = " << width
             << endl;
    }
};
class Area : public Rectangle {
   private:
    float area;

   public:
    void calc_area() { area = length * width; }
    void result() { cout << "Area = " << area << endl; }
};
class Perimeter : public Rectangle {
   private:
    float perimeter;

   public:
    void calc_peri() { perimeter = 2 * (length + width); }
    void result() { cout << "Perimeter = " << perimeter << endl; }
};
int main() {
    Rectangle R1;
    Area* A1;
    Perimeter P1;
    Rectangle* ptr;
    R1.getdata();
    ptr = &R1;
    A1 = static_cast<Area*>(ptr);
    // or:
    // A1 = (Area*)ptr;
    ptr->Rectangle::result();
}

Ptr 指向 Rectangle 类(Area 类)的一个子类的地址,因此它调用它所引用的对象(A1 类型的 A1)的成员(结果)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM