110 likes | 133 Views
Object-Based Programming. Mostly Review. Objects Review. what is object? class? member variables? member functions? public members? private members? friend function?. Object-Based Programming Terminology.
E N D
Object-Based Programming Mostly Review
Objects Review • what is object? class? • member variables? member functions? • public members? private members? • friend function?
Object-Based Programming Terminology • object-based programming – using objects in programming but no inheritance (extended/derived classes) • object-oriented programming – this plus inheritance • features – member variables and member functions • class attributes (UML term)/fields - member variables • class operators (UML)/methods - member functions • (class) interface – public class features • (class) implementation – private class features • accessor (inspector) member function – does not change the state of the object • mutator (modifier) member function – changes the state of the object
Accessors • member functions that do not modify the state of the object • signature should contain const int getNumber() const{ return number_; // returns value of private attribtue } • reasons for const • visually designates accessor • generates compile-time error if modifies object • if invokes another method, it has to be constant – const-ness cannot be lost through function invocation • only accessors can be invoked on constant objects such as const Date myBirthDate(1,14,2014);
Declarations vs. Definitions C++ syntax rule: a construct may be declared multiple times (provided signatures match), defined only once • variable/constant • declaration is preceded with extern useful for global variable/constant extern intmyGlobalVar; extern constintmyGlobalConst; • only single definition, name located by linker • function • declaration: prototype, multiple prototypes okay • definition: implementation, only single implementation • class • (forward) declaration (incomplete type), multiple times allowed, but can be used only to declare pointers/references class myClass; • definition – only once class myClass{};
Copying Objects • what are objects with dynamically allocated members? • why are they special? • why are copy constructor, destructor, overloaded assignment called big three? • what is this ? • shallow copy – memberwise copying of object attributes • may be inadequate in case object has dynamically allocated members and • passing of object by reference • returning an object • object assignment • deep copy – recursive (including attributes) copying of entire object
Constructor • method invoked automatically at object creation to initialize it • same name as class • can be overloaded and can use default parameters • default/no-arg constructor – either has no parameters or has default values for all existing parameters either • explicitly defined by programmer • generated automatically • does not initialize attributes of basic types, pointers or references • invokes default constructors on all non-static attributes of class types
Destructor • method automatically invoked when object goes out of scope to deallocate resources used by object • same name as class with tilde • cannot be overloaded, cannot have parameters, cannot be const • used to release dynamic memory used by object • note: need to call destructors on dynamically allocated objects pointed to by the objects going out of scope
Copy Constructor • invoked when initialing a new object using an existing object • when passing object by value as a parameter • when returning object • used to implement deep copy • signature: ClassName(const ClassName&);
Overloaded Assignment • invoked when one object is assigned to anther • used to implement deep copy, of right-hand-side object and preserve meaning of assignment operator • need to deallocate old object • need to create a copy of the new object • copy and swap idiom – common idiom to implement overloaded assignment: terser, re-uses copy-constructor need to define a friend swap function myClass& operator=(myClassrhs){ swap(*this, rhs); return(*this); } • invokes copy-constructor to create the copy of the right-hand-side object • implicitly uses destructor to deallocate old object • no need to check for self-assignment
Member Initialization List terse initialization of member variables, allows to initialize const and reference members • comma-separated list of initalizers of the form variableName(value) • appears in constructor definition between function head and function body following colon • primitive types: equivalent to assignment • objects: constructor is invoked class Student{ public: Student(long, string); private: long number_; string name_; }; Student::Student (long number, string name) : number_(number), name_(name) //initialization list {} // empty body • caution, initalizers invoked in order members are listed in class definition