1 / 25

Class and Objects

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 in C but with the addition of functions An Object is an instance of a class

doloresk
Télécharger la présentation

Class and Objects

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. 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 in C but with the addition of functions • An Object is an instance of a class • Objects share the same functions with other objects of the same class, but each object or instance has its own copy of the data structure I.e, the member data

  2. Member Functions • These are functions declared within a class definition • They are referred to as methods of the class • Member Functions may be public or private (as can member data)

  3. Public and Private • A public member (data or function) is one that can be read or written (data) or called (functions) by anybody • A private member can only be read, written, or called by a member function of the class

  4. Class Example 1 class Stack { public: void Push(int value); int top; int stack[10]; }; void Stack::Push(int value) { ASSERT(top < 10); stack[top++] = value; }

  5. Class Example 1 • The Stack class has two data members and one member function • All of these are public • If we have a pointer “s” to a Stack object we can access top by doing like so • s->top = 5; • If top were private we'd still be able to modify it through the member function Push as it increments the top variable and is public

  6. Class Example 2 class Stack { public: void Push(int value); bool Full(); int top; int stack[10]; }; bool Stack::Full() { return (top == 10); } void Stack::Push(int value) { ASSERT(!(this->Full())); stack[top++] = value; }

  7. Class Example 2 • You can get a pointer to the current object that has been called by using the this keyword • Note that the this is not necessary as it is implicit

  8. Class Example 3 class Stack { public: void Push(int value); bool Full(); private: int top; int stack[10]; };

  9. Class Example 3 • In this example, all data members are private • Private Members are only visible to member functions of the class • all data members are only accessible by the member functions Full() and Push(int) • It is suggested that data members remain private if possible

  10. Constructors and the "new" operator • In C one uses the malloc() function to allocate memory on the heap • In C++ we use the new Operator • C might look like this • struct Stack * s = (struct Stack *)malloc(sizeof(struct Stack)); • InitStack(s, 17); • C++ would look like this • Stack * s = new Stack(17);

  11. Constructors and the “new” operator – Example 4 class Stack { public: Stack(int sz); void Push(int value); bool Full(); private: int size; int top; int * stack; };

  12. Example 4 continued. // constructor Stack::Stack(int sz) { size = sz; top = 0; stack = new int[size]; }

  13. Example 4 – The Constructor • To specify how an object is initialized we write a constructor for it as a member function. • This member function has the same name as the class name i.e, Stack(). • Stack(int) is the constructor in this example

  14. Constructors and new Operator • The new operator automatically allocates space for the object and calls the constructor for the new object • If you declare an automatic variable then the object space is allocated on the stack and the constructor is called

  15. Final words on new and constructors • Notice the constructor does not have a return value • Always define a constructor and always initialize all data members • If you do not create a constructor one is automatically defined (not recommended)

  16. Destructors and delete operator • delete is to new as free() is to malloc() • TO destroy an object allocated with the new operator do: • delete s; • This deallocates the object but first calls the destructor. • The destructor, like the constructor as it is a member function of the Stack class and is called ~Stack()

  17. Example 5 class Stack { public: Stack(int sz); ~Stack(); void Push(int value); bool Full(); private: int size; int top; int * stack; };

  18. Example 5 Continued Stack::~Stack() { delete [] stack; } • Notice: to delete the whole allocated array the [] comes after delete and before the array variable name. • The Destructor is used to deallocate data allocated by the constructor. • It is not always necessary to define one.

  19. Pointers! • Pointers hold or store an address in memory • Pointers may point to any type • Assigning one pointer to another causes both pointers to point to the same location in memory, hence the same data or object

  20. Pointers! - Example void main() { int* x; // Allocate the pointers x and y int* y; // (but not the pointees) x = malloc(sizeof(int)); // Allocate an int pointee, // and set x to point to it *x = 42; // Dereference x to store 42 in its pointee *y = 13; // CRASH -- y does not have a pointee yet y = x; // Pointer assignment sets y to point to x's pointee *y = 13; // Dereference y to store 13 in its (shared) pointee }

  21. Pointers – Example (with “new”) void main() { int* x; // Allocate the pointers x and y int* y; // (but not the pointees) x = new int; // Allocate an int pointee, // and set x to point to it *x = 42; // Dereference x to store 42 in its pointee *y = 13; // CRASH -- y does not have a pointee yet y = x; // Pointer assignment sets y to point to x's pointee *y = 13; // Dereference y to store 13 in its (shared) pointee }

  22. Question • At the end of the above code, y is set to have a pointee and then dereferenced it store the number 13 into its pointee. After this happens, what is the value of x's pointee?

  23. Answer • The value of x's pointee is 13 because it is also y's pointee. This is what sharing is all about -- multiple pointers pointing to one pointee.

  24. Question 2 • Write some code that creates the above pointer structure in C++ using the new operator.

  25. Answer int* x; int* y; x = new int; y = new int; *x = 1; *y = 2; x = y;

More Related