CIS-255 Home http://www.c-jump.com/bcc/c255c/c255syllabus.htm

Encapsulation


  1. Encapsulation
  2. Recall a struct with a function:
  3. Creating your own object
  4. Separating declarations and definitions
  5. Scopes
  6. Other things that can go into class scopes
  7. The trouble with scopes
  8. Static data members
  9. Static member functions
  10. Calling static member functions
  11. Overloading functions
  12. Resolving overloaded function calls
  13. Access control
  14. Access control declarations
  15. Access control rules (simple version)
  16. Access control examples
  17. More access control examples
  18. struct vs. class
  19. OOP Philosophy Notes
  20. OOP Encapsulation Notes
  21. friends make exceptions from access control rules
  22. Friendship rules

Encapsulation


Recall a struct with a function:



struct Point {
    int x; // member variables
    int y;
    void adjust( int x_, int y_ ) // member function
    {
        x += x_;
        y += y_;
    }
};

void main()
{
    Point p;          // instantiate object
    p.x = p.y = 4;    // access member variables
    p.adjust( 1, 1 ); // call member function
}

Creating your own object



struct Point
{
    int x; // member variables
    int y;
    void adjust( int x_, int y_ ); // member functions
    void draw( Plane* plane_ );
};

Separating declarations and definitions



////////////////////////////////////////////////////////
// point.h
struct Point
{
    int x; // member variables
    int y;
    void adjust( int x_, int y_ ); // member declaration
};

////////////////////////////////////////////////////////
// point.cpp
#include "point.h"
void Point::adjust( int x_, int y_ ) // member definition
{
    x += x_; // Note: access to member variables is easy
    y += y_;
}

Scopes


Other things that can go into class scopes


The trouble with scopes


Static data members


Static member functions



// point.h
class Point
{
    static int m_x_origin;
    static int m_y_origin;
    static void set_origin( int x_, int y_ );
    //...
};

// point.cpp
void Point::set_origin( int x_, int y_ )
{
    m_x_origin = x_;
    m_y_origin = y_;
}

Calling static member functions



#include "point.h"

void main()
{
    // Correct, but confusing:
    // suggests that p is somehow involved:
    Point p;
    p.set_origin( 0, 0 );

    // Better:
    Point::set_origin( 0, 0 );
}

Overloading functions


Resolving overloaded function calls


Access control


Access control declarations


Access control rules (simple version)*



   *) protected access will be introduced when we cover inheritance.

Access control example



// point.h
class Point
{
private:
    int m_x;
    int m_y;
    void reset_coord();
public:
    double distance_to_origin();
};


// main.cpp
void main() // free function
{
    Point p;
    p.m_x = 4;       // Error, m_x is private
    p.reset_coord(); // Error, reset_coordinates() is private
    double d = p.distance_to_origin(); // Ok, public
}


// point.cpp
double Point::distance_to_origin()
{
    // Access to private members m_x, m_y is allowed,
    // because we are in a member function:
    return sqrt( ( double ) m_x * m_x + m_y * m_y );
}

More access control examples



// point.h
class Point
{
private:
    int m_x;
    int m_y;
    void reset_coord();
public:
    double distance_to_origin();
    double distance_to_point();
};


// point.cpp
double Point::distance_to_point( Point other )
{
    // Access to private members m_x, m_y is allowed,
    // because we are in a member function:
    double x_diff = m_x - other.m_x;
    double y_diff = m_y - other.m_y;
    return sqrt( x_diff * x_diff + y_diff * y_diff );
}

struct vs. class


OOP Philosophy Notes


C++ Encapsulation Notes


Friends make exceptions from access control rules


Friendship rules