Contents:
1. The Concept of Abstraction.
2. Procedural Abstraction and Data Abstraction.
3. Structure in C++.
4. OOP in C++.
5. Time Class Example.
The Concept of Abstraction:
Abstraction means to focus on the meaning and neglect of the details of the implementation.
Procedural Abstraction and Data Abstraction:
Procedural Abstraction:
Is the building of a new addition to the previous operations in the language definition. In C and C ++, we can obtain this form of abstraction using the concept of functions.
Data Abstraction:
Enriching the language patterns by building new data.
Abstract Data Types:
Data pattern is a pattern which the programmer just deal with through the processes available to it, not through the interior detail. When defined, we baptize to define a space for a range of values or objects in addition to the permitted operations could performed on these values or objects.
Structures in C++:
Structure: is a complex data type built using elements of other types may also be structures.
Example:
struct Time {
int hour;
int minute;
int second;
};
The name of structure is Time. hour, minute and second are the structure’s elements.
We can define new variables of this new structure type as follow:
Time timeObject, timeArray[ 10 ], *timePtr;
And we can access to the members of the structure using:
• Point (.) for objects
• Arrow (->) for pointers.
Example: to display hour member in timeObject:
cout << timeObject.hour;
timePtr = &timeObject; // timePtr pointer to timeObject
cout << timePtr->hour; // prints timeObject.hour
In the following steps we will create a time structure, set its members, and print it in two different ways (24 and 12 systems):
1. Define Time Structure:
struct Time { // structure definition
int hour; // 0-23
int minute; // 0-9
int second; // 0-59
};
2. Define Functions to Print The Time:
// Print the time in 24 format
void print24( const Time &t )
{
cout << ( t.hour < 10 ? "0" : "" ) << t.hour << ":"
<< ( t.minute < 10 ? "0" : "" ) << t.minute;
}
// Print the time in standard format
void print12( const Time &t )
{
cout << ( ( t.hour == 0 || t.hour == 12 ) ?
12 : t.hour % 12 )
<< ":" << ( t.minute < 10 ? "0" : "" ) << t.minute
<< ":" << ( t.second < 10 ? "0" : "" ) << t.second
<< ( t.hour < 12 ? " AM" : " PM" );
}
3. Implement "main" Function to Use Time Structure and it's Functions:
3.1. Define a New Variable of Time Structure Type:
Time dinnerTime; // variable of new type Time
3.2. Set Members to Valid Values:
dinnerTime.hour = 18;
dinnerTime.minute = 30;
dinnerTime.second = 0
3.3. Print the Time in Two Ways:
cout << "Dinner will be held at ";
print24( dinnerTime );
cout << " military time,\nwhichis ";
print12( dinnerTime );
cout << " standard time.\n";
3.4. Set Members to Invalid Values and Print it:
innerTime.hour = 29;
dinnerTime.minute = 73;
cout << "\nTimewith invalid values: ";
print24( dinnerTime );
cout << endl;
Note: You can find the full source code of this example in code .zip file.
OOP in C++:
Encapsulation:
Put the attributes and operations on them in packages called classes.
Information Hiding:
Hide the details of implementation within the class.
There are two phases in OOP:
1. Building classes: Create new class or expand an earlier one and reused it.
2. Making interact between objects: define objects of classes and define their relations with each other.
In the following steps we will re-implement abstract data structure of Time in a class.
1. Define The Time Class:
class Time {
public:
Time();
void setTime( int, int, int );
void print24();
void print12();
private:
int hour; // 0 - 23
int minute; // 0 - 59
int second; // 0 - 59
}
- print24, setTime, and print12 are member functions.
- Time() is the constructor which is a special member creates a objects of the class and give the initial value of the data members in it, called the same name as the class, and cannot return a value.
- hour, minute, and second are data members.
- Public members can be accessed from anywhere in the program.
- Private members can be accessed only from the member functions in the class.
After define the class, we can use it as a type to define objects, arrays and pointers like follow:
Time sunset, // object of type Time
arrayOfTimes[ 5 ], // array of Time objects
*pointerToTime, // pointer to a Time object
&dinnerTime= sunset; //reference to a Time object
2. Now, we will implement the member functions, we will use the binary scope resolution operator (::) to define the class which the member function belong to.
2.1. Time constructor initializes each data member to zero. and ensures all Time objects start in a consistent state.
Time::Time() { hour = minute = second = 0; }
2.2. We set a new Time value using 24-system time, check on the data values and set invalid values to zero.
void Time::setTime( int h, int m, int s )
{
hour = ( h >= 0 && h < 24 ) ? h : 0;
minute = ( m >= 0 && m < 60 ) ? m : 0;
second = ( s >= 0 && s < 60 ) ? s : 0;
}
2.3. Print Time in 24 Format:
void Time::print24()
{
cout << ( hour < 10 ? "0" : "" ) << hour << ":"
<< ( minute < 10 ? "0" : "" ) << minute;
}
2.4. Print Time in Standard Format:
void Time::print12()
{
cout << ( ( hour == 0 || hour ==12 ) ? 12 : hour % 12 )
<< ":" << ( minute < 10 ? "0" : "" ) << minute
<< ":" << ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM" : " PM" );
}
3. Now, we can write a main function to use the Time class and its functions:
int main()
{
Time t; // instantiate object t of class Time
cout << "The initial military time is ";
t.print24();
cout << "\nThe initial standard time is ";
t.print12();
t.setTime( 13, 27, 6 );
cout << "\n\nMilitary time after setTimeis ";
t.print24();
cout << "\nStandard time after setTime is ";
t.print12();
t.setTime( 99, 99, 99 ); // attempt invalid settings
cout << "\n\nAfter attempting invalid settings:"
<< "\nMilitary time: ";
t.print24();
cout << "\nStandard time: ";
t.printStandard();
cout << endl;
return 0;
}
Note: You can find the full source code of this example in code.zip file.