Top Banner
EXPT NO: 01 CONSTRUCTORS & DESTRUCTORS, COPY CONSTRUCTOR. Aim: To implement the concept of Constructor, destructor and copy constructor using C++ language Algorithm: Step 1: Start the program Step 2: Create class with two constructors. Step 3: Create the objects for the class with arguments. Step 4: Invoke the member function using objects. Step 5: Display the result. Step 6: Stop the program PROGRAM: // 1. Constructor, Destructor, copy constructor #include<iostream.h> #include<string.h> #include<conio.h> class string { char*name; int length; public: string() { length=0; name=new char[length+1]; } string(char*s) { length=strlen(s); strcpy(name,s); } void display(void)
52

pds LAB ii

Jul 21, 2016

Download

Documents

Ranjith Kumar

Programming and Data Structure
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: pds LAB ii

EXPT NO: 01 CONSTRUCTORS & DESTRUCTORS, COPY CONSTRUCTOR.

Aim:To implement the concept of Constructor, destructor and copy constructor using C++

language

Algorithm:

Step 1: Start the programStep 2: Create class with two constructors.Step 3: Create the objects for the class with arguments.Step 4: Invoke the member function using objects.Step 5: Display the result.Step 6: Stop the program

PROGRAM:

// 1. Constructor, Destructor, copy constructor#include<iostream.h>#include<string.h>#include<conio.h>class string { char*name; int length; public: string() { length=0; name=new char[length+1]; } string(char*s) { length=strlen(s); strcpy(name,s); } void display(void) { cout<<name<<"\n"; } void join(string & a,string & b); ~string(){} };void string::join(string&a,string&b)

Page 2: pds LAB ii

{ length=a.length+b.length; delete name; name=new char[length+1]; strcpy(name,a.name); strcat(name,b.name); }int main() { clrscr(); char*first="joseph"; string name1(first),name2("louis"),name3("lagrange"),s1,s2; s1.join(name1,name2); s2.join(s1,name3); cout<<"\n\t Dynamic memory Allocation, Constructor & Destructor\n"; name1.display(); name2.display(); name3.display(); s1.display(); s2.display(); getch(); return 0; }

OUTPUT

Dynamic memory Allocation, Constructor & Destructor joseph louis lagrange josephlouis josephlouislagrange Null pointer assignment

Result: Thus the C++ program for Constructor, destructor, and copy constructor was verified and executed successfully.

Page 3: pds LAB ii

EXPT NO: 02 FRIEND FUNCTION & FRIEND CLASS

Aim:To implement the concept of Friend Function & Friend Class using C++ language

Algorithm:

Step 1: Start the programStep 2: Create friend function and friend class.Step 3: Create the objects for the classStep 4: Invoke the Friend function using objects.Step 5: Display the result.Step 6: Stop the program

#include<iostream.h>

#include<string.h>

//class

class friends

{

//access specifies

private:

// initialization of member variables

int mVar1, mVar2;

//access specifier

public:

// member function

void test()

{

// assigning value to member variables

cout<<"Enter the first value :";

cin>>mVar1;

cout<<"Enter the second value :";

cin>>mVar2;

Page 4: pds LAB ii

}

// Declare the two functions friends.

friend int addition(friends input);

friend int subtraction(friends input);

};

// Function one

int addition(friends input)

{

// addition operation performed

return int(input.mVar1 + input.mVar2);

}

// Function two

int subtraction(friends input)

{

// subtraction operation performed

return int(input.mVar2- input.mVar1);

}

// main function

int main( )

{

// object created

friends ofriend;

//accessing member function using the object

ofriend.test();

//print the output of addition

cout << "The addition value is:"<<addition(ofriend) << endl;

// print the output of subtraction

cout << "The subtraction value is:"<<subtraction(ofriend) << endl;

}

Output:

Page 5: pds LAB ii

b) Program for friend class#include <iostream>using namespace std;class Square;class Rectangle { int width, height;

public: int area () { return ( width * height ); }

void convert ( Square a ); };

class Square {

friend class Rectangle; private: int side; public: Square ( int a ) : side ( a ) {} };

void Rectangle::convert ( Square a ){ width = a.side; height = a.side;}

Page 6: pds LAB ii

int main (){ Rectangle rect; Square sqr ( 4 ); rect.convert ( sqr ); cout << rect.area(); return 0;}

Output

Result

Thus the C++ program for Friend function and friend class was verified and executed successfully.

Page 7: pds LAB ii

EX. NO: 03 INHERITANCE

Aim:To implement the concept of inheritance using C+ + language.

Algorithm:

Step 1: Start the program.Step 2: Create a class student with two function get_number( )and put_number( ).Step 3: Define the member functions.Step 4: Create a class test inherited from class student which contain two member function get_mark( )and put_mark( ).Step 5: Create a class result inherited from class test which contain a member function display( ).Step 6: Create a object with result class and invoke the function.Step 7: Display the results.Step 8: Stop the program.

PROGRAM:

#include<iostream.h>#include<conio.h>class student { protected: int roll_number; public: void get_number(int a) { roll_number=a; } void put_number(void) { cout<<"roll_number:"<<roll_number<<"\n"; } };class test:public student { protected: float part1,part2; public: void get_marks(float x,float y) { part1=x;part2=y; }

Page 8: pds LAB ii

void put_marks(void) { cout<<"marks obtained:"<<"\n"; cout<<"part1="<<part1<<"\n"; cout<<"part2="<<part2<<"\n"; } };class sports { protected: float score; public: void get_score(float s) { score=s; } void put_score(void) { cout<<"sports wt:"<<score<<"\n\n"; } };class result:public test,public sports { float total; public: void display(void); };void result::display(void) { total=part1+part2+score; put_number(); put_marks(); put_score(); cout<<"total score:"<<total<<"\n"; }int main() { clrscr(); result student_1; cout<<"\n\t Multiple and Multilevel Inheritance \n"; student_1.get_number(1234); student_1.get_marks(27.5,33.0); student_1.get_score(6.0); student_1.display(); getch(); return 0;

Page 9: pds LAB ii

}

OUTPUT

Multiple and Multilevel Inheritance roll_number:1234 marks obtained: part1=27.5 part2=33 sports wt:6 total score:66.5

Result: Thus the C++ program for multiple and multilevel inheritance was verified and executed successfully.

Page 10: pds LAB ii

EX. NO.: 04 POLYMORPHISM & FUNCTION OVERLOADING.

Aim:To implement the concept of polymorphism, function overloading using C++

programming.

Algorithm:

Step 1: Start the programStep 2: Declare three methods with different parameters with same name.Step 3: Define three methods separately.Step 4: Call the methods by giving values at run time.Step 5: Return the result.Step 6: Stop the program

PROGRAM:

// 1. Function Overloading#include<iostream.h>#include<conio.h>int volume(int);double volume(double,int);long volume(long,int,int);int main() { clrscr(); cout<<"\n\t Function Overloading"; cout<<"\n Volume of a square ="<<volume(10); cout<<"\n Volume of a circle ="<<volume(2.5,8); cout<<"\n Volume of a rectangle ="<<volume(100l,75,15); getch(); return 0; }int volume(int s) { return (s*s*s); }double volume(double r,int h) { return(3.14519*r*r*h);

Page 11: pds LAB ii

}long volume(long l,int b,int h) { return (l*b*h); }

OUTPUT

Function OverloadingVolume of a square =1000Volume of a circle =157.2595Volume of a rectangle =112500

Result: Thus the C++ program for function overloading was verified and executed successfully.

Page 12: pds LAB ii

EXPT NO: 05 VIRTUAL FUNCTIONS

Aim:To implement the concept Virtual function using C++ programming.

Algorithm:

Step 1: Start the programStep 2: Declare two methods.Step 3: Define one method as VirtualStep 4: Call the methods by giving values at run time.Step 5: Return the result.Step 6: Stop the program

PROGRAM:

#include <iostream>using namespace std;// abstract base classclass Shape { protected: // attribute section int mWidth; int mHeight; int mResult; public: // behavior section void setVar(int Width,int Height) { mWidth = Width; mHeight= Height; } virtual void area() { // virtual function cout<< "shape drawn"; }

int getresult() { return mResult; } };

Page 13: pds LAB ii

// add class inherits from base classclass rectangle: public Shape {

public: void area() { mResult = mWidth*mHeight; } };

//sub class inherit base classclass triangle: public Shape {

public: void area() { mResult= ( mWidth*mHeight)/2; } };

int main(){ int Length,Breath; //pointer variable declaration of type base class Shape* oShape; //create object1 for addition process rectangle oRectangle; //create object2 for subtraction process triangle oTriangle; cout << "\nEnter the width and height: ";

while (cin >> Length >> Breath) { oShape = &oRectangle; oShape->setVar( Length , Breath ); //area of rectangle process, even though call is on pointer to base(shape)! oShape->area(); cout << "\nArea of rectangle = " << oShape->getresult(); oShape = &oTriangle; oShape->setVar( Length , Breath ); //triangle process, even though call is on pointer to base! oShape->area(); cout << "\nArea of triangle = " << oShape->getresult() << endl;

Page 14: pds LAB ii

} return 0;}

Output

Result: Thus the C++ program for operator overloading was verified and executed successfully.

Page 15: pds LAB ii

EXPT NO: 06 OVERLOADING UNARY AND BINARY OPERATOR USING MEMBER FUNCTION AND NON-MEMBER FUNCTION

Aim:

To implement Operator Overloading using member function and non member function in C++

Algorithm:

Step 1: Start the program.Step 2: Create a class with operator function, other function and variable.Step 3: Define the member functions.Step 4: Create the objects for the class.Step 5: Invoke the member function using object.Step 6: Display the output.Step 7: Stop the program.

PROGRAM:

#include<iostream>using namespace std;class ComplexOperations { // Access specifies private: // Declaration of member variables float mRealnumber; float mImaginarynumber; public: ComplexOperations() { mRealnumber = mImaginarynumber = 0.0; } ComplexOperations ( int real, int imaginary ) { mRealnumber = real; mImaginarynumber = imaginary; } ComplexOperations ( double real, double imaginary ) { mRealnumber = real; mImaginarynumber = imaginary;

Page 16: pds LAB ii

} friend istream & operator >> ( istream& , ComplexOperations& ); friend ostream & operator << ( ostream& , ComplexOperations& ); ComplexOperations operator+ ( ComplexOperations ); ComplexOperations operator- ( ComplexOperations ); ComplexOperations operator* ( ComplexOperations ); ComplexOperations operator/ ( ComplexOperations ); };istream& operator >> ( istream& input , ComplexOperations& oComplex ) { cout << "Real Part:"; input >> oComplex.mRealnumber; cout << "Imaginary Part:"; input >> oComplex.mImaginarynumber; return input;}

ostream& operator << ( ostream& output , ComplexOperations& oComplex )// Defining non-member function{ if ( oComplex.mImaginarynumber < 0 ) output << oComplex.mRealnumber << oComplex.mImaginarynumber << "i"; else output << oComplex.mRealnumber << "+" << oComplex.mImaginarynumber << "i"; return output;}// Defining member functionComplexOperations ComplexOperations::operator+ ( ComplexOperations oComplex){ ComplexOperations oAddition; oAddition.mRealnumber = mRealnumber + oComplex.mRealnumber; oAddition.mImaginarynumber = mImaginarynumber + oComplex.mImaginarynumber; return oAddition;}ComplexOperations ComplexOperations::operator-(ComplexOperations oComplex { ComplexOperations oSubtraction; oSubtraction.mRealnumber = mRealnumber - oComplex.mRealnumber; oSubtraction.mImaginarynumber =mImaginarynumber- oComplex.mImaginarynumber; return oSubtraction;}ComplexOperations ComplexOperations::operator*(ComplexOperations oComplex{ ComplexOperations oMulti; oMulti.mRealnumber = mRealnumber * oComplex.mRealnumber - mImaginarynumber * oComplex.mImaginarynumber;

Page 17: pds LAB ii

oMulti.mImaginarynumber = mRealnumber * oComplex.mImaginarynumber + mImaginarynumber * oComplex.mRealnumber; return oMulti;}ComplexOperations ComplexOperations::operator/ ( ComplexOperations oComplex ){ ComplexOperations oDivision; float result_of_complexoperations; result_of_complexoperations = oComplex.mRealnumber * oComplex.mRealnumber + oComplex.mImaginarynumber * oComplex.mImaginarynumber;oDivision.mRealnumber = ( mRealnumber * oComplex.mRealnumber + mImaginarynumber * oComplex.mImaginarynumber )/result_of_complexoperations; oDivision.mImaginarynumber = ( mImaginarynumber * oComplex.mRealnumber - mRealnumber * oComplex.mImaginarynumber )/result_of_complexoperations; return oDivision;}int main(){ ComplexOperations oNumberOne, oNumberTwo, oNumberThree; cout << "complex number 1: "; cin >> oNumberOne; cout << "complex number 2: "; cin >> oNumberTwo; cout << "complex numbers are:"; cout << "1st:" << oNumberOne; cout << "\n2nd:" << oNumberTwo; oNumberThree = oNumberOne + oNumberTwo; cout << "\nAddition is:" << oNumberThree; oNumberThree = oNumberOne - oNumberTwo cout << "\nSubtraction is:" << oNumberThree; oNumberThree = oNumberOne * oNumberTwo; cout << "\n Multiplication is:" << oNumberThree; oNumberThree = oNumberOne / oNumberTwo cout << "\n Division is:" << oNumberThree; return 0;}Output

Page 18: pds LAB ii

Result: Thus the C++ program for operator overloading was verified and executed successfully.

Page 19: pds LAB ii

EXPT NO: 07 CLASS AND FUNCTION TEMPLATES

a) Class Template

Aim:To write a C++ program for creating class template.

Algorithm:

Step 1: Start the program Step 2: Create a template vector using template <class T>.Step 3: Define the template.Step 4: Create the object using template vector.Step 5: Do the operations.Step 6: Display the result.Step 7: Stop the program

PROGRAM:

// Class Template#include<iostream.h>#include<conio.h>const size=3;template<class T>class vector { T*V; public: vector() { V=new T[size]; for(int i=0;i<size;i++) V[i]=0; } vector(T*a) { for(int i=0;i<size;i++) V[i]=a[i]; } T operator*(vector&y) { T sum=0; for(int i=0;i<size;i++)

Page 20: pds LAB ii

sum+=this->V[i]*y.V[i]; return sum; } };int main() { clrscr(); float X[3]={1.1,2.2,3.3}; float Y[3]={4.4,5.5,6.6}; vector<float>V1; vector<float>V2; V1=X; V2=Y; float R = V1*V2; cout<<"\n\n\n\t Class template \n"; cout<<"R="<<R<<"\n"; getch(); return 0; }

OUTPUT

Class templateR=38.720001

Result: Thus the C++ program for creating class template was verified and executed successfully.

b) Function Template

Aim:To write a C++ program for creating function template.

Algorithm:

Step 1: Start the program Step 2: Define the function template.

Page 21: pds LAB ii

Step 3: Do the operations.Step 4: Display the result.Step 5: Stop the program

/ Function Template

Displaying greatest value using function template

#include <iostream>

#include <string>

using namespace std;

// Function template declaration

template <typename T>

inline T const& Max (T const& number_one, T const& number_two)

{

// Returns greatest number

return number_one < number_two ? number_two : number_one;

}

int main ()

{

// Integer variables declaration

int int_number_one, int_number_two;

// Double variables declaration

double double_number_one, double_number_two;

// String variables declaration

string string_word_one, string_word_two;

cout << "Enter integer values";

cin >> int_number_one >> int_number_two;

// Function call to template

cout << "Integer Result:" << Max(int_number_one, int_number_two);

cout << "Enter double values";

cin >> double_number_one >> double_number_two;

cout << "Double Result:" << Max(double_number_one, double_number_two);

Page 22: pds LAB ii

cout << "Enter string values";

cin >> string_word_one >> string_word_two;

cout << "String Result:" << Max(string_word_one, string_word_two);

return 0;

}

Output:

Page 23: pds LAB ii

EXPT NO: 08 EXCEPTION HANDLING

Aim:To write a C++ program for Exception handling with multiple catch statements.

Algorithm:

Step 1: Start the programStep 2: Cerate a function test with conditions in the try block.Step 3: Catch the values which is troughed from the try block.Step 4: Display the messages from the block which is catched.Step 5: Stop the program.

PROGRAM:

#include<iostream.h>#include<conio.h>void main(){ int a,b,c; float d; clrscr(); cout<<"Enter the value of a:"; cin>>a; cout<<"Enter the value of b:"; cin>>b; cout<<"Enter the value of c:"; cin>>c; try { if((a-b)!=0) { d=c/(a-b); cout<<"Result is:"<<d; } else { throw(a-b); } }

Page 24: pds LAB ii

catch(int i) { cout<<"Answer is infinite because a-b is:"<<i; } getch();}

OUTPUT:

Enter the value of a: 5Enter the value of b: 4Enter the value of a: 2Result is: 2

Enter the value of a: 5Enter the value of b: 5Enter the value of a: 2Answer is infinite because a-b is: 0

Result: Thus the C++ program for exception handling with multiple catch statements was

verified and executed successfully.

Page 25: pds LAB ii

EXPT NO: 09 STL CONCEPTS

Aim:To implement concept STL using C++ program.

Algorithm:

Step 1: Start the program.Step 2: Include the standard template library of list in the header file.Step 3: Using the list STL display the list values, add the element in the list, remove the element from the list, merge two lists, sort and reverse the listStep 4: Display values after each operation.Step 5: Stop the program.

PROGRAM:

#include <iostream.h>#include <vector.h>#include <string.h>

void main(){ vector<string> SS;

SS.push_back("The number is 10"); SS.push_back("The number is 20"); SS.push_back("The number is 30"); cout << "Loop by index:" << endl; int ii; for(ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; }

cout << endl << "Constant Iterator:" << endl;

vector<string>::const_iterator cii; for(cii=SS.begin(); cii!=SS.end(); cii++) { cout << *cii << endl; }

Page 26: pds LAB ii

cout << endl << "Reverse Iterator:" << endl;

vector<string>::reverse_iterator rii; for(rii=SS.rbegin(); rii!=SS.rend(); ++rii) { cout << *rii << endl; } cout << endl << "Sample Output:" << endl; cout << SS.size() << endl; cout << SS[2] << endl;

swap(SS[0], SS[2]); cout << SS[2] << endl;}

OUTPUT:

Loop by index:The number is 10The number is 20The number is 30

Constant Iterator:The number is 10The number is 20The number is 30

Reverse Iterator:The number is 30The number is 20The number is 10

Sample Output:3The number is 30The number is 10

Result: Thus the C++ program for using STL was verified and executed successfully.

Page 27: pds LAB ii

EXPT NO: 10 FILESTREAM CONCEPTS

Aim:To concept of file stream concept using C++

Algorithm:

Step 1: Start the program.Step 2: Include the fstream in the header file.Step 3: perform all function in file stream Step 4: Display values after each operation.Step 5: Stop the program.

PROGRAM:

a) Program to write a text in the file#include<iostream>

#include<fstream>

using namespace std;

int main()

{

// Create object to display output

ofstream oFileoutput;

// Open the text file in write mode

oFileoutput.open ( "out.txt" );

//String declaration

char char_string[300] = "Time is a great teacher, but unfortunately it kills all its students.

Berlioz";

//Display the string

oFileoutput << char_string;

//Close the text file in write mode

oFileoutput.close();

return 0;

Page 28: pds LAB ii

}

Output:

b) Program to read data from text file and display it#include<iostream>

#include<fstream>

#include<conio.h>

using namespace std;

Page 29: pds LAB ii

int main()

{

// Create object to get input values

ifstream oFileinput;

// Open the text file in read mode

oFileinput.open ( "out.txt" );

// Character declaration

char char_text;

/* Read the character until the file reach end of file*/

while ( !oFileinput.eof() )

{

oFileinput.get ( char_text );

//Display the text

cout << char_text;

}

//Close the text file opened in read mode

oFileinput.close();

return 0;

}

Output:

Page 30: pds LAB ii

c) Program to count number of characters from “out.txt”.#include<iostream>

#include<fstream>

#include<conio.h>

using namespace std;

int main()

{

// Create object to get input values

ifstream oFileinput;

// Open the text file in read mode

oFileinput.open ( "out.txt" );

// Character declaration

char char_text;

//Integer variable declaration

int int_count = 0;

/* Read the character until the file reach end of file*/

while ( !oFileinput.eof() )

Page 31: pds LAB ii

{

// Read characters from the text file

oFileinput.get ( char_text );

// Increment the count values

int_count++;

}

// Display number of characters in text file

cout << "Number of characters in file is " << int_count;

//Close the text file opened in read mode

oFileinput.close();

return 0;

}

Output:

d) Program to count number of words from “out.txt”#include<iostream>

#include<fstream>

#include<conio.h>

using namespace std;

int main()

{

// Create object to read input values

Page 32: pds LAB ii

ifstream oFileinput;

// Open the text file in read mode

oFileinput.open ( "out.txt" );

// Character declaration

char char_word[30];

//Integer variable declaration

int int_count = 0;

/* Read the word until the file reach end of file*/

while ( !oFileinput.eof() )

{

//Read the word from the text file

oFileinput >> char_word;

// Increment the count values

int_count++;

}

// Display number of words in text file

cout << "Number of words in file is " << int_count;

//Close the text file opened in read mode

oFileinput.close();

return 0;

}

Output:

Page 33: pds LAB ii

e) Program to count number of lines in the file#include<iostream>

#include<fstream>

#include<conio.h>

using namespace std;

int main()

{

    // Create object to read input values

    ifstream oFileinput;

    // Open the text file in read mode

    oFileinput.open ( "out.txt" );

    // Character declaration

    char char_word[30];

    // Integer variable declaration

    int int_count = 0;

    /* Read the word until the file reach end of file*/

    while ( !oFileinput.eof() )

    {

Page 34: pds LAB ii

           // Read the word from the text file

           oFileinput >> char_word;

          // Increment the count values

           int_count++;

    }

    // Display number of words in text file

    cout << "Number of words in file is " << int_count;

    // Close the text file opened in read mode

    oFileinput.close();

    return 0;

}

Output

Page 35: pds LAB ii

f) Program to copy contents of one file to another file.#include<iostream>#include<fstream>#include<conio.h>using namespace std;int main(){

// Create object to read input values

       ifstream oFileinput;

       // Open the text file in read mode

       oFileinput.open ( "out.txt" );

       // Create object to display output values

       ofstream oFileoutput;

       // Open the text file in write mode

       oFileoutput.open ( "sample.txt" );

       // Character declaration

       char char_text;

       /* Read the character until the file reach end of file*/

       while ( !oFileinput.eof() )

       {

         // Read characters from the "out.txt"

           oFileinput.get ( char_text );

           // Write characters to "sample.txt"

           oFileoutput << char_text;

   }

   //Close the text file opened in read mode

   oFileinput.close();

   return 0;

}

Output:

Page 36: pds LAB ii
Page 37: pds LAB ii

g) Binary file(Write mode)#include<iostream>

#include<fstream>

using namespace std;

struct StudentDetails

{

int RollNo;

char Name[30];

char Address[40];

};

void ReadStudentDetails ( StudentDetails& TempStud )

{

cout << "\n Enter Roll No:";

cin >> TempStud.RollNo;

cout << "\n Enter Name:";

cin >> TempStud.Name;

cout << "\n Enter Address:";

Page 38: pds LAB ii

cin >> TempStud.Address;

cout << "\n";

}

int main()

{

struct StudentDetails BE_Student_Output;

ofstream BE_StudFile_Output;

BE_StudFile_Output.open ( "BE.dat", ios::out | ios::binary | ios::trunc );

if ( !BE_StudFile_Output.is_open() )

{

cout << "File cannot be opened \n";

}

char Continue = 'Y';

do

{

ReadStudentDetails ( BE_Student_Output );

BE_StudFile_Output.write ( ( char* ) &BE_Student_Output, sizeof ( struct StudentDetails )

);

if ( BE_StudFile_Output.fail() )

{

cout << "file write failed";

}

else

{

cout << "Do you want to continue Y/N:";

cin >> Continue;

}

}

while ( Continue != 'N' );

BE_StudFile_Output.close();

Page 39: pds LAB ii

return 0;

}

Output

h) Binary file(Read mode)#include<iostream>

#include<fstream>

using namespace std;

// Declaring the structure variables

struct StudentDetails

{

int RollNo;

char Name[30];

char Address[40];

};

// function declaration

Page 40: pds LAB ii

void WriteStudentDetails ( StudentDetails TempStud )

{

cout << "\n The Roll No:";

cout << TempStud.RollNo;

cout << "\n The Name:";

cout << TempStud.Name;

cout << "\n The Address:";

cout << TempStud.Address;

cout << "\n";

}

// Main function

int main()

{

struct StudentDetails BE_Student_Input;

ifstream BE_StudFile_Input ( "BE.dat", ios::out | ios::binary );

while ( !BE_StudFile_Input.eof() )

{

BE_StudFile_Input.read ( ( char* ) &BE_Student_Input, sizeof ( struct StudentDetails ) );

if ( BE_StudFile_Input.fail() )

{

break;

}

WriteStudentDetails ( BE_Student_Input );

}

BE_StudFile_Input.close();

return 0;

}

Output:

Page 41: pds LAB ii

i) Reading from and writing the personal details into the file using getline function#include <fstream>

#include <iostream>

using namespace std;

int main ()

{

char Personal_data[100];

// open a file in write mode.

Page 42: pds LAB ii

ofstream oFileoutput;

oFileoutput.open ( "personalfile.dat" );

cout << "Writing to the file" << endl;

cout << "Enter your name: ";

cin.getline ( Personal_data, 100 );

// write input data into the file.

oFileoutput << Personal_data << endl;

cout << "Enter your age: ";

cin >> Personal_data;

cin.ignore();

// again write input data into the file.

oFileoutput << Personal_data << endl;

// close the opened file.

oFileoutput.close();

// open a file in read mode.

ifstream oFileinput;

oFileinput.open ( "personalfile.dat" );

cout << "Reading from the file" << endl;

oFileinput >> Personal_data;

// write the data at the screen.

cout << Personal_data << endl;

// again read the data from the file and display it.

oFileinput >> Personal_data;

cout << Personal_data << endl;

// close the opened file.

oFileinput.close();

return 0;

}

Output

Page 43: pds LAB ii

j) Reading from and writing into the file#include<fstream.>

#include<stdio.h>

#include<ctype.h>

#include<string>

#include<iostream>

using namespace std;

int main()

{

Page 44: pds LAB ii

char txt_file_data,file_ASCII_result;

char char_filename[10];

ofstream oFileoutput;

cout<<"Enter File Name:";

cin>>char_filename;

oFileoutput.open(char_filename);

//write contents to file

cout<<"Enter the text(Enter # at end)\n";

while ((txt_file_data=getchar())!='#')

{

file_ASCII_result=txt_file_data-32;

oFileoutput<<file_ASCII_result;

}

oFileoutput.close();

//read the contents of file

ifstream oFileintput(char_filename);

cout<<"\n\n\t\tThe File contains\n\n";

while (oFileintput.eof()==0)

{

oFileintput.get(txt_file_data);

cout<<txt_file_data;

}}

Output

Page 45: pds LAB ii