-1

In the class base, we need to write a function such that printing an object will print arguments of an object using oops concept.

#include <iostream>
using namespace std;
class base{
    int num;
    string s;
    public:
    base(int elem,string p){
        cout<<elem<<" "<<p<<endl;
    }
    // todo:

};
int main() {
    base obj(12,"shivam");
   // cout<<obj<<endl;
}
Shivam Jain
  • 53
  • 1
  • 5
  • Take a look at [this](https://stackoverflow.com/questions/23239646/print-function-for-class-c) question, which addresses the printing. For the arguments passed to `obj`, take a look at constructors. – cigien Jun 12 '20 at 02:43
  • If you search for "c++ overload output", you will find plenty of tutorials. – Beta Jun 12 '20 at 02:53

2 Answers2

0

Your current idea is not far from working, except you print the constructor arguments to std::cout as soon as an instance of base is created - not when the programmer using the class expresses such a wish. What you need to do is to save the arguments given when constructing a base. You'll then be able to print them on demand.

Example:

#include <iostream>
#include <string>

class base {
public:
    base(int n, const std::string& str) : //    save the arguments given using
        num(n), s(str)                    // <- the member initializer list
    {
        // empty constructor body
    }
    // declare a friend function with special privileges to read private
    // member variables and call private functions:
    friend std::ostream& operator<<(std::ostream&, const base&);

private:
    int num;
    std::string s;
};

// define the free (friend) function with access to private base members:
std::ostream& operator<<(std::ostream& os, const base& b) {
    // here you format the output as you'd like:
    return os << '{' << b.num << ',' << b.s << '}';
}

int main() {
    base obj(12,"shivam");
    std::cout << obj << '\n';
}

Output:

{12,shivam}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • can you explain why you use return by reference? – Shivam Jain Jun 18 '20 at 06:24
  • @ShivamJain References are as lightweight as it comes in C++. A reference doesn't even need to occupy real memory. In the `ostream` case above, it's important for another reason. Example `the_ostream << a << b` This is chaining two operations, like this: `(the_ostream << a) << b` which would only be possible if `the_ostream << a` returned a reference to the same `ostream` that `b` is supposed to be streamed into. Perhaps not the best explanation, but ask again if I didn't make it clearer. – Ted Lyngmo Jun 18 '20 at 08:31
0

Try this:

  int IntS(int y)
  {
       return y;
  }

  class base{
  public:
        base(int enume, std::string str)
        {
             std::cout << IntS(enume) << '\n';
             std::cout << str << '\n;
        }

    };

    int main()
    {
         base mybase(IntS(5), "five");
         return 0;
    }

See also, std::map:

How can I print out C++ map values?

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
GobeRadJem32
  • 102
  • 3
  • 14