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

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Lecture 9 Concepts of Programming Languages
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Lecture #6 Classes and Objects.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Procedural and Object-Oriented Programming
Chapter 22 - C++ Templates
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Lecture 9 Concepts of Programming Languages
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes Short Review of Topics already covered Constructor
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
template< class T > class Stack { public:
C++ Plus Data Structures
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers, Dynamic Data, and Reference Types
Lecture 9 Concepts of Programming Languages
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 hide the 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 define class methods 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; }

7 using classes in a program #include 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; }

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 – 1 MaxStack top 3 2 public: 1 push( ) 0 pop( ) IsEmpty() IsFull() Dump()

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

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

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

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

16 stack x = pop( ); MaxStack – 1 MaxStack top (note: the pop sequence, Last In First Out)

17 stack x = pop( ); y = pop( ); MaxStack – 1 MaxStack top (note: the pop sequence, Last In First Out)

18 stack class defintion 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--]; }

19 stack class definition 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; };

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

21 Stack objects #include 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( ); // s1.pop( ); s1.push(55); s1.dump( ); // return 0; }

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 object reference #include 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; }

24 object reference 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:

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 constructors 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”); … }

27 dynamic allocation of objects #include // 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 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 destructor #include 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; };

30 destructor 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 } hortense constructing anonymous constructing. foo constructing foo destructing. anonymous destructing. anonymous constructing. anonymous destructing. hortense destructing.

31 pointer to objects 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 }

32 pointer to objects 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 }

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 … }