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

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

What's hot (20)

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

Viewers also liked

Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objectsrahulsahay19
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
Programming Homework Help
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptLinux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell script
Kenny (netman)
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Lan, man and wan ppt final
Lan, man and wan ppt finalLan, man and wan ppt final
Lan, man and wan ppt finalArushi Garg
 

Viewers also liked (8)

Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
Object and class
Object and classObject and class
Object and class
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptLinux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell script
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Lan, man and wan ppt final
Lan, man and wan ppt finalLan, man and wan ppt final
Lan, man and wan ppt final
 

Similar to Introduction to c ++ part -2

Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 
C++ course start
C++ course startC++ course start
C++ course startNet3lem
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
Codemotion
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
Mahmoud Samir Fayed
 
Advance python
Advance pythonAdvance python
Advance python
pulkit agrawal
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 
Synapse india complain sharing info on chapter 8 operator overloading
Synapse india complain sharing info on chapter 8   operator overloadingSynapse india complain sharing info on chapter 8   operator overloading
Synapse india complain sharing info on chapter 8 operator overloading
SynapseindiaComplaints
 
The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84
Mahmoud Samir Fayed
 
srgoc
srgocsrgoc
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
Michael Redlich
 

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

Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
C++ course start
C++ course startC++ course start
C++ course start
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
 
Advance python
Advance pythonAdvance python
Advance python
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Synapse india complain sharing info on chapter 8 operator overloading
Synapse india complain sharing info on chapter 8   operator overloadingSynapse india complain sharing info on chapter 8   operator overloading
Synapse india complain sharing info on chapter 8 operator overloading
 
The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84
 
Lecture5
Lecture5Lecture5
Lecture5
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
srgoc
srgocsrgoc
srgoc
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 

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

Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Php database connectivity
Php database connectivityPhp database connectivity
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
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
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Blue brain
Blue brainBlue brain
5g
5g5g
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra

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

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Recently uploaded

FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 

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