gpt4 book ai didi

c++ - 如何从存储在列表中的对象调用成员函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:10 25 4
gpt4 key购买 nike

我有几个对象存储在一个列表中。如何从列表中的对象调用成员函数?

我需要访问存储在列表中的每个对象并为每个对象调用 getX 函数来对对象进行排序。如果有比比较每个对象的 X 值并交换它们在列表中的位置更好的时间,我不确定该怎么做。

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

最佳答案

您需要使用比较器函数根据一些自定义标准对列表进行排序。例如,如果你想在 x 上排序,你应该创建一个全局函数:

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

然后调用sort函数:

shapeList.sort(compare_x);

这将根据 x 的值对列表进行排序。如果您使用的 C++ 高于 C++11,您还可以使用 lambda 函数:

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

在这种情况下,您无需创建显式比较器函数。

关于c++ - 如何从存储在列表中的对象调用成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43701433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com