-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;
}
8
  • 1
    U have used S1 name for two different object types . Commented Jun 8, 2013 at 15:34
  • bcz i have a logic that i am making output of of s1 object.isn't it right? Commented Jun 8, 2013 at 15:37
  • 1
    possible duplicate of How to write an object to file in C++ Commented Jun 8, 2013 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 cplusplus.com/doc/tutorial or other tutorial site you like. Commented Jun 8, 2013 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 Commented Jun 8, 2013 at 15:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.