DOC PREVIEW
Purdue ECE 462 - Lecture notes

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ECE 462Object-Oriented Programmingusing C++ and JavaLecture 10Yung-Hsiang [email protected] 6 2Lab 5: Comparing the Performance of C++ and Java Container Classesmeasure the time • C++ and Java (linked) lists of strings– push_back 1 million strings– push_front 1 million strings• C++ and Java set of Student objects– insert 1 million distinct objects– insert 1 million duplicate objectsweek 6 3Container Classes & Methods• Some container classes (such as set) need special methods.• C++ set needs – operator == to determine whether a new element is already in the set– operator < to order the elements in the set• Java Set needs– class to implement Comparable interface– override compareTo methodweek 6 4Self Test(Do Java containers copy objects?)import java.io.*;import java.util.*;// similar to VectorForClassType.ccclass X{private int x_val;public X(int val) { x_val = val; }public int getVal() { return x_val; }public void setVal(int val) { x_val = val; }public String toString(){String rtv = "val = " + x_val;return rtv;}}week 6 5class VectorForClass {public static void main( String[] args ){Vector<X> XVec = new Vector<X>();X x1 = new X(2);X x2 = new X(3);X x3 = new X(5);XVec.addElement(x1);XVec.addElement(x2);XVec.addElement(x3);System.out.println(XVec);x2.setVal(1000);System.out.println(XVec);}}same output?week 6 6Self Test#include <iostream>#include <vector> using namespace std;int callcount = 0;class X { int x_val;public:X(int val = 74) { x_val = val; cout << "X(int) " << x_val << endl; callcount ++; }X (const X & xin) { x_val = xin.x_val; cout << "X(const X&) " << x_val << endl; callcount += 2; }X & operator = (const X & xin){ if ( this != &xin ) { x_val = xin.x_val; }cout << "operator = (const X&) " << x_val << endl; callcount += 3; return *this; }week 6 7int getVal(void) const { return x_val; }friend ostream & operator << (ostream & os, const X & xin); };ostream & operator << (ostream & os, const X & xin) {os << xin.x_val; return os;} int sum(vector<X> v ) {cout << "\nvector sum is: ";int sum = 0;vector<X>::iterator p = v.begin();while ( p != v.end() ){sum += p->getVal();p ++;}cout << sum << endl; return sum;}void print( vector<X> v ) {cout << "\nvector size is: " << v.size() << endl;vector<X>::iterator p = v.begin();while ( p != v.end() ){cout << (*p) << " "; p ++;cout << " ";}cout << endl;sum(v);}?week 6 8int main(){vector<X> vec; X x1( 2 ); X x2( 3 ); X x3( 5 ); vec.push_back( x1 );vec.push_back( x2 );vec.push_back( x3 );vec[2] = x1;print( vec ); cout << "callcount = " << callcount << endl;return 0;}?week 6 9How to Improve the Speed?(avoid copying objects)void print(vector<X> & ); // object reference, chapter 9void print(vector<X> & v ) {cout << "\nvector size is: " << v.size() << endl;vector<X>::iterator p = v.begin();while ( p != v.end() )cout << (*p++).getp() << " "; cout << endl << endl;}week 6 10Java IO Stream• Java has a wide selection of IO streams, for different data types, buffered or not, whether the file is readable from a text editor, and file size.•example: 98– 4-byte integer (hexadecimal): 00 00 00 62–62– ASCII: 39 38• Java IO streams do not provide buffering, unless BufferOutputStream is used.• Java streams can be combined.week 6 11File IO for Classimport java.io.*;class ClassWithFileIO{private int c_iVal;private double c_dVal;private String c_sVal;public ClassWithFileIO(String fileName) throws Exception{DataInputStream dis =new DataInputStream(new FileInputStream(fileName));c_iVal = dis.readInt();c_dVal = dis.readDouble();c_sVal = dis.readUTF();dis.close();System.out.println("c_iVal = " + c_iVal +"\nc_dVal = " + c_dVal +"\nc_sVal = " + c_sVal);}public ClassWithFileIO(int ival, double dval, String sval){c_iVal = ival;c_dVal = dval;c_sVal = sval;}week 6 12public void writeFile(String fileName) throws Exception{DataOutputStream dos =new DataOutputStream(new FileOutputStream(fileName) );dos.writeInt(c_iVal);dos.writeDouble(c_dVal);dos.writeUTF(c_sVal);dos.close();}}class WriteReadClass {public static void main( String[] args ) throws Exception{ClassWithFileIO cfio1 =new ClassWithFileIO(-15, 62.347, "Purdue Boiler");cfio1.writeFile("classdata");ClassWithFileIO cfio2 =new ClassWithFileIO("classdata");}}ECE 462Object-Oriented Programmingusing C++ and JavaLecture 11Yung-Hsiang [email protected] 6 14C++ IO Stream• open file for output: ofstream fileout(“filename”);fileout << 123 << “a string” << endl;ofstream fileout(“filename”, ios::app); // append mode• open file for input:ifstream filein(“filename”);int x;filein >> x;• open file for both input and output:fstream finout(“filename”, ios::in | ios::out);ios::binary for binary fileweek 6 15#include <iostream>#include <fstream>#include <string>using namespace std;class ClassWithFileIO{private:int c_iVal;double c_dVal;string c_sVal;public:ClassWithFileIO(string fileName){ifstream fin(fileName.c_str(), ios::binary);fin >> c_iVal;fin >> c_dVal;fin >> c_sVal;fin.close();cout << "c_iVal = " << c_iVal<< "\nc_dVal = " << c_dVal<< "\nc_sVal = " << c_sVal << endl;}void writeFile(string fileName){ofstream fout(fileName.c_str(), ios::binary);fout << c_iVal;fout << c_dVal;fout << c_sVal;fout.close();}ClassWithFileIO(int ival, double dval, string sval){c_iVal = ival;c_dVal = dval;c_sVal = sval;week 6 16}};int main(int argc, char * argv[]){ClassWithFileIO cfio1(-217, 64.35, "Purdue Boiler");cfio1.writeFile("classdata");ClassWithFileIO cfio2("classdata");return 0;}week 6 17Declaration, Definition, Initialization• declaration: inform the compiler the existence of an identifier (variable name, function name, class name ...)• definition: for variable, memory is allocated; for function, code is written• initialization: assign (default) value to variable• You should always initialize all variables and objects. Uninitialized values are a common source for errors.• Never leave anything uninitialized.week 6 18Declaration and Definitionint xvar; // definitionint xvar = 4; // definition and initializationextern int xvar; // declaration only// defined somewhere elseclass Student; // declarationclass Student { // definitionint s_id;int s_year;};double func(int, float); // declarationdouble func(int


View Full Document

Purdue ECE 462 - Lecture notes

Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?