Problem with functions in classes

Hi, this is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
class student{
public: 

private: 
string name;
vector<char> favoriteletters;
int main()
{
some code}
infile >> ?

I know how to write the getters and setters for the name but I dont know how to do it for the vector. In addition to that, how would i call these functions in my main function? Thank you
As for favoriteletters, that depends on what you want to do.
I'd suggest add_favorite_letter(char c), get_favorite_letter (int n) and get_size(), which returns the number of favorite letters in the vector.
1
2
3
4
5
6
7
void add_favorite_letter (char c) 
{  favoriteletters.push_back (c);
}

char get_favorite_letter (int n)
{  return favoriteletters[n];  //  Be sure n < size of vector
}


Calling from main:
1
2
3
4
5
6
7
8
int main ()
{  student  s1;  // instance of student

    s1.set_name ("Fred");
    s1.add_favorite_letter ('A');
    cout << s1.get_name() << endl;
    cout << s1.get_favorite_letter (0) << endl;
}



Last edited on
Thank you so much. You are a life saver!
Topic archived. No new replies allowed.