Classes and Data Abstraction: (OO C++, Topic 6)
Classes and Data Abstraction: (OO C++, Topic 6)
#1.Textbook:
[3] Deitel & Deitel, “C++ How to program” (3rd edition, Prentice Hall, 2001, ISBN 0-13-089571-7,
pb, pp. 1168), chapter 6, pages 389-451.
#2. Objective ([3] p. 389): To be able to create C++ classes, to understand how to create, use,
and destroy class objects, and to access to object data members.
Properties of structures:
-They do not reserve any space in memory,
-They are used to declare data types.
1
To write member day of the structure CalendarObject referenced by CalendarPointer we need to
write:
cout << CalendarPointer->day;
Note that:
CalendarPointer->day is equivalent to (*CalendarPointer).day.
#5. Implementing Calendar data type with a class ([3] pp. 395-399):
Classes enable programmer to model objects that have:
-attributes (represented as data members), and
-behaviours or operations (represented as member functions, or methods).
class Calendar
{ //this begins the body of class definition – this body finishes with }
public: //this is a member access specifier public (accessible to all) – notice (:)
Calendar(); //this is a constructor for the class Calendar (it always has the name of its
class)
void setCalendar (int, int, int); //set day, month, year
void printMilitary(); //print military Calendar format
void printStandard(); //print standard Calendar format
~Calendar();//this is a destructor of that class (see [3] p. 419)
private: //member access specifier private (accessible only to member functions of this class) – notice (:)
int day;
int month;
int year;
}; // this ends body of the class – notice (;)
Examples ([3] p. 397): An example of a complete driver program that implements Calendar data
type with a class, including results produced by such a program, is provided in Fig. 6.3 of [3] –
see pages 397 – 399.
#6. Class scope and accessing class members ([3] pp. 402-403):
By the term “class scope” we understand a class’ data members (i.e. variables defined in
the class definition) and member functions (i.e. functions declared in the class definition). An
alternative to “class scope” is “file scope”, which includes all non-member functions. (Note that
this idea is a bit similar to “global variables” and “local variables”).
Within a class‘ scope all class members are immediately accessible by all member
functions of that class and can be referenced by name. For example see Fig. 6.4 from p. 403 of
[3]:
class Count
{
public:
int x; //declares an integer data member “x”
void print() {cout <<x <<endl;} //declares a member function print() which accesses x
};
Outside a class’ scope class members are referenced through one of the handles on an
object, namely:
2
-An object name, for example (see Fig. 6.4 from p. 403 of [3]):
int main()
{
Count counter, //create counter object of the Count class
*counterPtr = &counter, //pointer to counter
&counterRef = counter //reference to counter.
cout <<”assign 7 to x and print using the object’s name: “;
counter.x = 7;//assign 7 to data member x
counter.print(); //printing through the reference to the name of member function print()
-A reference to an object, for example (see [3] page 403, Fig. 6.4):
cout <<”Assign 8 to x and priont using a reference: “;
counterRef.x=8;//assigns 8 to data member x
counterRef.print();//calls member function print()
3
};
A destructor receives no parameters and returns no value. A class may have only one destructor.
#9. Example of a driver program that tests your skills on classes and objects.
For an example of such driver program, let us consider a program that creates a class
called cube. It is to read the side dimension of that cube, and then to calculate and to display the
volume and the surface area of that cube. Here is the code of that program in C++ (console
notation):
//---------------------------------------------------------------------------
//A driver program for testing a class named "square_bar"
//It inputs the side_dimension and height of a square_bar,
//while outputs the volume and surface area of that square_bar
//Prepared by Dr Jan Pajak, at ..., on ....
//---------------------------------------------------------------------------
#include <iostream.h>
#include <conio.h>
#include <cstdlib>
//Class definition
class square_bar
{
public:
square_bar(); //class constructor
void get_data(float, float); //member function for data input
void calculate_volume(); //member function for volume calculation
void calculate_surface(); //member function for surface calculation
void display_results(); //member function for displaying results
~square_bar(); //class destructor
private:
float side_dimension;//declares a side_dimension of a square_bar (input)
float height; //declares a height of the square_bar (input)
float volume; //declares a volume of the square_bar (output)
float surface; //declares a surface area of a square_bar (output)
};
//---------------------------------------------------------------------------
square_bar::square_bar() //the constructor - it sets default values
{
side_dimension = height = volume = surface = 0.0;
}
void square_bar::get_data(float a, float h) //this member function inputs data
4
{
side_dimension = a;
height = h;
}
void square_bar::calculate_volume() //this member function calculates the volume
{
volume = (side_dimension * side_dimension) * height;
}
void square_bar::calculate_surface() //it calculates the surface
{
surface = (4.0 * side_dimension * height) +
(2.0 * side_dimension * side_dimension);
}
void square_bar::display_results() //it displays results
{
cout <<"\nThe volume of the square_bar is: "<<volume<<endl;
cout <<"\nThe surface area of the square_bar is: "<<surface<<endl;
}
square_bar::~square_bar() //the destructor - it destroys the object
{
cout <<"\nThe bar object was destroyed!" <<endl;
}
//Functions headers
void display_end(void); //this declares that there is a "file scope" function
//Follows a driver program that tests the square_bar class
int main()
{
square_bar b; //creates an object for this class
float a, h = 0.0; //declares and initiates variables to be used
cout <<"Enter a side_dimension of a square_bar : ";
cin >>a;
cout <<"Enter the height of the same square_bar: ";
cin >>h;
b.get_data(a, h); //inputs values for variables
b.calculate_volume(); //calculates volume
b.calculate_surface(); //calculates surface
b.display_results(); //displays results
b.~square_bar(); //destroys the square_bar object
display_end(); //runs the "file scope" function to report end
return 0; //this is needed as main() is int not void
}
void display_end(void) //defines the "file scope" function
{
cout <<"\nThe correct end of run of that program!\n";
system("pause");
}
//---------------------------------------------------------------------------