Presentation is loading. Please wait.

Presentation is loading. Please wait.

Summary: Abstract Data Type

Similar presentations


Presentation on theme: "Summary: Abstract Data Type"— Presentation transcript:

1 Summary: Abstract Data Type
class A { public: A(); int f(); private: int x; } A::A() { x=0; int A::f() { return x; int main() { A a; cout << a.f(); // not a.x!!! A b(a); A c; c = a; // member-wise copy } Objects are calling member funtions (public interface) Member functions are using member variables (private data) Objects are not directly dependent on implementation (private data)

2 Static variables (objects) Dynamic variables (objects)
A (direct) named memory location A static part (pointer) + (indirect) nameless memory location (dynamic part) int a; a = 20; int* pa; pa = new int; *pa = 20; 20 20 a pa static dynamic static

3 At least one pointer variable (I.e. dynamic variable)
classes At least one pointer variable (I.e. dynamic variable) Only static member variables class B { public: B(); B(const B& b); ~B(); private: int* px; } class A { public: A(); private: int x; } (Must) a defaut value constructor A() for ‘copy constructor’ automatically privided No destructor ‘assignment’ automatically provided (Must) a defaut value constructor for (Must) redefine ‘copy constructor’ (Must) the destructor (Must) redefine ‘assignment’ (do later) Creation by new + initialiation initialisation The big three!

4 Dynamic classes: class B { public: B(sometype); // constructors
B(const B& b); // copy constructor B& operator=(const B&); // copy assignment ~B(); // destructor private: int* px; }

5 B::B() { px = new int; *px = 0; } B::B(const B& b) { (*px) = *(b.px); B::~B() { delete px; class B { public: B(); B(const B& b); ~B(); private: int* px; }

6 Automatic behavior of constructors/destructor
All constructors/destructor have automatic behavior, they are called ‘implicitly’!!! (that’s why they have standard names!) Constructors are called when creating variables and other occasions: A a; // implicitly call the default value constructor A::A(); A b(a); // implicitly call the copy constructor A::A(const A& a); Destructors are called when the variables go out of scope void somefunction() { B b; };


Download ppt "Summary: Abstract Data Type"

Similar presentations


Ads by Google