introduction to c ++ part -2

36
• What is the difference between c and c++? • What is a class? • What is an object? • What are cin,cout ? Recall

Upload: baabtracom-mentoring-partner-first-programming-school-in-india

Post on 22-Dec-2014

1.389 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Introduction to c ++   part -2

• What is the difference between c and c++?

• What is a class?• What is an object?• What are cin,cout ?

Recall

Page 2: Introduction to c ++   part -2

Introduction to C++ File handling ,Operator Over loading

Week 4 day 2

Page 3: Introduction to c ++   part -2

File Handling in C++

Page 4: Introduction to c ++   part -2

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.

Page 5: Introduction to c ++   part -2

Open a file

int main () {

ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0;

}

Class Name

Page 6: Introduction to c ++   part -2

Open a file

int main () {

ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0;

}

Object Name

Page 7: Introduction to c ++   part -2

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)

Page 8: Introduction to c ++   part -2

Open a file

int main () {

ofstream myfile; myfile.open ("example.txt"); myfile.close(); return 0;

}

Method named close() is used to close a file.

Page 9: Introduction to c ++   part -2

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.

Page 10: Introduction to c ++   part -2

Writing to a File

Page 11: Introduction to c ++   part -2

Writing to the fileint main () { ofstream myfile;

myfile.open("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0;}

//is_open() returns true if the object point to opened file

Page 12: Introduction to c ++   part -2

Writing to the fileint main () { ofstream myfile;

myfile.open("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0;}

Writing to the file

Page 13: Introduction to c ++   part -2

Reading from a File

Page 14: Introduction to c ++   part -2

Reading from Fileint main () { string line; ifstream myfile;

myfile.open("example.txt"); if (myfile.is_open()) { myfile>>line;

cout << line << “\n”; myfile.close(); } else cout << "Unable to open file"; return 0;}

Return single word from the file to the variable line

Page 15: Introduction to c ++   part -2

Reading from Fileint main () { string line; ifstream myfile;

myfile.open("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << “\n”; } myfile.close(); } else cout << "Unable to open file"; return 0;}

Return single line from the file to the variable in the argument

Page 16: Introduction to c ++   part -2

Operator Overloading

Page 17: Introduction to c ++   part -2

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

Page 18: Introduction to c ++   part -2

18

Simple Exampleclass complex {

public: double real, imag;}• I wish if I could do something as

belowcomplex a,b,c;a.real=12; a.imag=3;b.real=2; b.imag=6;

c = a + b;c = a-b;c = a*b ;

I.e., would like to write ordinary arithmetic expressionson this user-defined class.

Page 19: Introduction to c ++   part -2

#include<iostream>class complex{

public: int real,imaginary; complex add(complex ob)

{ complex t; t.real=real+ob.real; t.imaginary=imaginary+ob.imaginary; return(t); }};int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imaginary=3; obj2.real=8; obj2.imaginary=1;

result=obj1.add(obj2); //how if i could simply be result=obj1+obj2 cout<<result.real<<result.imaginary; return 0;}

Real =12Imaginary =3Complex add(obj){ t.real=12+obj.real;t.imaginary=3+obj.imaginary;Return t;}

Real =8Imaginary =1Complex add(obj){ t.real = 8+obj.real;t.imaginary=1+obj.imaginary;Return t;}

obj1

obj2

Page 20: Introduction to c ++   part -2

Operator Overloading 20CS-2303, C-Term 2010

General Format

returnType operator+(parameters);

any type keyword operator symbol

• Return type : may be whatever the operator returns

• Operator : is the keyword to be used for anyoverloading

• Operator symbol : may be any over loadable operator from the list.

Page 21: Introduction to c ++   part -2

#include<iostream>class complex{

public: int real,imaginary; complex operator+(complex ob)

{ complex t; t.real=real+ob.real; t.imaginary=imaginary+ob.imaginary; return(t); }};int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imaginary=3; obj2.real=8; obj2.imaginary=1; result=obj1+obj2 // result=obj1.operator+(obj2);cout<<result.real<<result.imaginary; return 0;}

Real =12Imaginary =3Complex operator+(obj){ t.real=12+obj.real;t.imaginary=3+obj.imaginary;Return t;}

Real =8Imaginary =1Complex operator+(obj){ t.real = 8+obj.real;t.imaginary=1+obj.imaginary;Return t;}

obj1

obj2

Page 22: Introduction to c ++   part -2

Questions?“A good question deserve a

good grade…”

Page 23: Introduction to c ++   part -2

Self Check

Page 24: Introduction to c ++   part -2

Self Check

• What are ifstream, ofstram, fstream?

–A function to operate file–Structure type pointer to the file–A class

Page 25: Introduction to c ++   part -2

Self Check

• What are ifstream, ofstram, fstream?

–A function to operate file–Structure type pointer to the file–A class

Page 26: Introduction to c ++   part -2

Self Check

• Where is is_open() defined?

–Class called ifstream/ofstream/fstream– Iostream.h– In the File

Page 27: Introduction to c ++   part -2

Self Check

• Where is is_open() defined?

–Class called ifstream/ofstream/fstream– Iostream.h– In the File

Page 28: Introduction to c ++   part -2

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(); }

Page 29: Introduction to c ++   part -2

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(); }

Page 30: Introduction to c ++   part -2

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(); }

Page 31: Introduction to c ++   part -2

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(); }

Page 32: Introduction to c ++   part -2

Self Check

• Act of taking more than one form with same name is called–Function overloading–Operator overloading–Inheritance–polymorphism

Page 33: Introduction to c ++   part -2

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”

Page 34: Introduction to c ++   part -2

class complex{

public: int real,imag; complex operator+(complex ob)

{ complex t; t.real=real+ob.real; t.imag=imag+ob.imag; return(t); }};

int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imag=3; obj2.real=8; obj2.imag=1; result=obj1+obj2cout<<result.real<<result.imag; return 0;}

Self CheckComplete the below

Page 35: Introduction to c ++   part -2

class complex{

public: int real,imag; complex operator+(complex ob)

{ complex t; t.real=real+ob.real; t.imag=imag+ob.imag; return(t); }};

int main(){ complex obj1,obj2,result; obj1.real=12; obj2.imag=3; obj2.real=8; obj2.imag=1; result=obj1+obj2cout<<result.real<<result.imag; return 0;}

Self CheckComplete the below

Page 36: Introduction to c ++   part -2

End of day