Working with Class and Objects: Write below given code for vector of objects. Re
ID: 3560212 • Letter: W
Question
Working with Class and Objects:
Write below given code for vector of objects.
Reference code for a vector of objects:
#include <string>
#include <vector>
using namespace std;
class C{
int x,y; //location
public:
//constructor
C (int setX,int setY)
{
x = setX;
y = setY;
}
void print()
{
cout<<"x= "<<x<<" y= "<<y<<endl;
}
~C(){ }
};
int main()
{
vector <C> gameObjects;
gameObjects.push_back(C(0,30));
gameObjects.push_back(C(1,2));
gameObjects.at(0).print();
gameObjects.at(1).print();
return 0;
}
Write a program that holds database for students. Keep students
Explanation / Answer
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Student
{
public:
// ... various functions to perform the required operations
private:
string name, ID;
int mark;
};
int main()
{
list<Student> students;
// Read from data base
while (more_students())
{
Student temp;
temp.read();
students.push_back (temp);
}
// Now print the students that failed (mark < 60%) - of
// course, the particular Student object should provide a
// member-function (say, passed()) that will determine that
list<Student>::iterator i;
for (i = students.begin(); i != students.end(); ++i)
{
if (! i->passed()) // iterators also provide operator ->
{
cout << "The student " << *i << " failed." << endl;
// provided that class Student provides the overloaded
// stream insertion operator <<
}
}
// Now remove the failed students (of course, this could have
// been done in the previous loop)
i = students.begin()
while (i != students.end())
{
if (! i->passed())
{
i = students.erase (i);
}
else
{
++i;
}
}
// ...
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.