Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to use class with stringstream in C++

1 Answer

0 votes
#include <iostream>
#include <sstream>
#include <string>

class Student {
    private:
        int age;
        std::string first_name;
        std::string last_name;
        std::string learn;
    public:
        void set_age(int _age) {
            age = _age;
        }
        void set_learn(std::string _learn) {
            learn = _learn;
        }
        void set_first_name(std::string _first_name) {
            first_name = _first_name;
        }
        void set_last_name(std::string _last_name) {
            last_name = _last_name;
        }
        std::string to_string() {
            std::stringstream os;
            os << first_name << " " << last_name << " " << age << " " << learn << "\n";
            
            return os.str();
        }
};

int main() {
    int age = 96;
    std::string first_name = "Albus", last_name = "Dumbledore";
    std::string learn = "C++";
    
    Student st;
    st.set_age(age);
    st.set_first_name(first_name);
    st.set_last_name(last_name);
    st.set_learn(learn);

    std::cout << st.to_string();
}




/*
run:

Albus Dumbledore 96 C++

*/

 



answered Jan 13, 2023 by avibootz

Related questions

1 answer 147 views
147 views asked Jan 24, 2021 by avibootz
2 answers 317 views
1 answer 85 views
1 answer 135 views
2 answers 82 views
...