L9_classes - Classes Member functions Classes Methods...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Classes: Methods, Constructors, Destructors and Assignment For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Classes: Member functions // classes example #include <iostream> using namespace std; class Square { int x; public: int area () {return (x*x);}; void set_values(int a); int get_sidelen(void) const; }; void Square::set_values(int a){ x = a; }; int Square::get_sidelen(void) const { return x;} int main () { Square s; s.set_values (3); cout << "area: " << s.area(); return 0; } Member functions: Methods | private members of a class are accessible only from within other members of the same class or from their friends . | protected members are accessible from members of their same class and from their friends, but also from members of their derived classes. | Finally, public members are accessible from anywhere where the object is visible. Objects : Reminder | An object is an instance of a class. | Memory is allocated for each object instantiated (and not for the class). z Example: Square S; // S is an object of Square class. (on stack) Square *pS = new Square; // on heap. | An object of a class can be defined in the same way as an internal type. Objects : Reminder | Multiple objects of a class can be created (as many as you want). | All the objects share the same copy of member functions. | But, they maintain a separate copy of data members. z Square s1,s2; // each has separate copy of x Objects: Reminder | The data members and member functions of an object have the same properties as the data members and member functions of its class.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon