-1

I want to make a class of a student and take 3 inputs information and make an output of this file. How to this? This is my try:

#include <iostream>
using namespace std;
class Student{
  private:
    char name[50];
    char id[50];
    int age;
  public:
    void getdata()
    {
        //take name as input
        //take id as input
        //take age as input
    }
    void showdata()
    {
         //display stored file
    }
 }

int main()
{
    Student s1;
    ofstream s1("student.txt");      //i want to store that 's1' object
    //anything else
    return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Jamil
  • 330
  • 1
  • 6
  • 25
  • 1
    U have used S1 name for two different object types . – Charlie Jun 08 '13 at 15:34
  • bcz i have a logic that i am making output of of s1 object.isn't it right? – Jamil Jun 08 '13 at 15:37
  • 1
    possible duplicate of [How to write an object to file in C++](http://stackoverflow.com/questions/2376193/how-to-write-an-object-to-file-in-c) – Mat Jun 08 '13 at 15:44
  • No @Jamel thats not right, in most of the other programming languages and c++, it doesn't work like that. You should brush up c++ concepts by going thru some tutorials http://www.cplusplus.com/doc/tutorial/ or other tutorial site you like. – cpz Jun 08 '13 at 15:45
  • I dont think so you can write s1 object of Student in text file using s1 object of ofstream ,just because you have the same name – Charlie Jun 08 '13 at 15:46
  • You should try to learn some C++ before trying to do this kind of thing. There is a [list of good books here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Jun 08 '13 at 15:47
  • then how to make output of object using fstream.Either in txt file or some other! – Jamil Jun 08 '13 at 15:48
  • Does this answer your question? [How to write an object to file in C++](https://stackoverflow.com/questions/2376193/how-to-write-an-object-to-file-in-c) – Audrius Meškauskas Nov 17 '21 at 14:14

2 Answers2

4

C++ uses the stream paradigm to implement standard input/output.

Stream paradigm means that if you application wants to access/use a resource (A file, the console, etc) a stream acts as a mediator between your application and the resource:

                     ofstream       +----+
              +-------------------->|File|
              |                     +----+
              |
       +------+------+
       | Application |
       +------+------+
              |
              |                     +-------+
              +-------------------->|Console|
                        cout        +-------+

It means every write/read operations you perform are really stream operations. The point of this is that stream operations are basically the same, independently of what type of resource (And what type of stream) are you using.

This allows us to implement a "generic" (Generic meaning valid for any type of stream/resource). How? Overloading C++ operators >> and <<.

For input operations (Input means receiving data from the stream and put it in our variable/object), we need to overload the >> operator as follows:

istream& operator>>(istream& is , MyClass& object)
{
    is >> object.myClassAtributte; (1)
    ... //Same for every attribute of your class.

    return is;
}

First, note that the input stream is passed by reference. By reference because streams are non-copyable (What exactly means to copy a stream? Copy the link between your app and the resource? That sounds ridiculous), and non-const beacuse you are going to modify the stream (You are going to write through it).

Finally, note that the function not returns void, returns a reference to the same stream that was passed to the function. That allows you to write concatenated-write/read sentences like cout << "Hello. " << "Im" << " Manu343726" << endl;

For output operations (Output means sending data to the stream), we need to overload the << operator, wich implementation is exactly the same:

ostream& operator<<(ostream& os , const MyClass& object)
{
    os << object.myClassAtributte; (1)
    ... //Same for every attribute of your class.

    return os;
}

Note that in this case, your object is passed const, beacuse we won't modify it (We will only read its attributes).

(1) Is preferable to implement this functions making them friend of your class, to allow us access to private/protected members.

Manu343726
  • 13,969
  • 4
  • 40
  • 75
0

You have to overload << and >> operators for ostream and istream

std::ostream& operator<< (std::ostream& stream, const Student& student)
std::istream& operator<< (std::istream& stream, Student& student)

make them friend and implement

Neel Basu
  • 12,638
  • 12
  • 82
  • 146