3.3 Oops
3.3 Oops
3.3 Oops
FLOWCHART/ ALGORITHM
ALGORITHM-
STEP 1: START
STEP 2: Declare Header file and namespace
STEP 3:In main method declare ch, sourcefile[20],targetfile[20] STEP 4: Declare
File*fs,*ft
STEP5 :Input the name of source file and pass through open(…)method and
store in fs
STEP 6: if fs =NULL print error ooccurred
STEP 7: Input the name of target file and pass through open(…)method and
store in ft STEP 8:ch= fget(fs)
STEP 9:if(ch!=EOF)
STEP10:fput(ch,ft)
STEP 11: ch=fget(fs) goto
STEP 12: print “File copied
Successfully
STEP 13: fclose(fs)
STEP 14: fclose(ft)
STEP 15: STOP
PROGRAM CODE
1)
#include <iostream>
#include <fstream>
using namespace std;
int main()
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103
{
int i; char name[10]; float sal;
cout<<"Name = Navneet UID = 21BCS6692<<endl;
ofstream outfile ("Employee.txt");
for (i=0;i<3;i++)
{
cout<<"Enter the name and salary of employee"<<endl;
cin>>name>>sal;
outfile<<name<<" "<< sal;
}
outfile.close();
ifstream inpfile("Employee.txt");
for (i=0; i<3; i++)
{
inpfile>>name;
Create two files inside the directory where you're saving your program or going to save
your C++ program of copy file (given below)
below).
OUTPUT
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
ofstream fout;
string line;
fout.open("Sample.txt");
while(fout)
{
getline(cin,line);
if(line=="-1")
break;
fout<<line<<endl;
}
fout.close();
fstream fin;
fin.open("Sample.txt");
while(fin)
{
getline(fin,line);
cout<<line<<endl;
}
fin.close();
}
OUTPUT
3)
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
ofstream outfile;
outfile.open("Questions.txt");
outfile<<"What
What is your name"<<endl;
outfile<<"What is your Address"<<endl;
outfile.close();
outfile.open("Answers.txt");
outfile<<"\n Anil"<<endl;
outfile<<"\n Chandigarh"<<endl;
outfile.close();
char Quest[82],ans[50];
ifstream inpfile;
inpfile.open("Questions.txt"
inpfile.open("Questions.txt");
while(inpfile.eof()==0)
{
inpfile.getline(Quest,82);
cout<<Quest;
}
inpfile.close();
inpfile.open("Answers.txt");
while(inpfile.eof()==0)
{
inpfile.getline(ans,50);
cout<<ans;
inpfile.close();
}
}
PROGRAM CODE
4)
#include <iostream>
#include <fstream>
using namespace std;
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103
int main()
{
char quest[80],ans[50];
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
ifstream inpfile1,inpfile2;
inpfile1.open("Questions.txt");
inpfile2.open("Answers.txt");
while (inpfile1.eof()==0|| inpfile2.eof()==0)
{
inpfile1.getline(quest,80);
cout<<"\n"<<quest;
inpfile2.getline(ans,20);
cout<<"\e"<<ans;
inpfile1.close();
inpfile2.close();
}
}
OUTPUT
PROGRAM CODE
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-21CSH103
5)
#include<iostream>
using namespace std;
int main()
{
cout<<"Name = Navneet UID = 21BCS6692"<<endl;
char ch, sourceFile[20], targetFile[20];
FILE *fs, *ft;
cout<<"Enter the Name of Source File: ";
cin>>sourceFile;
fs = fopen(sourceFile, "r");
if(fs == NULL)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"\nEnter the Name of Target File: ";
cin>>targetFile;
ft = fopen(targetFile, "w");
if(ft == NULL)
{
cout<<"\nError Occurred!";
return 0;
}
ch = fgetc(fs);
while(ch != EOF)
{
fputc(ch, ft);
ch = fgetc(fs);
}
cout<<"\nFile copied successfully.";
fclose(fs);
fclose(ft);
cout<<endl;
return 0;
}
PROGRAM CODE
6)
#include <iostream>
#include <fstream>
using namespace std;
//class student to read and write student details
class student
{
private:
char name[30];
}
void showData(void)
{
cout<<"Name:"<<name<<",Age:"<<age<<endl;
}
};
int main()
{
student s;
ofstream file;
Apply different techniques to decompose a problem and programmed a solution with its
sub modules.
Analyze and explain the behavior of simple programs involving the programming
addressed in the course.
Implement and evaluate the programs using the syntax and semantics of object-oriented
object
programming.
Design the solution of real--world problems in order to determine that the program
performs as expected.