Answers for "multiple objects in vector C++"

C++
0

multiple objects in vector C++

#include <iostream>
#include <vector>

using namespace std;


class Student
{
public:

	string name;
	int age;

	Student(string name, int age)
	{
		this->name = name;
		this->age  = age;
	}
};

vector<Student> students;

int main()
{

	for (int i = 0; i < 10; i++)
	{
		Student student("name", i);
		students.push_back(student);
	}

	for (int j = 0; j < students.size(); j++)
	{
		cout << students[j].name << ":" << students[j].age << "\n";
	}

}
Posted by: Guest on April-20-2022

Browse Popular Code Answers by Language