-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass_write_basic.cpp
46 lines (46 loc) · 1.01 KB
/
class_write_basic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<fstream>
using namespace std;
class nishkarsh
{
public:
int roll; //if these are private data members how will they be accessed in main so that they can be stored inside the file??
string name;
void insert(int r,string n)
{
roll=r;
name=n;
}
void show()
{
cout<<"Name is "<<name<<endl<<"Roll number is"<<roll<<endl;
}
};
int main()
{
cout<<"Lets try file handling with C++ classes"<<endl;
nishkarsh n1;
cout<<"Lets write to the file"<<endl;
cout<<"Insert the data as roll number and name"<<endl;
string n;
int r;
cin>>r>>n;
n1.insert(r,n);
n1.show();
cout<<"Class has been created"<<endl;
///////////////////////////
cout<<"Lets create a text file with the data members filling it"<<endl;
ofstream nish1("Class.txt");
nish1<<n1.name<<" "<<n1.roll<<endl;
nish1.close();
////////////////////////////
cout<<"Lets create another object with insertion from file "<<endl;
string n2; int r2;
ifstream nish("Class.txt");
nish>>n2>>r2;
nishkarsh o;
o.insert(r2,n2);
o.show();
///////////////////////////
return 0;
}