Object Oriented Programing
Lecture 3
Classes : An abstract data-type or a user-defined data type
Class Syntax Class Example
Class ClassName { class Rectangle {
declaration; double width; | Data (Attributes) :
declaration; double length; | define state in objects
};
displayWidth( ); | Functions : define the
displayLength( ); | behavior of objects
displayArea( ); |
};
Object : Instance of a class. Every object has a unique identity, its own state.
Object Syntax Object Example
ClassName objectName; Rectangle r;
Access Specifiers: control access to members of the class
public: can be accessed by functions outside of the class
private: can only be accessed by functions that are members of the class
Access Specifiers Example
class Rectangle { Keep data attributes private and functions
private: public
double width; To protect data from unwanted access and
double length; modification a.k.a data hiding
public:
getWidth( ) { return width; }
};
Rectangle r1; //class object
cout << r1.getWidth( ); //works getWidth( ) is public
cout << r1.width; //ERROR, width is private
Can appear in any order in a class
Can appear multiple times in a class
If not specified, the default is private
A class is like struct, but not the same
Members of a struct are public by default
Members of a class are private by default
Member Functions:
in-line member function out-of-line member functions
Within the class declaration Outside the class declaration
Example Put prototype in class declaration
class Rectangle { Define function using class name and
private: scope resolution operator (::)
double width; Combine class name with member
double length; function name
public: Different classes can have member
double calcArea( ) functions with the same name
{ Syntax
return width * length; //inline
returnType ClassName::MemberFunction( )
}
{
};
…………
}
Example
class Rectangle {
private:
double width;
double length;
public:
double calcArea( ) //prototype
}; //class declaration ends
double Rectangle::calcArea( )
{
return width * length; //out-of-line
}
Whether member function is inline or out-of-line Access remains the same as declared in the class
Private Member Functions:
Example
Only callable from member
class Rectangle {
functions of the class
private:
No direct access possible with
double width;
object double length;
Can be: inline / out-of-line double calcArea() // private
A private member function can member
only be called by another member {
function return width * length;
It is used for internal processing by }
the class, not for use outside of public:
the class void displayArea()
{
cout<< calcArea();
}
};
Setters and Getters:
Setter (Mutator) Example
Member function that stores/changes a value in class Rectangle {
a private member variable private:
Getter (Accessor) double width;
Member function that retrieves a value from a double length;
private member variable. public:
Constraints can be added in setters to void setWidth(double w) // Setter
prevent unwanted values in data {
members width = w;
}
void setLength(double l) // Setter
{
length = l;
}
double getWidth( ) // Getter
{
return width;
}
double getLength( ) // Getter
{
return length;
}
};
Const Member Functions: A read-only function, cannot change the value of any
attribute
Example 1 Example 2
class Rectangle { class Rectangle {
private: private:
double width; double width;
public: double length;
void changeWidth( ) const public:
{ void setWidth(double);
width++; //ERROR void setLength(double);
} double getWidth( ) const { return width; }
}; double getLength( ) const { return length; }
double getArea( ) const { return width *
length; }
};
Getters do not change an object's data, so they should
be marked const.
Avoiding Stale Data:
Some data is the result of a calculation. class Rectangle {
In the Rectangle class the area of a rectangle is private:
calculated using length x width double width;
If we were to use an area variable here in the double length;
Rectangle class, its value would be dependent public:
on the length and the width. void setWidth(double);
If we change length or width without updating void setLength(double);
area, then area would become stale. double getWidth( ) const { return width;
To avoid stale data, }
it is best to calculate the value of that data double getLength( ) const { return
within a member function rather than store it length; }
in a variable. double getArea( ) const { return width *
length; }
};
Pointer to an Object:
Can define a pointer to an object:
Rectangle *rPtr;
rPtr = &otherRectangle;
Can access public members via pointer. You can use * and . OR - >
rPtr - >setLength(12.5);
rPtr - >setWidth(4.8);
cout << rPtr - >getLenght( ) << endl;
Dynamically Allocating an Object:
We can also use a pointer to dynamically allocate an object.
Rectangle *rPtr = new Rectangle;
rPtr->setLength(12.5);
rPtr->setWidth(10.3);
Deallocate memory and delete object
delete rPtr;
rPtr = NULL;
Reference to Objects:
A reference variable provides a new name to an existing variable.
Reference and Pointers to Objects
Code Output
class Rectangle { Area (object) = 20
private: Area (pointer) = 20
int w; Area (reference to object) =
int h; 20
public: Area (ref to pointer) = 20
void setWidth(int ww) { w = ww; }
void setHeight(int hh) { h = hh; }
double getArea( ) const { return w * h; }
};
int main( )
{
Rectangle r1;
Rectangle *ptr = &r1;
Rectangle &ref = r1;
Rectangle* &ref2 = ptr;
r1.setWidth( 5 );
r1.setHeight( 4 );
cout<<“\n Area (object) = “<<r1.getArea( );
cout<<“\n Area (pointer) = “<<ptr->getArea( );
cout<<“\n Area (reference to object) = “<<ref.getArea( );
cout<<“\n Area (ref to pointer) = “<<ref->getArea( );
return 0;
}