Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.

Similar presentations


Presentation on theme: "© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0."— Presentation transcript:

1 © Copyright Eliyahu Brutman Programming Techniques Course Version 1.0

2 © Copyright Eliyahu Brutman Chapter 3A – Data Abstraction Version 1.0

3 © Copyright Eliyahu Brutman Classes and Objects - 3 Table of contents Expressing Our World via OO Expending our programming language to support those new needs User-defined type Class declaration, definition, implementation Object creation Encapsulation and interfaces Access control

4 © Copyright Eliyahu Brutman Classes and Objects - 4 What Do We Need From OO? Express our world via Objects Responsibilities Operations / capabilities Inter-Relationship Cardinality / Multiplicity Hierarchies Generic approach via interfaces Objects invoking one another via message passing Achieve de-coupled, self-maintained instances Via implementation hiding With well defined interfaces

5 © Copyright Eliyahu Brutman Classes and Objects - 5 What Do We Expect from an OO Language? User-Defined types Define new types, to support our new “ language ” Same behavior as had been with built-in types int a=1, b=2, c; c= a+b; Expand to user-defined types String s1 = “ C++ “ ; String s2 = “ Class ” ; String s3; S3 = s1 + s2; Hide implementation What do we mean by “ hide ” ?

6 © Copyright Eliyahu Brutman Classes and Objects - 6 Declaration Of a Class class String { char* m_str; public: char* GetString(); void SetString(char* newStr); }; String.h Declaration of a class The implementation is not important for us, as users of the class It is important for us to understand which services we get from the class, and its precise usage We can use those services

7 © Copyright Eliyahu Brutman Classes and Objects - 7 Object Creation - Instantiation void main() { String s1, s2; s1.SetString( “ hello ” ); s2.SetString( “ bye ” ); printf( “ s1 is: %s, s2 is: %s\n ”, s1.GetString(), s2.GetString()); } main.cpp Using the String class Creating 2 objects Setting them with values, via SetString() method What is the internal representation of s1 and s2 ? Do we care, as users of the class? Accessing their values, via GetString() method, returning their value, and printing them in an old manner

8 © Copyright Eliyahu Brutman Classes and Objects - 8 Definition Of A Class class String { char* m_str; public: char* GetString(); void SetString(char* newStr); }; char* String::GetString() { return m_str; } void String::SetString(char* newStr) { int len=strlen(newStr); m_str = new char[len+1]; strcpy(m_str, newStr); } String.h Declaration of a class String.cpp Definition of a class

9 © Copyright Eliyahu Brutman Classes and Objects - 9 Object Creation - Instantiation Lets add another method int String::CompareString(String* strToCompare) { if (strcmp(strToCompare->m_str, m_str)==0) return 1; else return 0; } void main() { String s1, s2; s1.SetString( “ hello ” );s2.SetString( “ bye ” ); printf( “ s1 is: %s, s2 is: %s\n They are ”, s1.GetString(), s2.GetString()); if (s1.CompareString(&s2)==1) printf( “ equivalent\n ” ); else printf( “ non-equivalent\n ” ); }

10 © Copyright Eliyahu Brutman Classes and Objects - 10 Object Creation – Cont’d class String { char* str; int len; public: char*GetString(); void SetString(char* newStr); }; Assume that most comparisons in our applications are FALSE. Suggest a more efficient solution to the above implementation

11 © Copyright Eliyahu Brutman Classes and Objects - 11 Object Creation – Cont’d char* String::GetString() { return m_str; } void String::SetString(char* newStr) { m_len=strlen(newStr); m_str = new char[m_len+1]; strcpy(m_str, newStr); } int String::CompareString(String* strToCompare) { if (len != strToCompare->m_len) return 0; else if (strcmp(strToCompare->str, str)==0) return 1; else return 0; }

12 © Copyright Eliyahu Brutman Classes and Objects - 12 Encapsulation Do we need to change main? Why not? ENCAPSULATION !!! Implementation Hiding !!! We touched internal presentation only !! We have not touched the exposed interface !! We have kept the rule of IMPLEMENTATION HIDING !! Major OO Concept for better SW Engineering !!

13 © Copyright Eliyahu Brutman Classes and Objects - 13 C++: Limiting data visibility Visibility of class members limited in class declaration private – accessible only within the class protected – accessible only within the class and classes that inherit from it public – accessible anywhere private is the default access level class C{ int i; //private by default private: int j; protected: int k; public: int l; } This is just the basics, more to come …

14 © Copyright Eliyahu Brutman Classes and Objects - 14 Encapsulation and Access Control Expose minimum to clients Do not expose internal representation This is enforced by the private keyword Client cannot access private members Default access is private (that is why we did not have to write the keyword private before the data members) Expose interface to clients Enforced by the public keyword There is more to accessibility We will talk about it later

15 © Copyright Eliyahu Brutman Classes and Objects - 15 Data Abstraction - summary Define a type and operations on it Like built-in types: int, float, … Conceal data and implementation Use access modifiers  private, protected, public Why? Easier to design and implement Localizes future modifications

16 © Copyright Eliyahu Brutman Classes and Objects - 16 Issues Will it work? 1: char* String::getString(){return chars;} 2: void String::setString(char* value){ 3: length = strlen(value); 4: chars = new char[length+1]; 5: strcpy(value, chars); 6: } 7: int String::equals(String *other){ 8: if (length != other->length) 9: return 0; 10:if (strcmp(chars, other->chars) == 0) 11:return 1; 12: return 0; 13: }

17 © Copyright Eliyahu Brutman Classes and Objects - 17 Issues Nullity checks Before using a pointer, ask if it is null Always check the parameters 1: char* String::getString(){return chars;} 2: void String::setString(char* value){ if (value == NULL) return; 3: length = strlen(value); 4: chars = new char[length+1]; 5: strcpy(value, chars); 6: } 7: int String::equals(String *other){ if (other == null) return 0; 8: if (length != other->length) 9: return 0; if (length == 0) return 1; //if length != 0, then chars != NULL 10:if (strcmp(chars, other->chars) == 0) 11:return 1; 12: return 0; 13: }

18 © Copyright Eliyahu Brutman Chapter 3A – Object Construction And Overloading Version 1.0

19 © Copyright Eliyahu Brutman Classes and Objects - 19 Table of contents Lifetime Of Variables Object construction Constructor Destructor Overloading a Constructor Parameter matching

20 © Copyright Eliyahu Brutman Classes and Objects - 20 Lifetime Of Variables Lifetime of an object Lets recall lifetime in “ C ” Automatic  On the stack  Allocated when entering scope  What is a scope? Static  Global  Static within module  Static within scope Dynamic  Allocated on heap Global Code Heap Stack

21 © Copyright Eliyahu Brutman Classes and Objects - 21 Initialization Similar mechanism is used for objects Important to distinguish between Physical memory allocation Logical variable initialization When physical memory is set aside for an object, it is not initialized Having “ garbage ” data, from previous memory occupation We need to initialize this memory to a stable state, with “ good ” values, for proper execution

22 © Copyright Eliyahu Brutman Classes and Objects - 22 Object Creation Object creation When an object is created, an initialization method is invoked, to bring the object to its invariant state This method is called a Constructor It is a special method, invoked automatically, when an object is born (not physically allocated memory) It has the same name as the name of the class, that is how a compiler distinguishes it from other methods.

23 © Copyright Eliyahu Brutman Classes and Objects - 23 Definition Of A Class class String { char* m_str; int m_len; public: String(); char* GetString(); void SetString(char* newStr); }; String:: String() { m_len = 0; m_str = NULL; } char* String::GetString() { return m_str; } void String::SetString(char* newStr) { … } String.h Declaration of a class String.cpp Definition of a class

24 © Copyright Eliyahu Brutman Classes and Objects - 24 Destruction Object destruction When an object is destroyed  Going out of scope (for automatic variables)  Deallocated globally  Deallocated dynamically (delete operator) This method is called a Destructor It is a special method, invoked automatically, when the life cycle of an object ends It has the same name as the name of the class with a preceding ~

25 © Copyright Eliyahu Brutman Classes and Objects - 25 Definition Of A Class class String { char* m_str; int m_len; public: String(); ~String(); char* GetString(); void SetString(char* newStr); }; String::String() { m_len = 0; m_str = NULL; } String::~String() { m_len = 0; free(m_str); m_str = NULL; } … String.h Declaration of a class String.cpp Definition of a class

26 © Copyright Eliyahu Brutman Classes and Objects - 26 Defaults If I do not declare a constructor, the compiler will generate one for me If I do not declare a destructor, the compiler will generate one for me If I do not declare a copying semantics, the compiler will generate one for me Assignment Copy construction We will talk about it more later

27 © Copyright Eliyahu Brutman Classes and Objects - 27 Defining Additional Ctors The Empty Ctor (no parameters) is called the default Ctor It is called when we define an object in the following manner String s; If we define the object with initialization data, a different Ctor is invoked, for instance String s= “ hello ” ; In this case a Ctor with the parameter char* is invoked

28 © Copyright Eliyahu Brutman Classes and Objects - 28 Function overloading Function overloading: Several functions Same name Different parameters In C: prohibited In C++: allowed Also for constructors At each call site, compiler finds the target function by matching the parameters

29 © Copyright Eliyahu Brutman Classes and Objects - 29 Definition Of A Class class String { char* m_str; int m_len; public: String(); String(char*); ~String(); char* GetString(); void SetString(char* newStr); }; String::String() { m_len = 0; m_str = NULL; } String::String(char* str) { m_len = strlen(str); m_str = new char[m_len+1]; strcpy(m_str, str); } String::~String() { m_len = 0; free(m_str); m_str = NULL; } … String.h Declaration of a class String.cpp Definition of a class We have here 2 functions with the same name !! How is it possible ?

30 © Copyright Eliyahu Brutman Classes and Objects - 30 Overloading Overloading is declaring the same function name more than once In “ C ” this would result in compilation error C++ permits this If the function have different parameters It performs “ name mangling ” (decoration) to produce different symbols for each function Upon method call, resolving should be taken to decide, which function should be actually called The caller “ sends a message ” The actual method is invoked after the compiler performed the suitable matching

31 © Copyright Eliyahu Brutman Classes and Objects - 31 How is it done? Name mangling Compiler encodes parameter types in the method name Compiler void h(int)void h(int,char) void h(void) GNU gcc 3.x, IA64 compilers _Z1hi_Z1hic_Z1hv GNU gcc 2.9.x h_Fih_Fich_Fv Microsoft Visual C++ v6, v7 ?h@@YAXH@Z?h@@YAXHD@Z?h@@YAXXZ

32 © Copyright Eliyahu Brutman Classes and Objects - 32 Parameter Matching In C++, parameter matching is performed, in the following order Exact matching is examined Built in type matching User defined matching  If suitable Ctor exists  Casting is defined In our example … Parameter passing By value By pointer By reference (we will talk more about it later … )

33 © Copyright Eliyahu Brutman Classes and Objects - 33 Using constructors: String int main(){ String str; String str2(“Hi”); String str3 = “Hello”; foo (str3); //foo declared as foo(String) } Calls String() Calls String(“Hi”) Calls String(“Hello”) Calls copy-constructor String(const String& other)

34 © Copyright Eliyahu Brutman Classes and Objects - 34 Copy-constructors Single parameter – reference to the copied object class X{ public X(const X& x); } Called by the compiler When an object is initialized using another object of the same class  X x; X x1(x); When an object is passed by value  void foo(X xparam); foo(x); If no copy-constructor defined, compiler adds one Bitwise copy of the copied object Not always fits

35 © Copyright Eliyahu Brutman Classes and Objects - 35 String copy-constructor char* String::getString(){return chars;} String::String(const String& other){ length = other.length; chars = new char[length+1]; strcpy(other.chars, chars); } String.cpp


Download ppt "© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0."

Similar presentations


Ads by Google