Search code examples
c++listfunctionmember

How to call member functions from objects stored in a list


I have several objects stored within a list. How do I call upon member functions from the objects in the list?

I need to access each object stored within the list and call the getX function for each one to sort the objects. If there is a better when to do this than compare the X values of each object and swap their positions within the list, I am not sure how to do it.

#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include <algorithm>
#include <conio.h>

#define sz 12

using namespace std;

class Point
{
private:
    int x, y;
public:
    Point() { }
    Point(int a, int b)
        :x(a), y(b) { }
    // print function is pure virtual and that makes class Point an abstract class
    // a pure virtual function can have prototype only without definition
    // an abstract class can't be instantiated
    // its derived class must override this function in order to be a real class
    virtual void print() const = 0;
    int getX(); // added function to get x value
    int getY(); // function to get Y value
};

void Point::print() const
{
    cout << "\nPoint: ( "
         << x
         << " , "
         << y
         << " )";
}

int Point::getX()
{
    return x;
}

int Point::getY()
{
    return y;
}

///////////////////////////////////////////////////////////////////

class Circle : public Point
{
private:
    int radius;
public:
    Circle() : Point() { }
    Circle(int a, int b, int c)
        :Point(a, b), radius(c) { }

    virtual void print() const;
};

void Circle::print() const
{
    cout << "\nCenter of the Circle is at:  ";
    Point::print();
    cout << "\nRadius of the Circle is: "
         << radius;
    cout << endl; // inserted endl
}

/////////////////////////////////////////////////////////////////////

class Cylinder : public Circle
{
private:
    int height;
    char color[sz];
public:
    Cylinder() { }
    Cylinder(int a, int b, int r, int h, char clr[])
    :   Circle(a, b, r), height(h)
    { strcpy(color, clr); }

    virtual void print() const;
};

void Cylinder::print() const
{
    Circle::print();

    cout << "\nHeight of Cylinder is: "
        << height
        << "\nColor of Cylinder is: "
        << color;
    Point::print();
         cout << endl;
}

void load_list(list<Point*>&, char*); // Create list of shapes
void output(Point*&); // display function for for_each 

///////////////////////////////////////////////////////////////
int main()
{
    char clr[10];

    list<Point*> shapeList;////
    list<Point*>::iterator it;

    load_list(shapeList, clr);

    for_each(shapeList.begin(), shapeList.end(), output);

    shapeList.sort();

    cout << "\n\n///////////////////\n"
        << "////////////////////\n "
        << "After list is Sorted: \n";

    for_each(shapeList.begin(), shapeList.end(), output);
    _getch();

    return 0;
}

void load_list(list<Point*>& ptList, char *ch)
{
    char type;
    int x, y, r, h;

    ifstream infile("shapes.txt");
    if (!infile)
    {
        cout << "\nCan not open input file.";
        exit(1);
    }

    infile >> type;

    while (infile)
    {
        if (type == 'c')
        {
            infile >> x >> y >> r;
            ptList.push_back(new Circle(x,y,r));
        }
        else if (type = 'l')
        {
            infile >> x >> y >> r >> h >> ch;
            ptList.push_back(new Cylinder(x, y, r, h, ch));
        }
        infile >> type;
    }
}

void output(Point*& point)
{
    point->print();
}

void sort_list_by_x(list<Point*>& pt)
{
    list<Point*>::iterator it;
    list<Point*>::iterator it2;

    auto it2 = pt.begin();
    auto it = it2++;
    auto e = pt.end();

    it->getX;
}

Solution

  • You need to use a comparator function to sort the list according to some custom criterion. For instance, if you want to sort on x, you should make a global function:

    bool compare_x (const Point* first, const Point* second)
    {
        return (first -> getX() < second -> getX());
    }
    

    And then call the sort function:

    shapeList.sort(compare_x);
    

    This will sort the list based on the values of x. If you are using C++ greater than C++11, you can also use lambda functions:

    shapeList.sort([](const Point* first, const Point* second) {
        return (first -> getX() < second -> getX());
    });
    

    You need not make an explicit comparator function in that case.