Robin_Dabank
Robin_Dabank

Reputation: 21

Pointer to different components of a class

I have an assignment from school to create pointers to different components of a class.

I don't understand how it works. Can someone help me with a simple program?

I have made the basic layout of what's needed. I don't know how to go about creating pointers.

#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;
    }
};

Upvotes: 2

Views: 111

Answers (2)

avenmia
avenmia

Reputation: 2605

A pointer is a reference to an area in memory.

pointer example

In the picture, foo is holds the value 1702 which is the spot in memory the string "hello" is stored. Pointers to elements in a class work the same way. Your class will occupy some part of memory and a pointer to the class member will hold the value of where the class member is in memory.

I'm not sure which type of pointer you're supposed to use for your class, but there's three different types.

Raw pointers: These are the types similar to shown in the picture. An example would be:

int * x = 5; // Let's say 5 is stored at memory location 0x15
cout << x; // This will give 0x15
cout << *x; // This "dereferences" the pointer also known as go to that memory location and retrieve the value. This outputs 5

There are also Smart Pointers as defined here: https://learn.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp?view=vs-2019

These are meant to be safer since they will be garbage collected, and prevent common dereferencing errors.

For using pointers in a class it could be as easy as:

class shape {
  int * height;
  int * width;
  public:
    void setHeight (int x) {height = &x; }
    void setWidth(int x) { width = &x; }
    int getHeight(){ return *height; }
    int getWidth() { return *width; }
};

class square : class shape {
  public getArea(int *h, int *w) {returns *h * *w; }
};

int main {
  int x = 5;
  int y = 6;
  int * pointerX = &x; //& means this variable's memory address
  int * pointerY = &y;
  rect rectangle;
  std::cout << rectangle.getArea(pointerX, pointerY) << std::endl;
  rectangle.setHeight(7);
  std::cout << "Rect height:" << rectangle.getHeight() << std::endl;
  rectangle.setWidth(9);
  std::cout << "Rect width:" << rectangle.getWidth() << std::endl;

  rect * ptrRect = &rectangle;
  std::cout << ptrRect->getArea(pointerX, pointerY) << std::endl;
  ptrRect->setHeight(9);
  std::cout << "ptrRect height:" << ptrRect->getHeight() << std::endl;
  ptrRect->setWidth(10);
  std::cout << "ptrRect width:" << ptrRect->getWidth() << std::endl;



  std::cout << square.getArea(pointerX, pointerY) << std::endl;

}

Upvotes: 1

Blue Dice
Blue Dice

Reputation: 155

#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;
}

I believe this is what you were looking for.

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.

Upvotes: 2

Related Questions