Pointers

You have been given a Student class. A user will insert multiple names followed by the word "STOP"

With each name, create a Student pointer and insert it into the "students" vector. You must print all the names in the vector in reverse order.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #pragma once
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Student
{
private:
	string name;

public:
	Student(string name);
	string getName();
	void setName(string newName);


	~Student();
};

Student::Student(string studentName)
{
	name = studentName;
}

string Student::getName()
{
	return name;
}

void Student::setName(string newName)
{
	name = newName;
}

Student::~Student()
{
}

//
 

int main()
{
	vector<Student*> students;
	string line = "";
	string name = "";
	
	while(line != "STOP")
	{
	    cin >> name;
	    if (name == "STOP")
	    break;
	    Student * student = new Student(name);
	    students.push_back(student);
	}
	for (int i = students.size()-1; i >= 0; i--)
	{
	    cout << students[i]<< endl;
	}
	

	return 0;
}
Hi,

You didn't ask a question .....
Sorry i dont know whats wrong about my main. When i run it, it prints the location instead of the value
line 60 prints a pointer, you need to use that pointer to call one of the class functions. Hint ->
GOT IT!!! THANKS!! Could you help me with one more problem?

For this one im having trouble doing the setName

You have been given a Student class. A student pointer, s, has been created for you. The student's name is "Po". The user will enter a new name for Po; you must call the "setName" function to change Po's name to the value given to you by the user. If done correctly, the new name should be shown once.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <iostream>
#include <string>

using namespace std;

class Student
{
private:
	string name;

public:
	Student(string name);
	string getName();
	void setName(string newName);


	~Student();
};

Student::Student(string studentName)
{
	name = studentName;
}

string Student::getName()
{
	return name;
}

void Student::setName(string newName)
{
	name = newName;
}

Student::~Student()
{
}

int main()
{
	string name;
	cin >> name;

	Student* s = new Student("Po");
	
	//
    setName(name);
	//

	cout << s->getName();
	return 0;
}
Hi again,

Lines 47 & 48: no need to initialise to empty string this is already the default.

Line 55: no need for new there, STL containers store their data on the heap already.

1
2
Student student =  Student(name);
	    students.push_back(&student);


The code leaks, there is no corresponding delete.

size() returns a type of std::size_t , so to avoid a warning there, make i's type std::size_t also.

There was a warning about #pragma once in the main file, not needed there. But it would be if the class had it own files - header and cpp.
Last edited on
you must call the "setName" function to change Po's name to the value given to you by the user.


On line 48, how is this different to where you call functions elsewhere?
perfect i got it! i was just missing the -> again
Topic archived. No new replies allowed.