Object-Oriented Programming In C++: Classes and Objects
Submitted by PhoeniciansSy on Tuesday, September 9, 2014 - 12:06.
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;
- };
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
- struct Time { // structure definition
- int hour; // 0-23
- int minute; // 0-9
- int second; // 0-59
- };
- // 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" );
- }
Time dinnerTime; // variable of new type Time
3.2. Set Members to Valid Values:
- dinnerTime.hour = 18;
- dinnerTime.minute = 30;
- dinnerTime.second = 0
- cout << "Dinner will be held at ";
- print24( dinnerTime );
- cout << " military time,\nwhichis ";
- print12( dinnerTime );
- cout << " standard time.\n";
- innerTime.hour = 29;
- dinnerTime.minute = 73;
- cout << "\nTimewith invalid values: ";
- print24( dinnerTime );
- cout << endl;
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.
- 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
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;
- }
- void Time::print24()
- {
- cout << ( hour < 10 ? "0" : "" ) << hour << ":"
- << ( minute < 10 ? "0" : "" ) << minute;
- }
- void Time::print12()
- {
- cout << ( ( hour == 0 || hour ==12 ) ? 12 : hour % 12 )
- << ":" << ( minute < 10 ? "0" : "" ) << minute
- << ":" << ( second < 10 ? "0" : "" ) << second
- << ( hour < 12 ? " AM" : " PM" );
- }
- 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;
- }
Add new comment
- 36 views