Object-Oriented Programming In C++: Classes and Objects

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:
  1. struct Time {
  2. int hour;
  3. int minute;
  4. int second;
  5. };
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:
  1. cout << timeObject.hour;
  2. timePtr = &timeObject; // timePtr pointer to timeObject
  3. 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:
  1. struct Time { // structure definition
  2. int hour; // 0-23
  3. int minute; // 0-9
  4. int second; // 0-59
  5. };
2. Define Functions to Print The Time:
  1. // Print the time in 24 format
  2. void print24( const Time &t )
  3. {
  4. cout << ( t.hour < 10 ? "0" : "" ) << t.hour << ":"
  5. << ( t.minute < 10 ? "0" : "" ) << t.minute;
  6. }
  7. // Print the time in standard format
  8. void print12( const Time &t )
  9. {
  10. cout << ( ( t.hour == 0 || t.hour == 12 ) ?
  11. 12 : t.hour % 12 )
  12. << ":" << ( t.minute < 10 ? "0" : "" ) << t.minute
  13. << ":" << ( t.second < 10 ? "0" : "" ) << t.second
  14. << ( t.hour < 12 ? " AM" : " PM" );
  15. }
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:
  1. dinnerTime.hour = 18;
  2. dinnerTime.minute = 30;
  3. dinnerTime.second = 0
3.3. Print the Time in Two Ways:
  1. cout << "Dinner will be held at ";
  2. print24( dinnerTime );
  3. cout << " military time,\nwhichis ";
  4. print12( dinnerTime );
  5. cout << " standard time.\n";
3.4. Set Members to Invalid Values and Print it:
  1. innerTime.hour = 29;
  2. dinnerTime.minute = 73;
  3. cout << "\nTimewith invalid values: ";
  4. print24( dinnerTime );
  5. 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:
  1. class Time {
  2. public:
  3. Time();
  4. void setTime( int, int, int );
  5. void print24();
  6. void print12();
  7. private:
  8. int hour; // 0 - 23
  9. int minute; // 0 - 59
  10. int second; // 0 - 59
  11. }
  • 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:
  1. Time sunset, // object of type Time
  2. arrayOfTimes[ 5 ], // array of Time objects
  3. *pointerToTime, // pointer to a Time object
  4. &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.
  1. void Time::setTime( int h, int m, int s )
  2. {
  3. hour = ( h >= 0 && h < 24 ) ? h : 0;
  4. minute = ( m >= 0 && m < 60 ) ? m : 0;
  5. second = ( s >= 0 && s < 60 ) ? s : 0;
  6. }
2.3. Print Time in 24 Format:
  1. void Time::print24()
  2. {
  3. cout << ( hour < 10 ? "0" : "" ) << hour << ":"
  4. << ( minute < 10 ? "0" : "" ) << minute;
  5. }
2.4. Print Time in Standard Format:
  1. void Time::print12()
  2. {
  3. cout << ( ( hour == 0 || hour ==12 ) ? 12 : hour % 12 )
  4. << ":" << ( minute < 10 ? "0" : "" ) << minute
  5. << ":" << ( second < 10 ? "0" : "" ) << second
  6. << ( hour < 12 ? " AM" : " PM" );
  7. }
3. Now, we can write a main function to use the Time class and its functions:
  1. int main()
  2. {
  3. Time t; // instantiate object t of class Time
  4. cout << "The initial military time is ";
  5. t.print24();
  6. cout << "\nThe initial standard time is ";
  7. t.print12();
  8. t.setTime( 13, 27, 6 );
  9. cout << "\n\nMilitary time after setTimeis ";
  10. t.print24();
  11. cout << "\nStandard time after setTime is ";
  12. t.print12();
  13. t.setTime( 99, 99, 99 ); // attempt invalid settings
  14. cout << "\n\nAfter attempting invalid settings:"
  15. << "\nMilitary time: ";
  16. t.print24();
  17. cout << "\nStandard time: ";
  18. t.printStandard();
  19. cout << endl;
  20. return 0;
  21. }
Note: You can find the full source code of this example in code.zip file.

Tags

Add new comment