Presentation is loading. Please wait.

Presentation is loading. Please wait.

. Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };

Similar presentations


Presentation on theme: ". Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };"— Presentation transcript:

1 . Plab – Tirgul 6 Classes

2 Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };

3 #include "Point.h“ class Circle { public: Circle(int x, int y, double r); ~Circle(); //... private: Point m_center; //... }; Circle.h

4 #include "Circle.h“ Circle::Circle(int x, int y, double r) : m_center(x,y) { //... } Circle::~Circle() { printf(“in ~Circle()"); } Circle.cpp Won’t compile without

5 The same syntax also works for base types. Point::Point(int x, int y) : m_x(x), m_y(y) { } is better than Point::Point(int x, int y) { m_x = x; m_y = y; }

6 Inheritance u The main ideas are similar to what you already know from Java. u C++ has no interfaces but it allows multiple inheritance u For now we will not discuss the problems that arise: Class A Class CClass B Class D

7 #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()"); } private: //... }; Circle class - again Won’t compile without

8 Default arguments in constructors In C++ Point::Point(int x = 0, int y = 0) { //... } is equivalent to Java’s Point(int x, int y) {...} Point(int x) { this(x,0); } Point() {this(0,0);} Note that now Point has a default constructor! Nothing similar in C++

9 C-tor & D-tor order of execution u Constructor of the base class is the 1 st to be executed. u Then the members are constructed. u Finally, the constructor of the class itself is executed. u Destruction is done in the opposite order.

10 protected u Class members that should be accessible by subclasses are declared protected.  Example: to allow Circle access coordinates of Point we would define: class Point { public: //... protected: int m_x, m_y; };

11 this u A (constant) pointer to the instance for which the member function was invoked. u 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; }

12 inline functions u A hint to a compiler to put function’s code inline, rather than perform a regular function call. u Objective: improve performance of small, frequently used functions.  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. u An inline function defined in.cpp file is not recognized in other source files.

13 Static class members u As in Java, these are members belonging to the class rather than to an instance. class X { private: static int instances; public: X() { instances++; } ~X(); { instances--; } static int getInstances() { return instances; } }; X.h

14 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: int X::instances = 0; In Java declaration and defintion were in the same place


Download ppt ". Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };"

Similar presentations


Ads by Google