DOC PREVIEW
Purdue ECE 462 - Program Structure

This preview shows page 1-2-3-4-5-6-7-8-9-10-11-79-80-81-82-83-84-85-86-87-88-89-90-158-159-160-161-162-163-164-165-166-167-168 out of 168 pages.

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

Unformatted text preview:

Program StructureGenerate Code from UML Class DiagramWhat is Method / MessageCommon MistakesClass ExamplesEncapsulation and PolymorphismWhy Encapsulation?PolymorphismVirtual Function in C++Polymorphism is determined at"run-time"Constructor and Destructor (C++)Constructor (both C++ and Java)Object CreationMemory LeakDestructor (C++ only)Constant AttributesECE 462Object-Oriented Programmingusing C++ and JavaProgram StructureYung-Hsiang [email protected] Program Structure 2Objects and Classes• Organize objects with similar properties (attributes and behaviors) into classes: human, car, computer, phone, window, circle, rectangle, triangle, bridge, skyscraper ...• Inheritance: find commonality among classes (not objects) and create a base class that represents the common interfaces and behavior• Common interface of Shape:– color– line style and thickness– area but it is computeddifferently in each derived classShapeCircleTriangleRectangleYHL Program Structure 3Reuse (Base or Derived?)• Attribute (member data)– If an attribute is shared by all derived classes, the attribute should be declared in the base class. example: color, line style, thickness.– If an attribute is unique to a class, it should be declared in this derived class, example: radius for circle.• Behavior (member function, method):– If a method is available to all derived classes, such as getAreaand setLineStyle, it should be declared in the base class.– If the method’s implementation is applicable to all derived classes, it should be implemented in the base class.– If the implementation is unique to each derived class, it should be implemented in the derived classes.Generate Code fromUML Class DiagramYHL Program Structure 5Use Netbeans• to draw a UML class diagram• to generate code.YHL Program Structure 6YHL Program Structure 7YHL Program Structure 8YHL Program Structure 9YHL Program Structure 10YHL Program Structure 11YHL Program Structure 12YHL Program Structure 13YHL Program Structure 14YHL Program Structure 15C++: string (lower case)Java: String (upper case)Both languages are case sensitiveYHL Program Structure 16name of classYHL Program Structure 17attributesbehaviors(functions,operations)YHL Program Structure 18YHL Program Structure 19YHL Program Structure 20YHL Program Structure 21YHL Program Structure 22YHL Program Structure 23YHL Program Structure 24YHL Program Structure 25YHL Program Structure 26YHL Program Structure 27YHL Program Structure 28YHL Program Structure 29base / derived classesGenerate Java Codefrom Class DiagramYHL Program Structure 31YHL Program Structure 32Open a Java project so that Netbeans can generate code from the UML diagramYHL Program Structure 33YHL Program Structure 34YHL Program Structure 35YHL Program Structure 36YHL Program Structure 37YHL Program Structure 38YHL Program Structure 39YHL Program Structure 40YHL Program Structure 41YHL Program Structure 42YHL Program Structure 43YHL Program Structure 44YHL Program Structure 45YHL Program Structure 46C++ and Java Syntaxclass Student extends Person class Student: public Personfinal String p_lastName;const string p_lastName;public class Person {public Person(String ln, String fn) {class Person{public:Person(string ln, string fn);p1.print();p1.print();Person p1 = new Person("Johnson", "Tom");Person p1("Johnson", "Tom");public static void main(String[] args) {int main(int argc, char * argv[])Javac++YHL Program Structure 47main alwaysinside a classCreating an object must use new.main has only one parameterJavaYHL Program Structure 48main always outside a classCreating an object does not use new.(C++ can also use new to create anobject; this will be discussed later.)main has two parametersC++YHL Program Structure 49Encapsulation• Both C++ and Java provide three levels of protection:– private (default), accessible to only the class– protected, accessible to the class and its derived classes– public, no restriction• The three protection levels apply to both attributes (also called member data) and functions (also called member function or methods)• In general, keep as many attributes and functions private as possible. Allow only limited accesses.YHL Program Structure 50JavaString (uppercase)public functionpublic return-type functionno return-type for constructorno header file for Javano destructorno virtualSystem.out.println to printYHL Program Structure 51C++string (lowercase)public:no return-type for constructordestructor, no return type, virtualYHL Program Structure 52C++line breakinitializationcout to printYHL Program Structure 53Javaextends for derived classsuper for base class constructorsuper for base class functionYHL Program Structure 54C++public: for derived classYHL Program Structure 55class name for base class constructorclass name for base class functionC++YHL Program Structure 56What is Method / Message•A message is a request to an object to do something. An object can receive a message if it is declared as a method in a base class or the corresponding class.• In OOP, many objects are created and they interact by “sending messages”, for example,– A Driver object sends a message “accelerate” to a MotorVehicle object.– An Instructor object sends a message “submitHomework” to a Student object.– A Caller object sends a message “callNumber” to a MobilePhone object.YHL Program Structure 57Message• not a network concept• the mechanism to interact with an object– ask a bridge about its length (no parameter)– turn on a light (no parameter)– accelerate a car (parameter = acceleration, m/s2)– add a customer to a database (parameter = customer)– ask a customer of the credit number• Disallowed messages are checked at compile time: compiler error if you ask a light bulb to accelerate.YHL Program Structure 58• A Driver object (dobj) sends an “accelerate” message to a Car object (cobj)⇒ cobj.accelerate();• A Teacher object (tobj) sends a message to a Student object (sobj) to submit a Homework object (hobj)⇒ sobj.submit(hobj);• A Person object (pobj) sends a Light Bulb object (bobj) message to turn on⇒ bobj.on();The object is the recipient of the message. Where is the sender? It is implicit by the location of the message.YHL Program Structure 59a Person object canaccept "print" messageYHL Program Structure 60sent a print message to a Person objectCommon MistakesYHL Program Structure 62A local variable has the same name as an attributeYHL


View Full Document

Purdue ECE 462 - Program Structure

Download Program Structure
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 Program Structure 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 Program Structure 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?