Download - Introduction to c ++ part -2
• What is the difference between c and c++?
• What is a class?• What is an object?• What are cin,cout ?
Recall
Introduction to C++ File handling ,Operator Over loading
Week 4 day 2
File Handling in C++
File Handling
• C++ provides the following classes to perform output and input of characters t to/from files: – ofstream: Stream class to write on files– ifstream: Stream class to read from
files– fstream: Stream class to both read and
write from/to files.
Open a file
int main () {
ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0;
}
Class Name
Open a file
int main () {
ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0;
}
Object Name
Open a file
int main () {
ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0;
}
Method named open() is used to open a file.It takes 2 parameter. 1) file name 2) File Mode (optional)
Open a file
int main () {
ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0;
}
Method named close() is used to close a file.
Open a file
ios:in : Open for input operations. ios::out : Open for output operations. ios::ateSet : The initial position at the
end of the file. ios::app : All output operations are
performed at the end of the file, appending the content to the current content of the file.
ios::trunc : If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.
Writing to a File
Writing to the fileint main () { ofstream myfile;
myfile.open("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0;}
//is_open() returns true if the object point to opened file
Writing to the fileint main () { ofstream myfile;
myfile.open("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0;}
Writing to the file
Reading from a File
Reading from Fileint main () { string line; ifstream myfile;
myfile.open("example.txt"); if (myfile.is_open()) { myfile>>line;
cout << line << “\n”; myfile.close(); } else cout << "Unable to open file"; return 0;}
Return single word from the file to the variable line
Reading from Fileint main () { string line; ifstream myfile;
myfile.open("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << “\n”; } myfile.close(); } else cout << "Unable to open file"; return 0;}
Return single line from the file to the variable in the argument
Operator Overloading
Why Operator Overloading?
• The meaning of operators are already defined and fixed for basic types like: int, float, double etc in C++ language. For example: If you want to add two integers then, + operator is used. But, for user-defined types(like: objects), you can define the meaning of operator
• Readable code• Extension of language to include user-
defined types• I.e., classes
• Make operators sensitive to context
18
Simple Exampleclass complex {
public: double real, imag;}• I wish if I could do something as
belowcomplex a,b,c;a.real=12; a.imag=3;b.real=2; b.imag=6;
c = a + b;c = a-b;c = a*b ;
I.e., would like to write ordinary arithmetic expressionson this user-defined class.
#include<iostream>class complex{
public: int real,imaginary; complex add(complex ob)
{ complex t; t.real=real+ob.real; t.imaginary=imaginary+ob.imaginary; return(t); }};int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imaginary=3; obj2.real=8; obj2.imaginary=1;
result=obj1.add(obj2); //how if i could simply be result=obj1+obj2 cout<<result.real<<result.imaginary; return 0;}
Real =12Imaginary =3Complex add(obj){ t.real=12+obj.real;t.imaginary=3+obj.imaginary;Return t;}
Real =8Imaginary =1Complex add(obj){ t.real = 8+obj.real;t.imaginary=1+obj.imaginary;Return t;}
obj1
obj2
Operator Overloading 20CS-2303, C-Term 2010
General Format
returnType operator+(parameters);
any type keyword operator symbol
• Return type : may be whatever the operator returns
• Operator : is the keyword to be used for anyoverloading
• Operator symbol : may be any over loadable operator from the list.
#include<iostream>class complex{
public: int real,imaginary; complex operator+(complex ob)
{ complex t; t.real=real+ob.real; t.imaginary=imaginary+ob.imaginary; return(t); }};int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imaginary=3; obj2.real=8; obj2.imaginary=1; result=obj1+obj2 // result=obj1.operator+(obj2);cout<<result.real<<result.imaginary; return 0;}
Real =12Imaginary =3Complex operator+(obj){ t.real=12+obj.real;t.imaginary=3+obj.imaginary;Return t;}
Real =8Imaginary =1Complex operator+(obj){ t.real = 8+obj.real;t.imaginary=1+obj.imaginary;Return t;}
obj1
obj2
Questions?“A good question deserve a
good grade…”
Self Check
Self Check
• What are ifstream, ofstram, fstream?
–A function to operate file–Structure type pointer to the file–A class
Self Check
• What are ifstream, ofstram, fstream?
–A function to operate file–Structure type pointer to the file–A class
Self Check
• Where is is_open() defined?
–Class called ifstream/ofstream/fstream– Iostream.h– In the File
Self Check
• Where is is_open() defined?
–Class called ifstream/ofstream/fstream– Iostream.h– In the File
Self Check
• I want to write “Hello baabtra” in to a file
ofstream myfile; myfile.open("example.txt"); if (myfile.is_open()) { myfile << “Hello baabtra"; myfile.close(); }
Self Check
• I want to write “Hello baabtra” in to a file
ofstream myfile; myfile.open("example.txt"); if (myfile.is_open()) { myfile << “Hello baabtra"; myfile.close(); }
Self Check
• From previous program i want to store “hello” in a varaible
string line;ifstream myfile; myfile.open("example.txt"); if (myfile.is_open()) { myfile>>line;
cout << line << “\n”; myfile.close(); }
Self Check
• From previous program i want to store “hello” in a varaible
string line;ifstream myfile; myfile.open("example.txt"); if (myfile.is_open()) { myfile>>line;
cout << line << “\n”; myfile.close(); }
Self Check
• Act of taking more than one form with same name is called–Function overloading–Operator overloading–Inheritance–polymorphism
Self Check
• Act of taking more than one form with same name is called–Function overloading–Operator overloading–Inheritance–polymorphism“Function overloading and operator overloading are implementation or examples of
polymorphism”
class complex{
public: int real,imag; complex operator+(complex ob)
{ complex t; t.real=real+ob.real; t.imag=imag+ob.imag; return(t); }};
int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imag=3; obj2.real=8; obj2.imag=1; result=obj1+obj2cout<<result.real<<result.imag; return 0;}
Self CheckComplete the below
class complex{
public: int real,imag; complex operator+(complex ob)
{ complex t; t.real=real+ob.real; t.imag=imag+ob.imag; return(t); }};
int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imag=3; obj2.real=8; obj2.imag=1; result=obj1+obj2cout<<result.real<<result.imag; return 0;}
Self CheckComplete the below
End of day