Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:

Similar presentations


Presentation on theme: "OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:"— Presentation transcript:

1 OOP Details Constructors, copies, access

2 Initialize Fields are not initialized by default:

3 Initialize C++11 allows initialization of members

4 Constructor Constructor : special function that initializes an object – No return type – Function name is same as class name – Job: initialize ALL member variables

5 Default Constructor Default Constructor : has no arguments – Called when you create an object like: – Can NOT be called with parens!!!

6 Customizing Default Constructor Default constructor provided, but useless – Does not initialize anything Define it yourself to initialize member variables:

7 Parameterized Constructors Constructors can have parameters: Use parens after object name to pass:

8 Overloaded Constructors can be overloaded – Selected based on provided parameters

9 Gotcha If you define constructors… Default constructor does not exist:

10 Rules of Thumb Always write constructors – At least default Each constructor should initialize every member variable

11 Modern Construction C++11 added member initialization in declaration: Old C++New C++

12 Why is this bad?

13

14 Avoid Redundancy If you are representing the same data in more than one place you may be doing it wrong… – Favor calculating over storing

15 Encapsulation & Information Hiding Two big ideas in OOP – Encapsulation : Grouping behaviors with data – Information Hiding : Limiting access to (messy) details of an object

16 Why Hide Details Abstraction is the only way to understand complex systems Want leak proof abstractions – How does clock represent the time internally? Who cares!

17 Private vs Public private & public : access specifiers – public: accessible anywhere – private: only accessible inside the class

18 Private vs Public Compile error to access private data:

19 Private vs Public Rule of thumb: – State (variables) private – Behaviors public

20 Accessing Private Data Accessors return value of private data:

21 Accessing Private Data Mutators allow modification of private data:

22 = Copies Members Assignment operator copies members c1 radius: 5 X: 0 Y: 0 c2 radius: 5 X: 0 Y: 0

23 = Copies Members Objects are distinct after that point: c1 radius: 5 X: 2 Y: 3 c2 radius: 5 X: 0 Y: 0

24 Other Operators = and. are only operators that work on objects automatically – No ==, etc… Can overload others…later

25 Anonymous Objects Constructor call on Classname() produces anonymous object: ???? radius: 5 X: 0 Y: 0

26 Anonymous Objects Constructor call on Classname() produces anonymous object: ???? radius: 5 X: 0 Y: 0


Download ppt "OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:"

Similar presentations


Ads by Google