L9_classes - Classes: Member functions Classes: Methods,...

Info iconThis 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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Assignment operator | Square s3 = s2; | By default, copying a class object is equivalent to copying all its elements including pointers. Variable assignment | Values are assigned to variables and not to their data types. | Hence, you assign values to members of an object and not a class. | Must create a unique object of a class because you cannot assign values to a class. z Square = 5 is meaningless… Back to member functions | The member functions in the public section of a class can be accessed using the “.” operator for instantiated objects. (For pointers its -> ) z Square s1, *s2; z s1.set_values(5); z s2 = new Square; s2->set_values(10); z delete s2; Member functions | Only the public members of an object can be directly accessed. Special Member functions | Constructors : Are invoked to initialize the data members of a class. | Can not return any value (not even void). | Can accept any parameters as needed. z What would happen if we called the member function area() before having called function set_values()?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 7

L9_classes - Classes: Member functions Classes: Methods,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online