250 likes | 268 Views
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
E N D
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
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)
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
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; }
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
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; }
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
Class Example 3 class Stack { public: void Push(int value); bool Full(); private: int top; int stack[10]; };
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
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);
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; };
Example 4 continued. // constructor Stack::Stack(int sz) { size = sz; top = 0; stack = new int[size]; }
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
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
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)
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()
Example 5 class Stack { public: Stack(int sz); ~Stack(); void Push(int value); bool Full(); private: int size; int top; int * stack; };
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.
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
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 }
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 }
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?
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.
Question 2 • Write some code that creates the above pointer structure in C++ using the new operator.
Answer int* x; int* y; x = new int; y = new int; *x = 1; *y = 2; x = y;