• 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: ...
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...
Open a file
int main ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile.close();
return 0;
}
Method named close() i...
Open a file
 ios:in : Open for input operations.
 ios::out : Open for output operations.
 ios::ateSet : The initial pos...
Writing to a File
Writing to the file
int main ()
{
ofstream myfile; myfile.open("example.txt");
if (myfile.is_open())
{
myfile << "This is ...
Writing to the file
int main ()
{
ofstream myfile; myfile.open("example.txt");
if (myfile.is_open())
{
myfile << "This is ...
Reading from a File
Reading from File
int main ()
{
string line; ifstream myfile; myfile.open("example.txt");
if (myfile.is_open())
{
myfile>>...
Reading from File
int main ()
{
string line; ifstream myfile; myfile.open("example.txt");
if (myfile.is_open())
{
while ( ...
Operator Overloading
Why Operator Overloading?
• The meaning of operators are already defined
and fixed for basic types like: int, float, doubl...
18
Simple Example
class complex {
public: double real, imag;
}
• I wish if I could do something as below
complex a,b,c;
a....
#include<iostream>
class complex
{
public: int real,imaginary;
complex add(complex ob)
{
complex t;
t.real=real+ob.real;
t...
Operator Overloading CS-2303, C-Term 2010 20
General Format
returnType operator+(parameters);
  
any type keyword opera...
#include<iostream>
class complex
{
public: int real,imaginary;
complex operator+(complex ob)
{
complex t;
t.real=real+ob.r...
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(...
Self Check
• I want to write “Hello baabtra” in
to a file
ofstream myfile;
myfile.open("example.txt");
if (myfile.is_open(...
Self Check
• From previous program i want to
store “hello” in a varaible
string line;
ifstream myfile;
myfile.open("exampl...
Self Check
• From previous program i want to
store “hello” in a varaible
string line;
ifstream myfile;
myfile.open("exampl...
Self Check
• Act of taking more than one form
with same name is called
–Function overloading
–Operator overloading
–Inheri...
Self Check
• Act of taking more than one form
with same name is called
–Function overloading
–Operator overloading
–Inheri...
class complex
{
public: int real,imag;
complex operator+(complex ob)
{
complex t;
t.real=real+ob.real;
t.imag=imag+ob.imag...
class complex
{
public: int real,imag;
complex operator+(complex ob)
{
complex t;
t.real=real+ob.real;
t.imag=imag+ob.imag...
End of day
Upcoming SlideShare
Loading in...5
×

Introduction to c ++ part -2

3,172

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,172
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to c ++ part -2

  1. 1. • What is the difference between c and c++? • What is a class? • What is an object? • What are cin,cout ? Recall
  2. 2. Introduction to C++ File handling ,Operator Over loading Week 4 day 2
  3. 3. File Handling in C++
  4. 4. 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.
  5. 5. Open a file int main () { ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0; } Class Name
  6. 6. Open a file int main () { ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0; } Object Name
  7. 7. 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)
  8. 8. Open a file int main () { ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0; } Method named close() is used to close a file.
  9. 9. 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.
  10. 10. Writing to a File
  11. 11. Writing to the file int 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
  12. 12. Writing to the file int 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
  13. 13. Reading from a File
  14. 14. Reading from File int 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
  15. 15. Reading from File int 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
  16. 16. Operator Overloading
  17. 17. 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. 18. 18 Simple Example class complex { public: double real, imag; } • I wish if I could do something as below complex 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 expressions on this user-defined class.
  19. 19. #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 =12 Imaginary =3 Complex add(obj) { t.real=12+obj.real; t.imaginary=3+obj.imaginary; Return t; } Real =8 Imaginary =1 Complex add(obj) { t.real = 8+obj.real; t.imaginary=1+obj.imaginary; Return t; } obj1 obj2
  20. 20. Operator Overloading CS-2303, C-Term 2010 20 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.
  21. 21. #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 =12 Imaginary =3 Complex operator+(obj) { t.real=12+obj.real; t.imaginary=3+obj.imaginary; Return t; } Real =8 Imaginary =1 Complex operator+(obj) { t.real = 8+obj.real; t.imaginary=1+obj.imaginary; Return t; } obj1 obj2
  22. 22. Questions? “A good question deserve a good grade…”
  23. 23. Self Check
  24. 24. Self Check • What are ifstream, ofstram, fstream? –A function to operate file –Structure type pointer to the file –A class
  25. 25. Self Check • What are ifstream, ofstram, fstream? –A function to operate file –Structure type pointer to the file –A class
  26. 26. Self Check • Where is is_open() defined? –Class called ifstream/ofstream/fstream –Iostream.h –In the File
  27. 27. Self Check • Where is is_open() defined? –Class called ifstream/ofstream/fstream –Iostream.h –In the File
  28. 28. 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();
  29. 29. 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();
  30. 30. 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(); }
  31. 31. 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(); }
  32. 32. Self Check • Act of taking more than one form with same name is called –Function overloading –Operator overloading –Inheritance –polymorphism
  33. 33. 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”
  34. 34. 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+obj2 cout<<result.real<<result.imag; return 0; } Self Check Complete the below
  35. 35. 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+obj2 cout<<result.real<<result.imag; return 0; } Self Check Complete the below
  36. 36. End of day
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×