CSE 332: C++ classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.

Slides:



Advertisements
Similar presentations
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Advertisements

CSE 332: C++ copy control II Copy Control (Part II) Review: copy control consists of 5 distinct operations –A copy constructor initializes an object by.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Object-Oriented Programming in C++ More examples of Association.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
CSE 332: Memory management with C++ classes Memory Management with Classes Review: for non-static built-in (native) types –default constructor and destructor.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Overview of C++ Polymorphism
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
C++ Functions A bit of review (things we’ve covered so far)
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Chapter 2 Objects and Classes
Motivation for Generic Programming in C++
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Motivation and Overview
Andy Wang Object Oriented Programming in C++ COP 3330
Review: Two Programming Paradigms
Class: Special Topics Copy Constructors Static members Friends this
Introduction to Classes
Overview of C++ Polymorphism
SPL – PS3 C++ Classes.
Presentation transcript:

CSE 332: C++ classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters and return values Exceptions and how the call stack behaves –We have viewed data and functions as being separate for the most part Object-oriented (a.k.a. OO) programming –Allows us to package data and functions together Makes data more interesting (adds behavior) Makes functions more focused (restricts data scope) –This module will focus on OO programing Using classes/structs, member functions/variables New ideas like inheritance, polymorphism, substitution

CSE 332: C++ classes Structure of a Simple C++ Class class Date { public: // member functions, visible outside the class Date (); // default constructor Date (const Date &); // copy constructor Date (int d, int m, int y); // another constructor virtual ~Date (); // (virtual) destructor Date & operator= (const Date &); // assignment operator int d () const; int m () const; int y () const; // accessors void d (int); void m (int); void y (int); // mutators string yyyymmdd () const; // generate a formatted string private: // member variables, visible only within functions above int d_; int m_; int y_; }; The compiler defines these 4 if you don’t Notice the semicolon at the end of the class declaration operators can be member functions as well

CSE 332: C++ classes Access Control Declaring access control scopes within a class private : visible only within the class protected : also visible within derived classes (more later) public : visible everywhere –Access control in a class is private by default but, it’s better style to label access control explicitly A struct is the same as a class, except –Access control for a struct is public by default –Usually used for things that are “mostly data” E.g., if initialization and deep copy only, may suggest using a struct –Versus classes, which are expected to have both data and some form of non-trivial behavior E.g., if reference counting, etc. probably want to use a class

CSE 332: C++ classes Issues with Encapsulation in C++ Sometimes two classes are closely tied –For example, a container and its iterators –One needs direct access to the other’s internal details –But other classes shouldn’t have such direct access –Can put their declarations in the same header file –Can put their definitions in the same source file Poses interesting design forces –How should iterator access members of container? –Making container members public violates encapsulation Any class, not just iterator could modify them –Make protected, derive iterator from container? Could work: inheritance for implementation But, may prove awkward if lots of classes/dependences are involved –Could have lots of fine-grain accessors and mutators Functions to get and set value of each member variable But may reduce encapsulation, clutter the interface for other classes

CSE 332: C++ classes Friends Offer a limited way to open up class encapsulation C++ allows a class to declare its “friends” –Give access to specific classes or functions A “controlled violation of encapsulation” –Keyword friend is used in class declaration Properties of the friend relation in C++ –Friendship gives complete access Friend methods/functions behave like class members public, protected, private scopes are all accessible by friends –Friendship is asymmetric and voluntary A class gets to say what friends it has (giving permission to them) But one cannot “force friendship” on a class from outside it –Friendship is not inherited Specific friend relationships must be declared by each class “Your parents’ friends are not necessarily your friends”

CSE 332: C++ classes Friends Example class Foo { friend ostream &operator<<(ostream &out, const Foo &f); public: Foo(int) {} ~Foo() {} private: int baz; }; ostream &operator<<(ostream &out, const Foo &f) { out << f.baz; return out; } Class declares operator<< as a friend –Notice operator<< is not a member of class Foo –Gives it access to member variable baz Can now print Foo like a built-in type Foo foo; cout << foo << endl;

CSE 332: C++ classes Constructors A constructor has the same name as its class Establishes invariants for the class instances (objects) –Properties that always hold –Like, no memory leaks Notice parameters given in base/member initialization list –You must initialize const and reference members there –Members are constructed in the order they were declared List should follow that order –Set up invariants before the constructor body is run –Help avoid/fix constructor failure More on this topic later class Date { public: Date (); Date (const Date &); Date (int d, int m, int y); //... private: int d_, m_, y_; }; Date::Date () : d_(0), m_(0), y_(0) {} Date::Date (const Date &d) : d_(d.d_), m_(d.m_), y_(d.y_) {} Date::Date (int d, int m, int y) : d_(d), m_(m), y_(y) {} base / member list constructor body

CSE 332: C++ classes Default and Copy Constructors Default constructor takes no arguments –Compiler synthesizes one if no constructors are provided Does default construction of all class members (a.k.a member-wise) –If you write a default constructor Can initialize default values via base/member list Must do this for const and reference members Copy constructor takes a reference to a (usually const) instance of the class –Compiler provides one by default if you don’t Does (shallow) copy construction of all class members Default / copy construction of built-in types –Default construction does nothing (leaves uninitialized) –Copy construction fills in the value given

CSE 332: C++ classes Destructors Constructors initialize objects –At start of object’s lifetime –implicitly called when object is created (can also call explicitly) Often want to make destructors virtual –More on this when we discuss inheritance Destructors clean up afterward –Compiler provides if you don’t Does member-wise destruction –Destructor is implicitly called when an object is destroyed –Can make destructor private, call it from a public member function e.g., a public one called destroy() Only allows heap allocation class Calendar { public: Calendar (size_t s); virtual ~Calendar (); // etc... private: size_t size_; Date * dates_; }; Calendar::Calendar (size_t s) : size_(s), dates_(0) { if (size_ > 0) { dates_ = new Date[size_]; } Calendar::~Calendar () { delete [] dates_; }

CSE 332: C++ classes More on Initialization and Destruction Initialization follows a well defined order –Base class constructor is called That constructor recursively follows this order, too –Member constructors are called In order members were declared Good style to list in that order (a good compiler may warn if not) –Constructor body is run Destruction occurs in the reverse order –Destructor body is run –Member destructors are called –Base class destructor is called That destructor recursively follows this order, too More on this when we cover inheritance

CSE 332: C++ classes Tips for Initialization and Destruction Use base/member initialization list if you can –To set pointers to zero, etc. before body is run –To initialize safe things not requiring a loop, etc. Do things that can fail in the constructor body –Rather than in the initialization list –For example, memory allocation, etc. Watch out for what can happen if an exception can leave a constructor –No guarantee destructor will be called in that case –Need to avoid having a partially initialized (“zombie”) object –Two kinds of approaches can be used (1 st is often preferred) Keep the object in a safe default state at all times so it doesn’t matter Use try/catch within the constructor and reset object to safe state

CSE 332: C++ classes Self Reference All non-static member functions are invoked with an implicit pointer to object on which function was called –non-const member function: X * const this –const member function: const X * const this Use of the “this” pointer is also implied when member variables are accessed from within a member function If you are passed a pointer or reference as a parameter –May want (or need) to compare against this (see assignment, next) Use this to return a reference to the object on which a member function (or operator) was called –e.g., cout << i << j; or i = j = k; Date & Date::add_year(int n) { if (…) {…}// check for leap year _y += n; // this-> implied return *this; } // allows Date d; d.add_year(3).add_year(5);

CSE 332: C++ classes Assignment Operator Compiler supplies if you don’t –Does member-wise assignment Similar to copy constructor –But must deal with the existing values of object’s members –And, no initialization list Watch out for self-reference –Assignment of an object to itself s = s; // perfectly legal syntax Efficiency, correctness issues Watch out for correct aliasing and copying cost trade-offs –Copying an int vs. an int * vs. an int & vs. an int [] –Can leverage copy constructor to ensure safe reallocation –More on these issues throughout the semester class Date { public: Date & operator= (const Date &); //... private: int d_, m_, y_; }; Date & Date::operator= (const Date &d){ d_ = d.d_; m_ = d.m_; y_ = d.y_; return *this; } int main (int, char *[]) { Date a; // default constructor Date b(a); // copy constructor Date c = b; // copy constructor a = c; // assignment operator }

CSE 332: C++ classes New C++11 Features Old (C++03) way to prevent compiler from generating a default constructor, copy constructor, destructor, or assignment operator was somewhat awkward –Declare private, don’t define, don’t use within class –This works but if used later may give a cryptic linker error New (C++11) way to prevent calls to any method –End the declaration with = delete (and don’t define) –Compiler will then give an error if a call is made C++11 allows a constructor to call peer constructors –Allows re-use of implementation (through delegation) –Object is fully constructed once any constructor finishes C++11 lets you ask compiler for a default constructor –Even if you have declared another constructor –End the declaration with = default (and don’t define)

CSE 332: C++ classes Accessor and Mutator Functions class Date { public: //... int d () const; int m () const; int y () const; void d (int); void m (int); void y (int); //... private: int d_, m_, y_; }; May want to give limited access to some of the private class members Accessors (a.k.a. “get” methods) –Member functions to “read” values –Return by value or by const reference –Functions often const (see next slide) Mutators (a.k.a. “set” methods) –Member functions to “write” values –Return type is often void –Parameter type Same as member variable type Const reference to member variable type Often see one of two main styles int d () const; void d (int); int get_d () const; void set_d (int);

CSE 332: C++ classes Const Member Functions class Date { public: //... int d () const; int m () const; int y () const; string yyyymmdd () const; //... private: int d_, m_, y_; }; int main (int, char *[]) { const Date c (4, 10, 2005); cout << c.yyyymmdd () << endl; return 0; } Can declare member functions as being “const” –Promises not to modify member variables when called Const member functions can be called on both const and non-const objects –Can’t call non-const member functions on const objects Const rule doesn’t apply to constructors or destructor –Const objects are still created and destroyed –Just can’t be modified between creation and destruction

CSE 332: C++ classes Static Member Variables and Functions A static member is part of class but not of any particular object (a single instance of each static member per class) Declaration –Use static keyword before variable or function in.h file (not in.cc file) Static class variables live in the global memory segment Access –Non-static member functions can access static member functions and static member variables –Static member functions cannot access non-static member functions or non-static member variables (no this pointer is passed to them) –From outside the class’ methods, a static variable or function is accessed using the class name and the :: (scope) operator E.g., Singleton::instance()->print(); –From inside class methods, can either just use the variable or function name or use the scope operator version that’s used outside the class

CSE 332: C++ classes Static Class Members Must define static members, usually outside of class –Initialized before any functions in same compilation unit are called Static member functions don’t get implicit this parameter –Can’t see non-static class members –But non-static member functions can see static members class Date { public: //... static void set_default(int,int,int); private: int _d, _m, _y; static Date default_date; }; Date::Date () : _d (default_date._d), _m (default_date._m), _y (default_data._y) {} Date::operator= (const Date &d){ this->d_ = d.d_; this->m_ = d.m_; this->y_ = d.y_; } Date Date::default_date(1, 1, 2004); void Date::set_default(int m, int d, int y) { Date::default_date = Date(m, d, y); }

CSE 332: C++ classes Initializing Static Member Variables Static integral constants may be initialized within a class declaration by a constant expression class Foo {... static const int MAX = 100;... }; –you must then also (only) define it outside of class const int Foo::MAX; // don’t reinitialize More common to just declare static member in the class class Date {... static Date default_date;... }; –and then initialize it at its definition outside the class Date Date::default_date(1,1,2004);

CSE 332: C++ classes A Quick Review What four member functions/operators does the compiler automatically define for you? When/how are they invoked? What if you don’t want one of them to be used?