Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of.

Similar presentations


Presentation on theme: "Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of."— Presentation transcript:

1 Plab 2003 Lecture 41 C++ - Classes

2 Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of shapes

3 Plab 2003 Lecture 43 Solution #1 struct Shape { enum { RECTANGLE, CIRCLE, TRIANGLE } type; doublex, y; doubleheight, width; }; void draw( Shape const* shape ) { switch( shape->type ) { case RECTANGLE: … case CIRCLE: …

4 Plab 2003 Lecture 44 Solution #1 - Discussion Pros: Simple, direct Cons: Adding new shapes requires changing all procedures that deal with shape. Require fat interface in order to support all kinds of shapes. No type checking. Unsafe!

5 Plab 2003 Lecture 45 Solution #2 Allow to implement shape specific code struct Shape { doublex, y; doubleheight, width; void(*draw)( Shape const* ); }; void draw( Shape const* shape ) { (*shape->draw)(shape); }

6 Plab 2003 Lecture 46 Solution #2 Pros: Extendable Drawing method of each shape encapsulated Efficient Cons: Require fat interface in order to support all kinds of shapes. Still no type checking. Unsafe!

7 Plab 2003 Lecture 47 Solution #3 – C++ classes C++ Language provides tools that support object oriented programming.

8 Plab 2003 Lecture 48 Simple Class Definition class Counter { public: //function prototypes Counter(); // Constructor void increment(); // A method int value() const; // Another method private: int m_count; };

9 Plab 2003 Lecture 49 Class Implementation Counter::Counter() { //fully qualified name m_count = 0; } void Counter::increment(){ m_count++; } int Counter::value(){ return m_count; }

10 Plab 2003 Lecture 410 Using the class int main() { Counter cnt; // default constructor! printf("Initial value = %d\n", cnt.value() ); cnt.increment(); printf("New value = %d\n", cnt.value() ); }

11 Plab 2003 Lecture 411 Class Basics: Access control Declare which parts of the class are accessible outside the class class Counter { public: //designate a section! … // accessible from outside private: //designate a section! … // private };

12 Plab 2003 Lecture 412 Example class Counter { public: Counter(); int value()const; void increment(); int m_flag; private: int m_counter; }; int main() { Counter c; // legal int x = c.value(); int y = c.m_flag; // illegal c.m_counter = 2.0; }

13 Plab 2003 Lecture 413 Class Basics: Constructors Initialize the class object upon construction class Counter { public: Counter(); //default constructor Counter( int c ); //2 Counter( int c, int f ); //3 }; … Counter a; // Calls default constructor Counter b(0); // Calls 2 Counter c( 1, 2); // Calls 3

14 Plab 2003 Lecture 414 Default arguments in constructors/functions In C++ Counter::Counter( int c = 0, int f = 1) { } Note that now Counter has a default constructor! Counter::Counter( int c = 0, int f) //illegal { }

15 Plab 2003 Lecture 415 Destructors Ensure propose “cleanup” when the object is destructed Use for freeing memory, notifying related objects. free resources. Files, Sockets…

16 Plab 2003 Lecture 416 Class Basics: Destructors class Counter { public: Counter(); ~Counter(); // destructor private: char* message; }; Counter::Counter(){ message = (char*)malloc(1000); } Counter::~Counter(){ free(message); } int main() { Counter a; if( … ) { Counter b; … } … }

17 Plab 2003 Lecture 417 Class – Memory Management Representation of data structure list. C++ implementation of data structure Data members of IntList are protected using appropriate access control. Usage is more natural … IntList L; L.pushFront(6) if( !L.isEmpty() ) x = L.popBack();

18 Plab 2003 Lecture 418 Class – Memory Management Cont Consider this code main() { IntList L; … } What is the difference? Compare to main() { IntList* L = (IntList*)malloc( sizeof(IntList)); … free(L) }

19 Plab 2003 Lecture 419 IntList* L = (IntList*)malloc(sizeof(IntList)); Does not call constructor! Internal data members are not initialized free(L); Does not call destructor! Internal data members are not freed Class – Memory Management Cont

20 Plab 2003 Lecture 420 new & delete - Operators Special operators: IntList* L = new IntList; –allocate memory –call constructor delete L; –call destructor –free memory

21 Plab 2003 Lecture 421 New new ; Allocate an object of type Apply constructor to the new object Return a pointer to the new object Can be used with any type: int *i = new int; char** p = new (char *);

22 Plab 2003 Lecture 422 New & Constructors class Counter { public: Counter(); Counter( int c ); Counter( int c, int f); }; … Counter* cp; cp = new Counter; // Calls cp = new Counter(1); // Calls cp = new Counter(1, 2); // Calls

23 Plab 2003 Lecture 423 New & arrays To allocate arrays, use int n = 4; int* a = new int[10]; // array of 10 ints IntList* b = new IntList[n]; // array of n IntLists Objects in allocated array must have an default constructor!

24 Plab 2003 Lecture 424 Delete & array Special operation to delete arrays int* a = new int[10]; int* b = new int[10]; … delete [] a; // proper delete command delete b; // error, undefined behavior!

25 Plab 2003 Lecture 425 Member Initialization class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };

26 Plab 2003 Lecture 426 #include "Point.h“ class Circle { public: Circle(int x, int y, double r); ~Circle(); //... private: Point m_center; //... }; Member Initialization Cont

27 Plab 2003 Lecture 427 #include "Circle.h“ Circle::Circle(int x, int y, double r) : m_center(x,y) { //... } Circle::~Circle() { printf(“in ~Circle()"); } Member Initialization Cont Won’t compile without

28 Plab 2003 Lecture 428 Member Initialization Cont The same syntax also works for primitive types Point::Point(int x, int y) : m_x(x), m_y(y) { } safer than Point::Point(int x, int y) { m_x = x; m_y = y; }

29 Plab 2003 Lecture 429 Inheritance The main ideas are similar to what you already know from Java. C++ has no interfaces but it allows multiple inheritance

30 Plab 2003 Lecture 430 #include "Point.h“ class Circle : Point { public: Circle(int x, int y, double r); ~Circle(); //... #include "Circle.h“ Circle::Circle(int x, int y, double r) : Point(x,y) { //... } Circle::~Circle() { printf(“in ~Circle()"); } Member Initialization Won’t compile without

31 Plab 2003 Lecture 431 C-tor & D-tor order of execution Constructor of the base class is the 1 st to be executed. Why? Then the members are constructed. Finally, the constructor of the class itself is executed. Destruction is done in the opposite order.

32 Plab 2003 Lecture 432 protected Class members that should be accessible by subclasses are declared protected.

33 Plab 2003 Lecture 433 “this” pointer A (constant) pointer to the instance for which the member function was invoked. Example: class List { List* next; public: bool on(List*); //... }; bool List::on(List* p) { if (p==null) return false; for (List *q = this; q; q=q->next) if (q == p) return true; return false; }

34 Plab 2003 Lecture 434 inline functions A hint to a compiler to put function’s code inline, rather than perform a regular function call. Objective: improve performance of small, frequently used functions.

35 Plab 2003 Lecture 435 inline functions Cont Example: allocateNode() function in data structures like Linked List or Binary Tree. A member function defined in class definition (i.e. the header file) is automatically considered to be inline. An inline function defined in.cpp file is not recognized in other source files.

36 Plab 2003 Lecture 436 Static class members These are class members class X { private: static int instances; public: X() { instances++; } ~X(); { instances--; } static int getInstances() const { return instances; } };

37 Plab 2003 Lecture 437 Static class members Example of using a static function: X x1; //... X x2; //... printf(“%d\n”, X::getInstances()); Somewhere (but only in one place!) the instances variable should be defined: //C++ use qualified names int X::instances = 0; In Java declaration and defintion were in the same place


Download ppt "Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of."

Similar presentations


Ads by Google