You are on page 1of 90

Encapsulation Value of classes and objects Defining classes Data encapsulation Procedural encapsulation Declaring and using objects

What you have learned thus far

excellent practice

Function Function Function Data

Function Function Function Function

Function

difficult to maintain large collection of functions


Function Function Function Data Function Function Function Function

Function

For more structured code

tasks split out into collaborating objects objects have


data members and functions that act upon the data

tasks split out into collaborating objects objects have


data members and functions that act upon the data
Member Function Member Function Object Data members Member Function

Member Function

Member Function

Object Data members

Can only access object through member functions

Can only access object through member functions


string str; int length = str.length();

Can only access object through member functions


string str; int length = str.length(); cin >> str; cout << str; str = str + "help";

You can use an object without knowing its implementation

Use the member functions

Data and code in single object


Can only access data using member functions

Member Function Member Function

Object Data members

Member Function

Hide data from code that is outside the object Only member fns can directly access and alter object data
Object Data members

Member Function Member Function

Client Code

Programmer

doesn't implement single object implements a class

Describes set of objects with same behavior Specify


define data members

define member functions


implement member functions

class

class

Instances of house described by blueprint

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;

sets up space in memory tells compiler how to allocate

#include <string>
string cityName; cityName = "Ann Arbor";

assign value into instance

#include <string>
string cityName; cityName = "Ann Arbor";
member function

int size = cityName.length();


instance

#include <iostream>
int val; cin >> val;

cout << val + 5;


instance of class istream instance of class ostream

class

#include <fstream>
ifstream ins;

instance

ins.open("data.txt");

class

#include <fstream>
ifstream ins;

instance

ins.open("data.txt");

member function

class Rectangle { double width; double length; };

struct Rectangle { double width; double length; };

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

Data Code that manipulates that data

Interface

Data Code that manipulates that data

Interface

Data Code that manipulates that data Clients

class Rectangle { double width; double length; };

struct Rectangle { double width; double length; };

class Rectangle { public double width; double length; };

struct Rectangle { double width; double length; };

//defaults public

class Rectangle int main { { public: Rectangle rect; double width; rect.width = 5; double length; rect.length = 10; }; }

class Rectangle { private: double width; double length; };

class Rectangle { private: double width; double length; };

provides protection for data

class Rectangle { private: double width; double length; } };


provides protection for data

int main { Rectangle rect; rect.width = 5; 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);

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);

private: double width; double length; };

use "public interface" to alter private data members

setWidth setLength

rect width: length:

int main { Rectangle rect; rect.setWidth(5); rect.setLength(10); }

setWidth setLength

rect
width:
length:

5 10

int main { Rectangle rect; rect.setWidth(5); rect.setLength(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; }

double getWidth() const; double getLength() const; double calcArea() const;

blocks function from altering private data

double getWidth() const; double getLength() const; double calcArea() const;

blocks function from altering private data


compile error if attempted

void Rectangle::setWidth(double w);


indicates: setWidth()

void Rectangle::setWidth(double w);


indicates: setWidth() part of Rectangle class

box

int main() { Rectangle box; double tempWidth, tempLength;

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

int main() { Rectangle box; double tempWidth, tempLength;

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

int main() { Rectangle box; double tempWidth, tempLength;

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

int main() { Rectangle box; double tempWidth, tempLength;

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

Class Instance Diagrams


Rectangle rect1
setWidth
setLength
Private data:

Rectangle rect2
setWidth setLength
Private data:

getWidth

width length

10 13

getWidth getLength calcArea

width length

20 15

getLength
calcArea

Structures

Typically all members public No member functions


Typically all data members private Interface member functions public Perceptually, very different mechanisms

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

// IMPLEMENTATION FILE #include "Rectangle.h" #include <iostream> void Rectangle::setWidth(double w) { . . . }

void Rectangle::setLength(double len) { . . . }

interface file

main program

Rectangle.h
#include "Rectangle.h"

implementation file

client.cpp
Compiler

Rectangle.cpp
Compiler

client.obj
Linker

Rectangle.obj

client.exe

#include "Rectangle.h" in Rectangle.cpp client.cpp

use preprocessor directive to avoid this

#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

#include "Rectangle.h" #include "iostream" using namespace std;


void Rectangle::setWidth(double w) { width = w; } . . .

#include "Rectangle.h" #include <iostream> using namespace std;


int main() { Rectangle rect1, rect2; rect1.setWidth(5); rect1.setLength(10); ...

int x = 5; int y(10); string str("Help");


bool flag(true);

How about
Rectangle rect(20, 15); where
20 would be width

15 would be length

The constructor is a member function


purpose is to initialize the private data

members of a class object

The name of a constructor is always the name of the class


no return type for the constructor

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

Rectangle::Rectangle( ) { width = 0; length = 0; }

Rectangle::Rectangle( ) { width = 0; length = 0; } int main() { Rectangle rect1; . . . . . . }

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;

};

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

Classes creation why header files preprocessor directives constructors

You might also like