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

What's hot (20)

PPTX
Oop c++class(final).ppt
Alok Kumar
Ā 
PPTX
Object oriented programming in C++
jehan1987
Ā 
PPTX
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
Ā 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
Ā 
PPT
Java oops PPT
kishu0005
Ā 
PDF
Object oriented concepts
Pranali Chaudhari
Ā 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
Ā 
PPT
C++ oop
Sunil OS
Ā 
PPT
C++ classes tutorials
FALLEE31188
Ā 
PPT
Class and object in C++
rprajat007
Ā 
PPTX
OOP C++
Ahmed Farag
Ā 
PDF
Object-oriented Programming-with C#
Doncho Minkov
Ā 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
Ā 
PDF
Classes and objects
Nilesh Dalvi
Ā 
PPTX
Functions, classes & objects in c++
ThamizhselviKrishnam
Ā 
PPT
Basic c#
kishore4268
Ā 
PDF
Object Oriented Programming using C++ Part I
Ajit Nayak
Ā 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
Ā 
PDF
C++ Object oriented concepts & programming
nirajmandaliya
Ā 
PDF
Introduction to C++
Pranali Chaudhari
Ā 
Oop c++class(final).ppt
Alok Kumar
Ā 
Object oriented programming in C++
jehan1987
Ā 
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
Ā 
OOPS Basics With Example
Thooyavan Venkatachalam
Ā 
Java oops PPT
kishu0005
Ā 
Object oriented concepts
Pranali Chaudhari
Ā 
Chapter 05 classes and objects
Praveen M Jigajinni
Ā 
C++ oop
Sunil OS
Ā 
C++ classes tutorials
FALLEE31188
Ā 
Class and object in C++
rprajat007
Ā 
OOP C++
Ahmed Farag
Ā 
Object-oriented Programming-with C#
Doncho Minkov
Ā 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
Ā 
Classes and objects
Nilesh Dalvi
Ā 
Functions, classes & objects in c++
ThamizhselviKrishnam
Ā 
Basic c#
kishore4268
Ā 
Object Oriented Programming using C++ Part I
Ajit Nayak
Ā 
Static Data Members and Member Functions
MOHIT AGARWAL
Ā 
C++ Object oriented concepts & programming
nirajmandaliya
Ā 
Introduction to C++
Pranali Chaudhari
Ā 

Viewers also liked (8)

PPTX
Classes And Objects
rahulsahay19
Ā 
PDF
C# Assignmet Help
Programming Homework Help
Ā 
PPT
Object and class
mohit tripathi
Ā 
PDF
Linux fundamental - Chap 14 shell script
Kenny (netman)
Ā 
PPT
C++ Function
Hajar
Ā 
PPT
Oops ppt
abhayjuneja
Ā 
PPT
Object Oriented Programming Concepts
thinkphp
Ā 
PPTX
Lan, man and wan ppt final
Arushi Garg
Ā 
Classes And Objects
rahulsahay19
Ā 
C# Assignmet Help
Programming Homework Help
Ā 
Object and class
mohit tripathi
Ā 
Linux fundamental - Chap 14 shell script
Kenny (netman)
Ā 
C++ Function
Hajar
Ā 
Oops ppt
abhayjuneja
Ā 
Object Oriented Programming Concepts
thinkphp
Ā 
Lan, man and wan ppt final
Arushi Garg
Ā 
Ad

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

PPTX
Chapter 4
temkin abdlkader
Ā 
PDF
Ds lab handouts
Ayesha Bhatti
Ā 
PDF
Functions and modules in python
Karin Lagesen
Ā 
PDF
C++ course start
Net3lem
Ā 
PDF
Ch-4-Operator Overloading.pdf
esuEthopi
Ā 
PDF
TWINS: OOP and FP - Warburton
Codemotion
Ā 
PPTX
Chp 3 C++ for newbies, learn fast and earn fast
nhbinaaa112
Ā 
PDF
Java 8 Workshop
Mario Fusco
Ā 
PDF
The Ring programming language version 1.5.2 book - Part 7 of 181
Mahmoud Samir Fayed
Ā 
PPTX
Advance python
pulkit agrawal
Ā 
PDF
data structure book in c++ and c in easy wording
yhrcxd8wpm
Ā 
PPTX
C++ theory
Shyam Khant
Ā 
PDF
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
Ā 
PDF
Operator overloading
Pranali Chaudhari
Ā 
PPT
Lec 26.27-operator overloading
Princess Sam
Ā 
PPTX
Introduction to files management systems
araba8
Ā 
PPTX
Object oriented programming system with C++
msharshitha03s
Ā 
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
ssuser10ed71
Ā 
PPT
Synapse india complain sharing info on chapter 8 operator overloading
SynapseindiaComplaints
Ā 
PPTX
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
adharshan2706
Ā 
Chapter 4
temkin abdlkader
Ā 
Ds lab handouts
Ayesha Bhatti
Ā 
Functions and modules in python
Karin Lagesen
Ā 
C++ course start
Net3lem
Ā 
Ch-4-Operator Overloading.pdf
esuEthopi
Ā 
TWINS: OOP and FP - Warburton
Codemotion
Ā 
Chp 3 C++ for newbies, learn fast and earn fast
nhbinaaa112
Ā 
Java 8 Workshop
Mario Fusco
Ā 
The Ring programming language version 1.5.2 book - Part 7 of 181
Mahmoud Samir Fayed
Ā 
Advance python
pulkit agrawal
Ā 
data structure book in c++ and c in easy wording
yhrcxd8wpm
Ā 
C++ theory
Shyam Khant
Ā 
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
Ā 
Operator overloading
Pranali Chaudhari
Ā 
Lec 26.27-operator overloading
Princess Sam
Ā 
Introduction to files management systems
araba8
Ā 
Object oriented programming system with C++
msharshitha03s
Ā 
Chp8_C++_Functions_Part2_User-defined functions.pptx
ssuser10ed71
Ā 
Synapse india complain sharing info on chapter 8 operator overloading
SynapseindiaComplaints
Ā 
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
adharshan2706
Ā 
Ad

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

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

Recently uploaded (20)

PDF
Market Insight : ETH Dominance Returns
CIFDAQ
Ā 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
Ā 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
Ā 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
Ā 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
Ā 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
Ā 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
Ā 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
Ā 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
Ā 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
Ā 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
Ā 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
Ā 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
Ā 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
Ā 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
Ā 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
Ā 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
Ā 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
Ā 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
Ā 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
Ā 
Market Insight : ETH Dominance Returns
CIFDAQ
Ā 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
Ā 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
Ā 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
Ā 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
Ā 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
Ā 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
Ā 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
Ā 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
Ā 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
Ā 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
Ā 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
Ā 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
Ā 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
Ā 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
Ā 
Presentation about Hardware and Software in Computer
snehamodhawadiya
Ā 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
Ā 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
Ā 
Researching The Best Chat SDK Providers in 2025
Ray Fields
Ā 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
Ā 

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