1 / 27

User-Defined Classes

User-Defined Classes. Class Definition. int, float, char are built into C++ Declare and use int x = 5; Create user defined data types Extensive use throughout remainder of course Counter class will introduce the concept counter.h counter class definitions Define the class in the .h file.

maddy
Download Presentation

User-Defined Classes

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. User-Defined Classes

  2. Class Definition • int, float, char are built into C++ • Declare and use • int x = 5; • Create user defined data types • Extensive use throughout remainder of course • Counter class will introduce the concept • counter.h counter class definitions • Define the class in the .h file

  3. User Defined Types • Define what data we want in the class • data elements of the class are private • Create the functionality to operate on the data • class member functions access data • We have created a new data type not known to the compiler

  4. Comparing struct and classes • struct & class define a data type • Collection of related elements • Prototype declarations • Three levels of access control • Public • Private • Protected • Major difference default access opposite • class private - struct public

  5. Syntax Form: class class_name { public: List all public data and functions private: List all private data and functions };

  6. Counter.h // FILE: Counter.h // COUNTER CLASS DEFINITION class counter { public: counter (); counter (int m_v);

  7. Counter.h // SET COUNTER VALUE void setcount (int val); // INCREMENT COUNTER void increment (); // DECREMENT COUNTER void decrement (); // RETURN CURRENT COUNTER VALUE int getCount ();

  8. Counter.h // RETURN MAXIMUM COUNTER VALUE int get_maxvalue (); private: // Data Members (Attributes)... int count; int max_value; };

  9. Class Implementation • Counter.cpp implementation details • Hidden from users (details) • Scope resolution operator • :: prefix for each member function • Informs the compiler that the function is a member of the class • Class member functions can access all class data members • Avoid using member functions inside member functions

  10. Constructors • Two special member functions • Same name as class name • Constructor executes each time an object of type counter is declared • counter mike; • Initializes object • Types • Default • Class

  11. Constructors • Default Constructor • Used when no arguments passed as part of the declaration • Class Constructor • Used when arguments are passed as part of the declaration • Constructors do NOT specify a return type

  12. Member Functions • Member functions that modify data are • set_value • increment • decrement • Member functions that retrieve data are • get_value • get_maxvalue

  13. Counter.cpp // FILE: Counter.cpp // COUNTER CLASS DEFINITION #include "Counter.h" #include <iostream.h> #include <limits.h> // DEFAULT CONSTRUCTOR counter :: counter () { value = 0; max_value = INT_MAX; }

  14. Counter.cpp // CONSTRUCTOR WITH ARGUMENT counter :: counter (int m_v) { count = 0; max_value = m_v; }

  15. Counter.cpp // SET COUNTER VALUE void counter::setCount (int val) { if (val >= 0 && val <= max_value) count = val; else cout << "New value is out of range. Value not changed." << endl; }

  16. Counter.cpp // INCREMENT COUNTER void counter::increment () { if (count < max_value) count++; else cout << "Counter overflow. Increment ignored." << endl; }

  17. Counter.cpp // DECREMENT COUNTER void counter::decrement () { if (count > 0) count--; else cout << "Counter underflow. Decrement ignored." << endl; } // end decrement

  18. Counter.cpp // RETURN CURRENT COUNTER VALUE int counter::getcount () { return count; } // end getvalue // RETURN MAXIMUM COUNTER VALUE int counter::get_maxvalue () { return max_value; } // end get_maxvalue

  19. Using the counter Class • Driver program CntrTest.cpp will help us see how the Counter Class is used • Must #include “Counter.cpp” because our class definitions are contained in it.

  20. Use of Classes and Objects • Class Instance • counter c1; • Creates an instance of the counter class (object) • Default constructor invoked • Allocates space in memory for object • Similar to other data type declarations • int value; • Space in memory allocated for a data type int

  21. Private vs Public • Public member functions allow users to operate on the counter object c_1 • May have private member functions • If member functions need to call another function not part of the class it should be private • Use care when defining public access • Users of a class are clients • Class sometimes referred to asthe server

  22. CounterTest.cpp // FILE: CounterTest.cpp // TEST PROGRAM FOR Counter CLASS #include <iostream.h> #include "Counter.cpp" int main () { // Local data ... counter c1; counter c2 (10);

  23. CounterTest.cpp c1.set_value (50); c1.decrement (); c1.decrement (); c1.increment (); cout << "Final value of c_1 is " << c1.getCount () << endl; c2.increment (); c2.increment (); c2.decrement (); cout << "Final value of c_2 is " << c2.getCount () << endl; return 0; }

  24. Function Overloading & Polymorphism • Functions with the same name is called function overloading • Polymorphism is what allows functions with the same name to do different things based on its arguments

  25. Destructors • Member function automatically called when an object is destroyed • Destructor name is ~classname, e.g., ~Square • Has no return type; takes no arguments • Only 1 destructor per class, i.e., it cannot be overloaded • If constructor allocates dynamic memory, destructor will release memory

  26. Arrays of Objects • Objects can be the elements of an array: Square lottaSquares[10]; • Default constructor for object is used when array is defined • Must use initializer list to invoke constructor that takes arguments: Square triSqu[3] = {5,7,11}; • More complex initialization if constructor takes > 1 argument

  27. Accessing Array Objects • Objects in an array are referenced using subscripts • Member functions are referenced using dot notation: lottaSquares[3].setSide(6); cout << triSqu[i].getSide;

More Related