DOC PREVIEW
Purdue ECE 462 - Lecture 22

This preview shows page 1-2-3-4-24-25-26-50-51-52-53 out of 53 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 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 53 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ECE 462Object-Oriented Programmingusing C++ and JavaLecture 22Yung-Hsiang [email protected] 13 2Programming Techniques• Some tricky concepts that often confuse programmers.• They really test your understanding of the languages.• They are also favorite questions in interviews (and of course, exams).• Knowing these answers help you avoid hard-to-find bugs.• Please pause the video as necessary and make sure you really understand the answers.week 13 3Program Crash Before Main Starts• Can it print anything else before "main starts"?• Can this program crash without printing "main starts"?• What is the implication if the answer is yes?– Program does not necessarily start at main.– It may take a long time before main starts.– An interactive may not be interactive at the beginning.int main(int argc, char * argv[]) {cout << "main starts" << endl;return 0; }public static void main(String [] args) {System.out.println("main starts");}week 13 4#include <iostream>using namespace std;class X {public:X() { cout << "X::X" << endl; }};X xobj;int main(int argc, char * argv[]) {cout << "main starts" << endl;return 0;}class X {public X() { System.out.println("X::X"); }}class programstart{private static X xobj = new X();public static void main( String[] args ){System.out.println("main start");}}What are the outputs?Pause the video and write your answers.week 13 5Non-Virtual and Virtual• Non-virtual (NV) calls virtual (V): base NV and derived V• V calls NV: derived V then derived NV• V calls V: derived V then derived V• NV calls NV: base NV and base NVweek 13 6#include <iostream>using namespace std;class BaseC {public:virtual void vfunc() { cout << "BaseC::vfunct" << endl; }void nvfunc() {cout << "BaseC::nvfunct" << endl; vfunc();}virtual void vfunc2() {cout << "BaseC::vfunc2" << endl;nvfunc();}};week 13 7class DerivedC: public BaseC {public:virtual void vfunc() { cout << "Derived::vfunct" << endl; }void nvfunc() {cout << "Derived::nvfunct" << endl; vfunc();}virtual void vfunc2() {cout << "DerivedC::vfunc2" << endl;nvfunc();}};week 13 8int main(int argc, char * argv[]) {BaseC * bobj = new DerivedC;bobj -> nvfunc();bobj -> vfunc2();return 0;}What are the outputs?Pause the video and write your answers.week 13 9Operator Calls Virtual#include <iostream>using namespace std;class X { };class Y: public X {};class Z: public Y {};ostream& operator << ( ostream& os, const X & arg ) {os << "operator << X" << endl;return os;}ostream& operator << ( ostream& os, const Y & arg ) {os << "operator << Y" << endl;return os;}week 13 10ostream& operator << ( ostream& os, const Z & arg ){os << "operator << Z" << endl;return os;}int main(int argc, char * argv[]){X * xptr[3];xptr[0] = new X;xptr[1] = new Y;xptr[2] = new Z;cout << (* xptr[0]);cout << (* xptr[1]);cout << (* xptr[2]);return 0;}What are the outputs?Pause the video and write your answers.week 13 11#include <iostream>using namespace std;class X {public:virtual void print() const { cout << "Xp " << endl; }};class Y: public X {public:void print() const { cout << "Yp " << endl; }};class Z: public Y {public:void print() const { cout << "Zp " << endl; }};week 13 12ostream& operator << ( ostream& os, const X & arg ) {os << "operator << X" << endl;arg.print();return os;}int main(int argc, char * argv[]){X * xptr[3];xptr[0] = new X;xptr[1] = new Y;xptr[2] = new Z;cout << (* xptr[0]);cout << (* xptr[1]);cout << (* xptr[2]);return 0;}week 13 13#include <iostream>#include <string>using namespace std;class X {public:virtual void print() const { cout << "Xp " << endl; }X() { cout << "X::X" << endl; }};class Y: public X {public:void print() const { cout << "Yp " << endl; }Y() { cout << "Y::Y" << endl; }};class Z: public Y {public:void print() const { cout << "Zp " << endl; }week 13 14Z() { cout << "Z::Z" << endl; }};ostream& operator << ( ostream& os, const X & arg ) {os << "operator << X" << endl;arg.print();return os;}ostream& operator << ( ostream& os, const Y & arg ) {os << "operator << Y" << endl;arg.print();return os;}ostream& operator << ( ostream& os, const Z & arg ) {os << "operator << Z" << endl;arg.print();return os;}week 13 15void funct(X * xptr) { cout << "fX " << endl; }void funct(Y * yptr) { cout << "fY " << endl; }void funct(Z * zptr) { cout << "fZ " << endl; }int main(int argc, char * argv[]){X * xptr[3];xptr[0] = new X; cout << endl; xptr[1] = new Y; cout << endl; xptr[2] = new Z; cout << endl;if (argc > 1) { xptr[2] = new Z; }else { xptr[2] = new Y; }funct(xptr[0]);funct(xptr[1]);funct(xptr[2]);xptr[0] -> print();xptr[1] -> print();xptr[2] -> print();week 13 16cout << (* xptr[0]);cout << (* xptr[1]);cout << (* xptr[2]);return 0;} What are the outputs?Pause the video and write your answers.week 13 17class X { public void print() { System.out.println(" X::print"); } }class Y extends X { public void print() { System.out.println(" Y::print"); } }class Z extends Y { public void print() { System.out.println(" Z::print"); } }class virtualoverload{public static void func(X obj) { System.out.println("func(X)");obj.print();} public static void func(Y obj) { System.out.println("func(Y)"); obj.print();}week 13 18public static void func(Z obj) { System.out.println("func(Z)"); obj.print();} public static void main( String[] args ){X [] xobj = new X[3];xobj[0] = new X();xobj[1] = new Y();xobj[2] = new Z();func(xobj[0]);func(xobj[1]);func(xobj[2]);}}week 13 19Overload → Method → Overloadclass Common {public static void func1(X obj) { System.out.println("func1(X)");} public static void func1(Y obj) { System.out.println("func1(Y)"); }public static void func1(Z obj) { System.out.println("func1(Z)"); } }week 13 20class X {public void print() { System.out.println("X::print"); Common.func1(this);}}class Y extends X {public void print() { System.out.println("Y::print"); Common.func1(this);}}class Z extends Y {public void print() { System.out.println("Z::print"); Common.func1(this);}}week 13 21class virtualoverload2{public static void func2(X obj) { System.out.println("\nfunc2(X)");} public static void func2(Y obj) { System.out.println("\nfunc2(Y)"); }public static void func2(Z obj) { System.out.println("\nfunc2(Z)"); } public static void main( String[] args ){X [] xobj = new X[3];xobj[0] = new X();xobj[1] = new Y();xobj[2] = new Z();week 13 22func2(xobj[0]);func2(xobj[1]);func2(xobj[2]);}}week 13 23class Common {public static void func1(X obj) { System.out.println("func1(X)");} public static void


View Full Document

Purdue ECE 462 - Lecture 22

Download Lecture 22
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 22 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 22 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?