SlideShare a Scribd company logo
• 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 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
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
Reading from a File
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
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
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 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.
#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
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.
#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
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+obj2
cout<<result.real<<result.imag;
return 0;
}
Self Check
Complete 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+obj2
cout<<result.real<<result.imag;
return 0;
}
Self Check
Complete the below
End of day

More Related Content

PPTX
class and objects
PPT
C++ classes
PPT
Object-Oriented Programming Using C++
PPTX
Classes in c++ (OOP Presentation)
PDF
A COMPLETE FILE FOR C++
PDF
Programming in c++
PPTX
classes and objects in C++
PPT
Object oriented programming using c++
class and objects
C++ classes
Object-Oriented Programming Using C++
Classes in c++ (OOP Presentation)
A COMPLETE FILE FOR C++
Programming in c++
classes and objects in C++
Object oriented programming using c++

What's hot (20)

PPTX
Oop c++class(final).ppt
PPTX
Object oriented programming in C++
PPTX
Learn Concept of Class and Object in C# Part 3
PPTX
OOPS Basics With Example
PPT
Java oops PPT
PDF
Object oriented concepts
PPTX
Chapter 05 classes and objects
PPT
C++ oop
PPT
C++ classes tutorials
PPT
Class and object in C++
PPTX
OOP C++
PDF
Object-oriented Programming-with C#
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PDF
Classes and objects
PPTX
Functions, classes & objects in c++
PPT
Basic c#
PDF
Object Oriented Programming using C++ Part I
PPTX
Static Data Members and Member Functions
PDF
C++ Object oriented concepts & programming
PDF
Introduction to C++
Oop c++class(final).ppt
Object oriented programming in C++
Learn Concept of Class and Object in C# Part 3
OOPS Basics With Example
Java oops PPT
Object oriented concepts
Chapter 05 classes and objects
C++ oop
C++ classes tutorials
Class and object in C++
OOP C++
Object-oriented Programming-with C#
Basic Concepts of OOPs (Object Oriented Programming in Java)
Classes and objects
Functions, classes & objects in c++
Basic c#
Object Oriented Programming using C++ Part I
Static Data Members and Member Functions
C++ Object oriented concepts & programming
Introduction to C++
Ad

Viewers also liked (8)

PPTX
Classes And Objects
PDF
C# Assignmet Help
PPT
Object and class
PDF
Linux fundamental - Chap 14 shell script
PPT
C++ Function
PPT
Oops ppt
PPT
Object Oriented Programming Concepts
PPTX
Lan, man and wan ppt final
Classes And Objects
C# Assignmet Help
Object and class
Linux fundamental - Chap 14 shell script
C++ Function
Oops ppt
Object Oriented Programming Concepts
Lan, man and wan ppt final
Ad

Similar to Introduction to c ++ part -2 (20)

PPTX
Working with files in c++. file handling
PPTX
File handling.pptx
PPTX
C++ppt.pptx
PPTX
Data file handling
PPTX
IF OF parameters of oops.pptxy4ehz4w4ywhzdw
PPTX
File handling
PDF
Data file handling
PPTX
File & Exception Handling in C++.pptx
PPT
csc1201_lecture13.ppt
PPTX
File management in C++
PPT
Data file handling
PPTX
File_Handling_Slides_v2 of C++ (Bs CS.pptx
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PPTX
13 file handling in C++.pptx oops object oriented programming
PDF
Files and streams
PPTX
Introduction to files management systems
PPTX
Cs1123 10 file operations
PPTX
Stream classes in C++
PPTX
Filesin c++
PDF
Files in C++.pdf is the notes of cpp for reference
Working with files in c++. file handling
File handling.pptx
C++ppt.pptx
Data file handling
IF OF parameters of oops.pptxy4ehz4w4ywhzdw
File handling
Data file handling
File & Exception Handling in C++.pptx
csc1201_lecture13.ppt
File management in C++
Data file handling
File_Handling_Slides_v2 of C++ (Bs CS.pptx
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
13 file handling in C++.pptx oops object oriented programming
Files and streams
Introduction to files management systems
Cs1123 10 file operations
Stream classes in C++
Filesin c++
Files in C++.pdf is the notes of cpp for reference

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mushroom cultivation and it's methods.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Approach and Philosophy of On baking technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Machine Learning_overview_presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Encapsulation_ Review paper, used for researhc scholars
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mushroom cultivation and it's methods.pdf
Network Security Unit 5.pdf for BCA BBA.
Tartificialntelligence_presentation.pptx
Programs and apps: productivity, graphics, security and other tools
Approach and Philosophy of On baking technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
OMC Textile Division Presentation 2021.pptx
Spectroscopy.pptx food analysis technology
Machine Learning_overview_presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Univ-Connecticut-ChatGPT-Presentaion.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Encapsulation_ Review paper, used for researhc scholars

Introduction to c ++ part -2

  • 1. • What is the difference between c and c++? • What is a class? • What is an object? • What are cin,cout ? Recall
  • 2. Introduction to C++ File handling ,Operator Over loading Week 4 day 2
  • 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. Open a file int main () { ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0; } Class Name
  • 6. Open a file int main () { ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0; } Object Name
  • 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. 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. 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. Writing to a File
  • 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. 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
  • 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. 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
  • 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 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. #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. 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. #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. Questions? “A good question deserve a good grade…”
  • 24. Self Check • What are ifstream, ofstram, fstream? –A function to operate file –Structure type pointer to the file –A class
  • 25. Self Check • What are ifstream, ofstram, fstream? –A function to operate file –Structure type pointer to the file –A class
  • 26. Self Check • Where is is_open() defined? –Class called ifstream/ofstream/fstream –Iostream.h –In the File
  • 27. Self Check • Where is is_open() defined? –Class called ifstream/ofstream/fstream –Iostream.h –In the File
  • 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. 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. 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. 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. Self Check • Act of taking more than one form with same name is called –Function overloading –Operator overloading –Inheritance –polymorphism
  • 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. 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. 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