DOC PREVIEW
Purdue ECE 462 - Lecture notes

This preview shows page 1-2-3-27-28-29 out of 29 pages.

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

Unformatted text preview:

ECE 462Object-Oriented Programmingusing C++ and JavaLecture 3Yung-Hsiang [email protected] 2 2Mini Reviewconcepts explained so far:• object: identity, states (attributes), behavior (methods)• class: interface and implementation• inheritance: code reuse• polymorphism: different derived classes have different behavior• encapsulation: hide information inside objectsweek 2 3Execute C++ and Java Programs•C++– Linux: g++– Windows: g++ in cygwin•Java– Linux: javac to compile and java to execute or Netbeans to compile and execute– Windows: javac to compile and java to execute or Netbeans to compile and executeweek 2 4//AddArray.javapublic class AddArray { //(A)public static void main( String[] args ) //(B){int[] data = { 0, 1, 2, 3, 4, 5, 9, 8, 7, 6 }; //(C)System.out.println( "The sum is: " //(D)+ addArray(data) ); }public static int addArray( int[] a ) { //(E)int sum = 0; for ( int i=0; i < a.length; i++ ) sum += a[i]; return sum; }}similar to (int argc, char * argv[]) in Csimilar to printf in Cweek 2 5//Polymorph.javaclass User { private String name;private int age;public User( String str, int yy ) { name = str; age = yy; } public void print() { System.out.print( "name: " + name + " age: " + age ); }} // ; not needed for Javaclass StudentUser extends User { // derived class private String schoolEnrolled;public StudentUser( String nam, int y, String sch ) { super(nam, y); // User::print(); in C++ schoolEnrolled = sch;}public void print() { super.print(); System.out.print( " School: " + schoolEnrolled );}}week 2 6class Test {public static void main( String[] args ) {User[] users = new User[3]; users[0] = new User( "Buster Dandy", 34 ); users[1] = new StudentUser("Missy Showoff",25,"Math"); users[2] = new User( "Mister Meister", 28 ); for (int i=0; i<3; i++) { users[i].print(); System.out.println(); }}}Encapsulation– users[2].name ⇒ compile-time error– users[2].age ⇒ compile-time error– Information is available only if an object allows the visibility; visibility is specified in the class interface.week 2 7• Inheritance: – A derived class has all the public and protected properties (= attributes + methods) of the base class. Private properties are not inherited.– A pointer for a base class can point to an object of a derived class.– A pointer for a derived class can not point to an object of a base class.– A StudentUser is a User, incorrect the other way• Polymorphism: – A pointer's behavior changes based on the object being pointed to.– determined at run-timeweek 2 8Polymorphism• users[1] = new StudentUser("Missy Showoff",25,"Math");– behavior as a StudentUser– users[1].print will include the school• users[1] = new User("Missy Showoff",25);– behavior as a User – users[1].print will not include the schoolUserStudentUserweek 2 9Self TestWhat concept is used? Choose your answers from1. encapsulation2. inheritance3. polymorphismQuestions:• a StudentUser object can call super.print();• a StudentUser object can be assigned to (RHS) to a User object• in StudentUser’s print, accessing name is an error• users[0].print() and users[1].print() behave differentlyweek 2 10Self TestWhat concept is used? Choose your answers from1. encapsulation2. inheritance3. polymorphismQuestions:• a StudentUser object can call super.print(); ⇒ 2• a StudentUser object can be assigned to (RHS) to a User object ⇒ 2• in StudentUser’s print, accessing name is an error ⇒ 1• users[0].print() and users[1].print() behave differently ⇒3week 2 11class User { private String name;private int age;public User( String str, int yy ) { name = str; age = yy; } public void setAge(int newage) {// an example of information consistencyif (age > newage) { System.out.println("You can't become younger."); }else { age = newage; }}public void print() { System.out.print( "name: " + name + " age: " + age ); }}In general, it is a bad programming habit to provide “setXXX” methods, worse to make them public.week 2 12// ----------------------- User1.cc#include <iostream>#include <string>using namespace std;class User { string name; // default visibility is privateint age;public: User( string str, int yy ) { name = str; age = yy; } // constructorvoid print() { cout << "name: " << name << " age: " << age << endl; }}; // ; not needed for Javaint main(){User u( "Zaphod", 119 ); // create an objectu.print();return 0;}commentconstructor: a method with the same name as the classweek 2 13// ----------------------- User2.cc#include <iostream>#include <string>using namespace std;class User { string name;int age;public: User( string str, int yy ); void print();};User::User( string str, int yy ) {// this does not have to in the same file name = str; age = yy; } void User::print() { cout << "name: " << name << " age: " << age << endl; }int main(){User u( "Zaphod", 119 );u.print();return 0;}week 2 14// ----------------------- Polymorph.cc#include <iostream>#include <string>using namespace std;class User {string name; int age; public:User(string nm, int a) {name=nm; age=a;} virtual void print() { // virtual explained later cout << "Name: " << name << " Age: " << age; }};week 2 15class StudentUser : public User { string schoolEnrolled; // new attributepublic: StudentUser(string nam, int y, string school) : User(nam, y){ schoolEnrolled = school; }void print() { User::print(); // print a user's attribute cout << " School Enrolled: " << schoolEnrolled;}};week 2 16int main(){User* users[3]; users[0] = new User( "Buster Dandy", 34 ); users[1] = new StudentUser("Missy Showoff", 25, "Math");// a StudentUser is a Userusers[2] = new User( "Mister Meister", 28 ); for (int i=0; i<3; i++) { users[i]->print(); cout << endl;}// this program has a memory leak; ignore it for now return 0;}ECE 462Object-Oriented Programmingusing C++ and JavaLecture 4Yung-Hsiang [email protected] 2 18Self Test• Java: class Shape { ...}class Triangle ______ Shape { ...}•C++class Shape {


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?