1 / 35

Classes

Classes. classes and objects from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions) … }. Classes. member access specifiers private public protected information hiding

karif
Download Presentation

Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes • classes and objects • from object-oriented programming point of view • class declaration class class_name{ data members … methods (member functions) … }

  2. Classes • member access specifiers • private • public • protected • information hiding • private (and protected) can be used to hidethe data members and methods • public is used to expose the data members and methods to the outside

  3. information hiding class Person { public: boxer void setAge(unsigned n); unsigned getAge( ); private: unsigned age; } Person boxer, student; student boxer.setAge(27); student.setAge(18); cout << boxer.age; // Error: age is private data cout << boxer.getAge( ); // o.k. priviate: age public: setAge( ) getAge( ) 27 priviate: age public: setAge( ) getAge( ) 18

  4. Classes • class scope of private members • only can be accessed by its own class methods (functions) • define class methods • defined inside the class • inline declaration • good for small codes • defined outside the class

  5. define class methods • defined inside the class class Person { public: void setAge( unsigned n ) { age = n; } unsigned getAge( ){ return age; } private: unsigned age; };

  6. defined outside the class class Person { public: void setAge( unsigned n ); unsigned getAge( ); private: unsigned age; }; void Person::setAge( unsigned n ){ age = n; } unsigned Person::getAge( ) { return age; } define class methods

  7. #include <iostream> using namespace std; class Person { public: void setAge( unsigned n ) { age = n; } unsigned getAge( ) { return age; } private: unsigned age; }; int main() { Person p; Person stooges[ 3 ]; p.setAge( 12 ); stooges[ 0 ].setAge( 45 ); stooges[ 1 ].setAge( 46 ); stooges[ 2 ].setAge( 44 ); cout << p.getAge() << '\n'; for ( int i = 0; i < 3; i++ ) cout <<stooges[ i ].getAge( ) << '\n'; return 0; } using classes in a program

  8. classes • class and struct • both can be used to create classes • class • all members default to private if not specified • struct • all members default to public if not specified

  9. class and struct class C { int x; // private by default public: void setX( int X); // public } struct C { void setX( int X); // public by default private: int x; // private }

  10. application: stack class • stack • a Last In First Out (LIFO) data structure • contains: • an array for storing data, an index variable • basic operations • push: add an element to the stack • pop: extract an element from the stack • IsFull (IsEmpty): test whether the stack is full (empty) • dump: print out the contain of the stack • init: initialize the stack

  11. stack Initialize: arr[MaxStack] MaxStack – 1MaxStack top 3 2 public: 1 push( ) 0 pop( ) IsEmpty() IsFull() Dump() . . . 100 -1

  12. stack push(100); MaxStack – 1MaxStack top 3 2 1 0 (note: the sequence of pushing data ) 100 100 0

  13. stack push(100); push(30); MaxStack – 1MaxStack top 3 2 1 0 (note: the sequence of pushing data ) 30 100 100 1

  14. stack push(100); push(30); push (88); MaxStack – 1MaxStack top 3 2 1 0 (note: the sequence of pushing data ) 88 30 100 100 2

  15. stack push(100); push(30); push (88); push(307); MaxStack – 1MaxStack top 3 2 1 0 (note: the sequence of pushing data ) 307 88 30 100 100 3

  16. stack x = pop( ); MaxStack – 1MaxStack top 3 2 1 0 (note: the pop sequence, Last In First Out) 88 30 100 100 2

  17. stack x = pop( ); y = pop( ); MaxStack – 1MaxStack top 3 2 1 0 (note: the pop sequence, Last In First Out) 30 100 100 1

  18. class Stack { public: enum {MaxStack =100}; void init(){top =-1; dummy_val = -9999;} void push(int n ){ if (isFull()){ errMsg("Full stack.Can ’t push."); return; } arr [++top ]=n; } int pop(){ if (isEmpty()){ errMsg("Empty stack.Popping dummy value."); return dummy_val; } return arr [top--]; } stack class defintion

  19. bool isEmpty(){return top <0;} bool isFull(){ return top >=MaxStack -1;} void dump(){ cout <<"Stack contents,top to bottom:\n"; for (int i =top;i>=0;i--) cout <<’\t ’<<arr [i ]<<’\n ’; } private: void errMsg(const char*msg ) {cerr <<"\n***Stack operation failure:"<<msg <<’\n ’;} int top; int arr [MaxStack ]; int dummy_val; }; stack class definition

  20. create a Stack object Stack s1;// create an object s1 s1 arr[MaxStack] MaxStack – 1MaxStack top s1.init( ); dummy_val 3 public: 2 init( ) 1 push(int) 0 pop( ) dump( ) … . . . 100 -1

  21. #include <iostream> using namespace std; class Stack { // stack definition . . . } int main( ) { Stack s1; s1.init( ): s1.push(100); s1.push(30); s1.push(88); s1.push(307); s1.dump( ); // 100 30 88 307 s1.pop( ); s1.pop( ); s1.push(55); s1.dump( ); // 100 30 55 return 0; } Stack objects

  22. object reference • passing and returning objects by reference • cp. by value • save time and space • need to declare static if return by reference • const method • cannot change any values of data members of it’s object

  23. #include <iostream> using namespace std; class C { public: void set( int n ) { num = n; } int get( ) const { return num; } private: int num; }; void f( C& ); C& g( ); int main( ) { C c1, c2; f( c1 ); // pass by reference c2 = g( ); // return by reference cout << c2.get( ) << '\n'; return 0; } object reference

  24. void f( C& c ) { c.set( -999 ); cout << c.get() << '\n'; } C& g( ) { static C c3; // static, not auto c3.set( 123 ); return c3; } output: -999 123 object reference

  25. constructors and destructors • constructor • a method with the same name as it’s class • automatically executed while the object is created • may be overloaded, among them only one constructor is executed • destructor • with the same name plus a ~ sing • automatically executed while the object is destroyed

  26. class Person { public: Person( ) { name = "Unknown"; } Person( const string& n ); Person( const char* n ); void setName( const string& n ); void setName( const char* n ); const string& getName( ) const; private: string name; }; Person::Person( const string& n ) { name = n; } Person::Person( const char* n ) { name = n; } … int main( ) { Person anonymous; Person jc(“J. Coltrane”); … } constructors

  27. #include <cstdlib> // for malloc and calloc class Emp { public: elvis Emp() { /*...*/ } Emp( const char* name ) { /*...*/ } cher //... }; int main() { Emp* elvis = new Emp(); // default Emp* cher = new Emp( "Cher" ); // convert dynamic allocation of objects Emp object instance Emp object instance

  28. dynamic allocation of objects Emp* lotsOfEmps = new Emp[ 1000 ]; // default Emp* foo = malloc( sizeof( Emp ) ); // no constructor //... } Emp object array lotsOfEmps foo … Emp object

  29. #include <iostream> #include <string> using namespace std; class C { public: // default constructor C( ) { name = "anonymous"; cout << name << " constructing.\n"; } // parameterized constructor C( const char* n ) { name = n; cout << name << " constructing.\n"; } ~C( ) { cout << name << "destructing.\n"; } private: string name; }; destructor

  30. int main() { output: C c0( "hortense" ); { C c1; C c2( "foo" ); cout << '\n'; } // c1 and c2 destructors called C* ptr = new C( ); delete ptr; return 0; // c0 destructor called } destructor hortense constructing anonymous constructing. foo constructing foo destructing. anonymous destructing. anonymous constructing. anonymous destructing. hortense destructing.

  31. class C { public: void m() { /*...*/ } }; void f( C* ); // pass a pointer int main() { C c1; c1.m(); // object f( &c1 ); // address of object //... } void f( C* p ) { p->m(); // pointer to C object } pointer to objects

  32. class C { public: void m() { /*...*/ } }; void f( C& ); // pass by reference int main() { C c1; c1.m(); // object f( c1 ); //... } void f( C& c ) { c.m(); // object reference } pointer to objects

  33. pointer constant “this” • this • default constant pointer variable points to the object itself • no need to declare • an object: note: “this” is the object itself this …

  34. this class C { public: C( ) { this->x = 0;} // same as: x=0; private: int x; }

  35. this (example: copy file) … void File::copy(File& dest) { if (this == &dest) // cannot copy file to itself return; // otherwise, copy the file to dest … }

More Related