c++ - 指向类的不同组件的指针

标签 c++

学校给我布置了一项作业,要求我创建指向类的不同组件的指针。

我不明白它是如何工作的。有人可以帮我编写一个简单的程序吗?

我已经做了所需的基本布局。我不知道如何创建指针。

#include <iostream>
#include <math.h>
using namespace std;

class Rectangle
{
    int a,b;
    public:
};

class Perimeter : public Rectangle
{
public:
    int c;
    void P(int a, int b)
    {
        c = 2 * (a + b);
        cout << "This Is The Perimeter Of The Rectangle: " << c << endl;
    }
};

class Area : public Rectangle
{
public:
    int c;
    void A(int a, int b)
    {
        c = a * b;
        cout << "This Is The Area Of The Rectangle: " << c << endl;
    }
};

class Diagonal : public Rectangle
{
public:
    float c;
    void D(int a, int b)
    {
        c = sqrt((a*a)+(b*b));
        cout << "This Is The Diagonal Of Rectangle: " << c << endl;
    }
};

最佳答案

#include<iostream>
#include<math.h>
using namespace std;
class Rectangle
{
        int a,b;
        public:
};
class Perimeter : public Rectangle
{
        public:
                int c;
                void P(int a, int b)
                {
                        c = 2 * (a + b);
                        cout<<"This Is The Perimeter Of The Rectangle: "<<c<<endl;
                }
};
class Area : public Rectangle
{
        public:
                int c;
                void A(int a, int b)
                {
                        c = a * b;
                        cout<<"This Is The Area Of The Rectangle: "<<c<<endl;
                }
};
class Diagonal : public Rectangle
{
        public:
                float c;
                void D(int a, int b)
                {
                        c = sqrt((a*a)+(b*b));
                        cout<<"This Is The Diagonal Of Rectangle: "<<c<<endl;
                }
};
int main()
{
        int e,f;
        cout<<"Enter Length And Breadth: "<<endl;
        cin>>e>>f;
        /***************************************/
        Perimeter p;            //CREATING AN OBJECT
        Perimeter *Peri;        //CREATING A POINTER TO THE OBJECT
        Peri=&p;                //ASSIGNING ADDRESS TO THE POINTER
        Peri->P(e,f);           //MEMBER ACCESS USING POINTER TO AN OBJECT
        /**************************************/
        Area a;
        int Area::*ptr=&Area::c;        //CREATING A POINTER TO THE DATA MEMBER
        a.*ptr = e;
        a.A(e,f);
        /*************************************/
        Diagonal d;
        void (Diagonal::*Dia)(int,int)=&Diagonal::D;    //CREATING POINTER TO MEMBER FUNCTION
        (d.*Dia)(e,f);                                  //THIS IS HOW WE CALL THE MEMBER FUNCTION USING ITS POINTER
        /*************************************/
        return 0;
}

我相信这就是您正在寻找的。

there are some errors you made in the program. i didn't correct them but i am pointing them out. though you didn't write anything(create any functions) in the parent class, creating pointer to an object of the sub-class is useless. in this case, early binding is taking place. you can go with a pure virtual function following function Over-Riding.

关于c++ - 指向类的不同组件的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56677810/

相关文章:

c++ - 哪些替换失败在 requires 子句中是不允许的?

c++ - 为什么我在控制台做双缓冲时会出现随机符号?

c++ - 复制构造函数的 const 正确性问题?

c++ - 迭代器成员行为

c++ - 在 C++ 中使用二维数组

c++ - 这个 nm 输出是什么意思?

c++ - 如何解决 numeric_limits<T>::min() 的不一致定义?

c++ - 使用 QtCreator(qmake) 编译 wtwithqt 示例

c++ - 在循环中创建新线程是否安全?

c++ - 查找 NaN 错误的原因