#include <iostream>
using namespace std;
#include <fstream>
class files
{
int num1, num2, rollnumber, a, b;
string name;
public:
void setdata(int num1, int num2)
{
this->num1 = num1;
this->num2 = num2;
}
void setdata()
{
cout << "enter rollnumber:" << endl;
cin >> rollnumber;
cout << "enter name:" << endl;
cin >> name;
}
void displaydata()
{
cout << "rollnumber is:" << rollnumber << endl;
cout << "name is :" << name << endl;
}
void showdata()
{
cout << "entered numbers are: " << num1 << " "
<< "and " << num2 << endl;
}
void showdata(int num3) // function overrloading
{
cout << "sum of entered number and 9 is: " << num3 + 9 << endl;
}
files(){};
files(int a, int b)
{
this->a = a;
this->b = b;
}
files(int num4)
{
cout << "number passed in constructor is " << num4 << endl;
}
bool operator==(files c) // operator overloading
{
if ((a == c.a) && (b = c.b))
{
return true;
}
else
return false;
}
virtual void newdata() // for run time polymorphism
{
cout << "base class func called\n";
}
};
class newinh : virtual public files // virtual base class for no ambiguity problem which could have arrised in multiple inheritance
{
public:
void newdata()
{
cout << "inh class func called\n";
}
};
class newinh2 : virtual public files
{
int a;
int b;
public:
newinh2() {}
newinh2(int a, int b) : files(b) // using inheritence constructor
{
cout << "inh2 constructor called\n"
<< a << endl;
}
};
class multiple : public newinh, public newinh2
{
public:
void newdata1() { cout << "new function of multiple class called\n"; }
};
int main()
{
int a, b, c, d, e, choice, choice2, choice3;
string name;
files ob1, ob2, ob3;
ofstream f1("set.dat", ios::binary);
cout << "Enter 1 if you want to set data of two numbers and\n"
<< "Enter 2 if you want to write data of students to a binary file or \n"
<< "Enter 3 if you want to add 9 to the number\n ";
cin >> choice;
if (choice == 1)
{
cout << "enter two numbers:" << endl;
cin >> a >> b;
ob1.setdata(a, b);
cout << "enter 1 if you want to see the data you entered: " << endl;
cin >> choice2;
if (choice2 == 1)
{
ob1.showdata();
}
else
{
cout << "entered wrong choice " << endl;
}
}
else if (choice == 2)
{
cout << "enter number of students" << endl;
cin >> d;
for (int i = 0; i < d; i++)
{
ob2.setdata();
f1.write((char *)&ob2, sizeof(ob2));
}
cout << "position of file curser is " << f1.tellp() << endl;
cout << "enter position from begining you want the curser to be:" << endl;
int position;
cin >> position;
f1.seekp(position, ios::beg);
cout << "new position of curser is " << f1.tellp() << endl;
f1.seekp(0, ios::beg);
f1.close();
cout << "enter 1 if you want to see data from file: " << endl;
cin >> choice3;
if (choice3 == 1)
{
ifstream f2("set.dat", ios::binary);
while (f2.read((char *)&ob2, sizeof(ob2)))
{
ob2.displaydata();
};
}
else
{
cout << "wrong choice" << endl;
}
}
else if (choice == 3)
{
cout << "enter the number :" << endl;
cin >> e;
ob3.showdata(e);
}
else
{
cout << "wromg choice\n";
}
cout << "enter 1 if you want to copy data of one text file to another file: " << endl;
cin >> choice;
if (choice == 1)
{
ifstream sourcefile;
ofstream targettedfile;
char ch;
string file1, file2, new1;
cout << "Enter source file name with extension (like files.txt) : ";
cin >> file1;
sourcefile.open(file1);
if (!sourcefile)
{
cout << "Error in opening source file..!!";
exit(1); // wll terminate the program immediately after printing cout and wont execute code after this
}
cout << "Enter target file name with extension (like filet.txt) : ";
cin >> file2;
targettedfile.open(file2, ios::app);
if (!targettedfile)
{
cout << "Error in opening target file..!!";
sourcefile.close();
exit(2);
}
while (sourcefile)
{
sourcefile.get(ch);
targettedfile << ch;
}
cout << "File copied successfully..!!" << endl;
sourcefile.close();
targettedfile.close();
}
else
cout << "wrong choice\n";
cout << "enter 1 if you want to compare to objects are equal or not\n";
cin >> choice;
if (choice == 1)
{
int x, y, z, q;
cout << "enter two numbers of object 1\n";
cin >> x >> y;
cout << "enter two numbers of object 2\n";
cin >> z >> q;
files ob3(x, y), ob4(z, q);
if (ob3 == ob4)
{
cout << "hell yeah they are equal you fucking genius\n";
}
else
cout << "fuck nooo\n";
}
files *fptr; // file ptr to save refrence for using virtual function
newinh obj;
fptr = &obj; // taking refrence
fptr->newdata(); // using virtual function to point function
newinh2(2, 3); // using construcor in inherited class
multiple onj1;
onj1.newdata1();
return 0;
}
No comments:
Post a Comment