12
Object-Based Programming Mostly Review

Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Embed Size (px)

Citation preview

Page 1: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Object-Based Programming

Mostly Review

Page 2: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Objects Review• what is object? class?

• member variables? member functions?

• public members? private members?

• friend function?

2

Page 3: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

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

3

Page 4: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Accessors• member functions that do not modify the state of the object• signature should contains 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 asconst myBirthDate(1,14,2014);

4

Page 5: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Declarations vs. DefinitionsC++ syntax rule: a construct may declared multiple times (provided signatures

match), defined only once– variable

• declaration is preceded with extern useful for global variable declaration in header file

extern int myGlobalVar; • only single definition

– 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{};

5

Page 6: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Style Guide

• start class name with a capital letter

• name class according to its use

• list class access modifiers in this order: public, protected, private, i.e. interface first, implementation last

• use underscore_ to end names of private attributes – distinguishes them from local variables in methods

int x_;• define every accessor (query) with const• header file should include declarations only: class (?),

standalone functions, constants

6

Page 7: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Copying Objects

• what are objects with dynamically allocated members?– why are they special? – why are constructor,destructor,copy constructor 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

7

Page 8: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

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

8

Page 9: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

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

9

Page 10: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

Copy Constructor

• invoked when initialing a new object using an existing object– when passing object by vaue as a parameter– when returning object

• used to implement deep copy• signature: ClassName(const ClassName&);

10

Page 11: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

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=(myClass rhs){

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

11

Page 12: Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend

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

12