Module 13 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Module 13

Description:

Assignment operations and class members? Allowing other classes access to ... We can declare some functions to be friend functions functions on a list of ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 11
Provided by: humb
Category:
Tags: befriend | module

less

Transcript and Presenter's Notes

Title: Module 13


1
Module 13
  • Class Interactions

2
Today
  • Were interested in how classes interact with
    each other and with the rest of the C universe
  • Assignment operations and class members?
  • Allowing other classes access to private data
    members?
  • Defining your own operations?

3
Note on Declaration/Instantiation
  • When you declare an object belonging to a class,
    memory is set aside even if you havent
    instantiated the data members
  • Thus some constructor is called in these
    instances (usually default constructor)

ComplexNumber n
4
So suppose you want to change values after this
  • You may want to assign an objects data members
    the same values from another object
  • Your natural intuition says you might write
  • This is called memberwise copying
  • Your intuition is right! But where does it come
    from?

myRectangle yourRectangle
5
Gang of Three
  • When you build a class, you get these three
    different things for free (without you defining
    them)
  • An assignment operator
  • A copy constructor (allows instantiation from an
    existing object of the class)
  • A destructor method
  • Can you generate an example of using each?

6
Problem!
  • These three are grouped together because of what
    can happen when objects reference dynamic memory
    (example on board)
  • The default copy constructor makes a shallow copy
    of its data.
  • Usually this is sufficient, but you should be
    aware this is happening

7
Static Data Members
  • Remember our old friend static?
  • If we declare a data member of a class static,
    then all objects of that class are referring to
    the same memory location
  • Might be good for keeping track of how many
    Rectangles we create, for example

8
Friend Functions
  • Sometimes we want another class to have access to
    our (otherwise) private data
  • Note we dont do this often!
  • We can declare some functions to be friend
    functionsfunctions on a list of friends
    maintained inside the class
  • Such a function has access to private data
    membersbut note that it lacks the reference of a
    particular data member!

9
Redefining Operators
  • I can, if I wish, decide to create a new operator
    for my classsay something like the operator
  • An obvious one to define is the operation of the
    ltlt and gtgt operators
  • Note that we had some lab problems with strings
    related to this

10
Okay, lets build some
  • Lets try to build some of these for a class to
    represent Rational numbers
Write a Comment
User Comments (0)
About PowerShow.com