Encapsulation Value of Classes and Objects Defining Classes: Data Encapsulation Procedural Encapsulation
Encapsulation Value of Classes and Objects Defining Classes: Data Encapsulation Procedural Encapsulation
excellent practice
Function
Function
Member Function
Member Function
Member Function
Hide data from code that is outside the object Only member fns can directly access and alter object data
Object Data members
Client Code
Programmer
class
class
Classes reflect concepts Objects reflect instances that embody those concepts. object
class
girl
Jodie
Daria
Jane
Brittany
#include <string>
string cityName;
instance class
#include <string>
string cityName;
#include <string>
string cityName; cityName = "Ann Arbor";
#include <string>
string cityName; cityName = "Ann Arbor";
member function
#include <iostream>
int val; cin >> val;
class
#include <fstream>
ifstream ins;
instance
ins.open("data.txt");
class
#include <fstream>
ifstream ins;
instance
ins.open("data.txt");
member function
Class implementation details are hidden from the clients view. This is called information hiding.
Public functions of a class provide the interface between the client code and the class objects.
client code
specification
implementation
Data
Interface
Interface
//defaults public
class Rectangle int main { { public: Rectangle rect; double width; rect.width = 5; double length; rect.length = 10; }; }
compile error
class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double calcArea() const; };
class Rectangle { public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double calcArea() const; private: double width; double length; };
class Rectangle int main { { public: Rectangle rect; void setWidth(double); rect.setWidth(5); void setLength(double);
double getWidth() const; double getLength() const; double calcArea() const;
rect.setLength(10);
class Rectangle int main { { public: Rectangle rect; void setWidth(double); rect.setWidth(5); void setLength(double);
double getWidth() const; double getLength() const; double calcArea() const;
rect.setLength(10);
setWidth setLength
setWidth setLength
rect
width:
length:
5 10
rect
width:
length:
5 10
rect2
width:
length:
7 42
int main { Rectangle rect; rect.setWidth(5); rect.setLength(10); Rectangle rect2 rect2.setWidth(7); rect2.setLength(42); }
//setWidth assigns its argument to the //private member width void Rectangle::setWidth(double w) { if (w >= 0) rect width = w; else width: 5 width = 0; length: 10 }
//setLength assigns its argument to the //private member length void Rectangle::setLength(double len) { if (len >= 0) length = len; rect else length = 0; width: 5 } length: 10
//getWidth returns the value within // width double Rectangle::getWidth() const { return width; }
//getLength returns the value within // length double Rectangle::getLength() const { return length; }
//calcArea() calculates the area of the // rectangle double Rectangle::calcArea() const { return width * length; }
box
width length
cout << "Enter: width and length "; cin >> tempWidth >> tempLength; box.setWidth(tempWidth); box.setLength(tempLength);
tempWidth
cout << "Here's the area: "; cout << box.getArea() << endl;
return 0; }
tempLength
box
width length
cout << "Enter: width and length "; cin >> tempWidth >> tempLength; box.setWidth(tempWidth); box.setLength(tempLength);
tempWidth
10
cout << "Here's the area: "; cout << box.getArea() << endl;
return 0; }
tempLength
5
box
width
10
length 5
cout << "Enter: width and length "; cin >> tempWidth >> tempLength; box.setWidth(tempWidth); box.setLength(tempLength);
tempWidth
10
cout << "Here's the area: "; cout << box.getArea() << endl;
return 0; }
tempLength
5
box width
10
length 5
cout << "Enter: width and length "; cin >> tempWidth >> tempLength; box.setWidth(tempWidth); box.setLength(tempLength);
tempWidth
10
cout << "Here's the area: "; cout << box.getArea() << endl;
return 0; }
tempLength
5
kitchen
int main() length { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width bedroom = getData("bedroom"); den = getData("den"); length cout << "total area is: "; den cout << kitchen.getArea() + width bedroom.getArea() + length bedroom.getArea() << endl; return 0; }
width
kitchen
int main() length { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width bedroom = getData("bedroom"); den = getData("den"); length cout << "total area is: "; den cout << kitchen.getArea() + width bedroom.getArea() + length bedroom.getArea() << endl; return 0; }
width
Rectangle getData(string str) { double tempWidth, tempLength; cout << "Enter width for " << str << ": "; cin >> tempWidth;
str kitchen
cout << "Enter length for " tempWidth 5 << str << ": "; cin >> tempLength; tempLength 10 Rectangle temp; temp.setWidth(tempWidth); temp.setLength(tempLength); return temp; }
Rectangle getData(string str) { double tempWidth, tempLength; cout << "Enter width for " << str << ": "; cin >> tempWidth;
str kitchen
cout << "Enter length for " tempWidth 5 << str << ": "; cin >> tempLength; tempLength 10 Rectangle temp; temp.setWidth(tempWidth); temp.setLength(tempLength); return temp; }
temp
width
length 10
Rectangle getData(string str) { double tempWidth, tempLength; cout << "Enter width for " << str << ": "; cin >> tempWidth;
str kitchen
cout << "Enter length for " tempWidth 5 << str << ": "; cin >> tempLength; tempLength 10 Rectangle temp; temp.setWidth(tempWidth); temp.setLength(tempLength); return temp; }
temp
width
length 10
kitchen
int main() length 10 { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width bedroom = getData("bedroom"); den = getData("den"); length cout << "total area is: "; den cout << kitchen.getArea() + width bedroom.getArea() + length bedroom.getArea() << endl; return 0; }
width
kitchen
int main() length 10 { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width bedroom = getData("bedroom"); den = getData("den"); length cout << "total area is: "; den cout << kitchen.getArea() + width bedroom.getArea() + length bedroom.getArea() << endl; return 0; }
width
Rectangle getData(string str) { double tempWidth, tempLength; cout << "Enter width for " << str << ": "; cin >> tempWidth; cout << "Enter length for " << str << ": "; cin >> tempLength; Rectangle temp; temp.setWidth(tempWidth); temp.setLength(tempLength); return temp; }
str bedroom
temp
width
12
length 15
kitchen
int main() length 10 { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width 12 bedroom = getData("bedroom"); den = getData("den"); length 15 cout << "total area is: "; den cout << kitchen.getArea() + width bedroom.getArea() + length bedroom.getArea() << endl; return 0; }
width
kitchen
int main() length 10 { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width 12 bedroom = getData("bedroom"); den = getData("den"); length 15 cout << "total area is: "; den cout << kitchen.getArea() + width bedroom.getArea() + length bedroom.getArea() << endl; return 0; }
width
Rectangle getData(string str) { double tempWidth, tempLength; cout << "Enter width for " << str << ": "; cin >> tempWidth; cout << "Enter length for " << str << ": "; cin >> tempLength; Rectangle temp; temp.setWidth(tempWidth); temp.setLength(tempLength); return temp; }
str den
temp
width
20
length 30
kitchen
int main() length 10 { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width 12 bedroom = getData("bedroom"); den = getData("den"); length 15 cout << "total area is: "; den cout << kitchen.getArea() + width 20 bedroom.getArea() + length 30 bedroom.getArea() << endl; return 0; }
width
kitchen
int main() length 10 { Rectangle kitchen, bedroom, den; bedroom kitchen = getData("kitchen"); width 12 bedroom = getData("bedroom"); den = getData("den"); length 15 cout << "total area is: "; den cout << kitchen.getArea() + width 20 bedroom.getArea() + length 30 bedroom.getArea() << endl; return 0; }
width
Rectangle
Rectangle rect2
setWidth setLength
Private data:
getWidth
width length
10 13
width length
20 15
getLength
calcArea
Structures
Classes
Technically, same
// SPECIFICATION FILE
( Rectangle.h )
class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double calcArea() const; };
interface file
main program
Rectangle.h
#include "Rectangle.h"
implementation file
client.cpp
Compiler
Rectangle.cpp
Compiler
client.obj
Linker
Rectangle.obj
client.exe
#ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double calcArea() const; };
#endif
How about
Rectangle rect(20, 15); where
20 would be width
15 would be length
class Rectangle { private: double width; double length; public: Rectangle(); //default constructor Rectangle(double w, double len); void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double calcArea() const; };
//default constructor
};
Rectangle::Rectangle(double w, double len) { if (w > 0) width = w; else width = 0; int main() { if (len > 0) length = len; Rectangle rect1(10, 20); else length = 0; } }