Oop Presentation

Download Oop Presentation

Post on 18-Dec-2014

1.680 views

Category:

Technology

1 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> 1. Object Oriented Programming - Essential Techniques S G Ganesh sgganesh@gmail.com </li> <li> 2. Language Pragmatics A language is not just syntax and learning a language isnt just learning to program. Mastering a language requires good understanding of language semantics, pragmatics, traps and pitfalls with considerable experience in programming and design using that language. </li> <li> 3. Five Specific OO Tips and Techniques Well see 5 specific tips/techniques Based on understanding, experience and usage of language features Tips are about pragmatics of using language features The mistakes covered in the tips are errors in usage Examples in C++ and Java (sometimes in C#) </li> <li> 4. 1. Avoid calling virtual functions in constructors Constructors do not support runtime polymorphism fully as the derived objects are not constructed yet when base class constructor executes. So, avoid calling virtual functions from base- class constructors, which might result in subtle bugs in the code. </li> <li> 5. C++ resolves virtual function calls to base type struct base { base() { vfun(); } virtual void vfun() { cout bar(); } virtual void bar() =0; }; struct deri: base { void bar(){ } }; int main() { deri d; } // g++ output: // pure virtual method called // ABORT instruction (core dumped) </li> <li> 11. Dynamic method call in Java might lead to trouble // Java code class Base { public Base() { foo(); } public void foo() { System.out.println(quot;In Base's foo quot;); } } class Derived extends Base { public Derived() { i = new Integer(10); } public void foo() { System.out.println(quot;In Derived's foo quot; + i.toString() ); } private Integer i; } class Test { public static void main(String [] s) { new Derived().foo(); } } </li> <li> 12. Dynamic method call in Java might lead to trouble // Java code class Base { public Base() { foo(); } public void foo() { System.out.println(quot;In Base's foo quot;); } } class Derived extends Base { public Derived() { i = new Integer(10); } public void foo() { System.out.println(quot;In Derived's foo quot; + i.toString() ); } private Integer i; } class Test { public static void main(String [] s) { new Derived().foo(); } } // this program fails by throwing a NullPointerException </li> <li> 13. 2. Preserve the basic properties of methods while overriding Overriding the methods incorrectly can result in bugs and unexpected problems in the code. Adhering to Liskovs Substitution Principle is possible only when overriding is done properly. Make sure that the method signatures match exactly while overriding is done Provide semantics similar to the base method in the overridden method. </li> <li> 14. In C++, provide consistent default parameters struct Base { virtual void call(int val = 10) { cout</li></ul>