In this tutorial of our Object Orientation Course in C++, we will learn how to use pointers with objects, and see a new way to allocate memory.
Object Pointers
Initially, we will create a class, called Student, which will have two variables (math and english), which will store the student's grades. There will also be a get and set, and a getAverage() call to calculate the average for each student.
Our class, all complete and cute looks like this:
class Student { private: double math, english; public: double getAverage() const; void setMath(double); void setEnglish(double); double getMath() const; double getEnglish() const; }; double Student::getAverage() const { return (getMath() + getEnglish())/2; } void Student::setMath(double m) { math = m; } void Student::setEnglish(double e) { english = e; } double Student::getMath() const { return math; } double Student::getEnglish() const { return english; }
We will now declare a pointer named 'ptr' of type Student:
- Student *ptrStudent
Now let's actually declare an object of the Student class:
- Student s1;
We can associate the pointer with the object as follows:
- ptrStudent = &s1
(don't forget &, remember that pointers store memory addresses, and by placing & before a variable, it gives the address of that variable)
Working with Pointers with Classes and Objects: ->
- ptrStudent-> setMath (8);
- ptrStudent-> setEnglish (10);
- ptrStudent-> getAverage()
#include <iostream> using namespace std; class Student { private: double math, english; public: double getAverage() const; void setMath(double); void setEnglish(double); double getMath() const; double getEnglish() const; }; double Student::getAverage() const { return (getMath() + getEnglish())/2; } void Student::setMath(double m) { math = m; } void Student::setEnglish(double e) { english = e; } double Student::getMath() const { return math; } double Student::getEnglish() const { return english; } int main() { Student *ptrStudent; Student s1; ptrStudent = &s1; ptrStudent->setMath(8); ptrStudent->setEnglish(10); cout << ptrStudent->getAverage()<<endl; return 0; }
Allocating space for objects: new operator
- MyClass *ptr;
- ptr = new MyClass
Deleting objects - delete
- delete ptr;
#include <iostream> using namespace std; class Student { private: double math, english; public: double getAverage() const; void setMath(double); void setEnglish(double); double getMath() const; double getEnglish() const; }; double Student::getAverage() const { return (getMath() + getEnglish())/2; } void Student::setMath(double m) { math = m; } void Student::setEnglish(double e) { english = e; } double Student::getMath() const { return math; } double Student::getEnglish() const { return english; } int main() { Student *ptrStudent; ptrStudent = new Student; ptrStudent->setMath(8); ptrStudent->setEnglish(10); cout << "Studant average 1: " <<ptrStudent->getAverage()<<endl; ptrStudent->setMath(6); ptrStudent->setEnglish(7); cout << "Studant average 2: " <<ptrStudent->getAverage()<<endl; delete ptrStudent; return 0; }
No comments:
Post a Comment