1

// The function which i was required to make was to.string() in the class,Which i had no idea how to make.This is an odd function(not comparing with the math one.)which returns value in two different types of data types i.e(string,integer).The only thing stuck me was assigning a variable after making (string to.string()) function//The return value of function is something like [age,first_name,last_name,standard](without the square brackets with the commmasin the output) p.s=need a simpler function without using vector header.

#include <iostream>
    #include <sstream>
    using namespace std;
    
    class Student{
        public :
    
    
       void set_age(int no){
          age_no=no;
       }
       void set_standard(int no){
           std_no=no;
       }
       void set_first_name(string identity){
           name_letter=identity;
       }
       void set_last_name(string identity2){
           last_name_letter = identity2;
       }
      
       int get_age(){
           return age_num;
       }
       int get_standard(){
           return std_no;
       }
       string get_first_name(){
           return name_letter;
       }
       string get_last_name(){
           return last_name_letter;
       }
        private :
            int age_no;
            int std_no;
            string name_letter;
            string last_name_letter;
    
    };
    
    int main() {
        int age, standard;
        string first_name, last_name;
        
        cin >> age >> first_name >> last_name >> standard;
        
        Student st;
        st.set_age(age);
        st.set_standard(standard);
        st.set_first_name(first_name);
        st.set_last_name(last_name);
        
        cout << st.get_age() << "\n";
        cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
        cout << st.get_standard() << "\n";
        cout << "\n";
        cout << st.to_string();
        
        return 0;
    }

1 Answers1

0

If you want to create a string with the format age, first_name, last_name, standard then you could do something like

class Student
{
public:
    ...
    std::string to_string() const;
    ...
};

std::string Student::to_string() const
{
    return std::to_string(get_age()) + ", " +
           get_first_name() + ", "
           get_last_name() + ", "
           std::to_string(get_standard());       
}

As an aside, I would suggest making all of your getter functions const for example

int get_age() const;

This denotes that the method will not mutate or modify any of the values of the class's member variables.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    Yes you must at least declare it within the class. It is canonical for your header files (.h) to contain the class and function **declarations** but the cpp files to contain the full class definitions (for sufficiently large classes). In this case you have all of your source code in a single cpp file so that's not quite relevant, but when you start writing larger programs this will become more important to organize your code. – Cory Kramer Oct 09 '20 at 11:38
  • error: extra qualification 'Student::' on member 'to_string' [-fpermissive] –  Oct 09 '20 at 11:42
  • 1
    If you define the function *within* your class it would just be `to_string`, if the definition is *outside* your class it needs the class scope `Student::to_string` – Cory Kramer Oct 09 '20 at 11:51
  • ok but i need to make changes in your code a little by adding other get functions in it . But no problem , Thanks for your help –  Oct 09 '20 at 11:54
  • Kramar can you explain why you made them const ??? by any chance ??? –  Oct 09 '20 at 11:57
  • 1
    See [this explanation](https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-function-declaration-of-a-class) – Cory Kramer Oct 09 '20 at 11:57