1 / 56

DATA ABSTRACTION: USER DEFINED TYPES AND THE CLASS

DATA TYPES. A DATA TYPE IS A FORMAL DESCRIPTION OF:1. THE DOMAIN THAT AN OBJECT OF THAT TYPE CAN HAVE.2. THE BASIC SET OF OPERATIONS THAT CAN BE APPLIED TO VALUES OF THAT TYPE.3. DATA REPRESENTATION. . . THE FORMAL DESCRIPTION OF int AND float VALUES AND THE ALLOWABLE OPERATIONS ON SUCH DATA

adelaide
Download Presentation

DATA ABSTRACTION: USER DEFINED TYPES AND THE CLASS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. DATA ABSTRACTION: USER DEFINED TYPES AND THE CLASS

    2. DATA TYPES

    3.

    4. THE VALUES OF TYPE char ARE THE ALPHANUMERIC CHARACTERS OF THE ASCII SET. MEMBERS ARE ORDERED IN RELATION TO EACH OTHER. '0' < '1' < '2' < ... '7' < '8' < '9' AND 'a' < 'b' < 'c'< ... 'x' < 'y' < 'z'

    5. AN OBJECT OF THE BASIC TYPES int, float, AND char CAN HOLD ONLY ONE VALUE. EACH ONE IS MADE UP OF INDIVISIBLE, OR ATOMIC ELEMENTS. int AND char SHARE A COMMON PROPERTY: THEY ARE ORDINAL TYPES. THE VALUES OF EACH TYPE CAN BE LISTED IN A SEQUENCE.

    6. IN SOME INSTANCES, THE BASIC TYPES MAY NOT BE SUITABLE FOR DATA REPRESENTATION, OR SIMPLY NOT QUITE READABLE. C++ HAS A MECHANISM FOR ALLOWING TO CREATE NEW DATA TYPES. THAT IS, THE PROGRAMMERS DECLARE AND DEFINE NEW TYPES.

    12. EXAMPLE:

    16. NEW CONCEPTS

    17. RELATIONSHIP BETWEEN DATA AND OPERATIONS: SEPARATE ENTITIES

    19. RELATIONSHIP BETWEEN DATA AND OPERATIONS: A COHERENT UNIT

    22. CLASS SYNTAX Member access specifiers are always followed by a colon (:) and can appear multiple times in any order. Member access specifiers are always followed by a colon (:) and can appear multiple times in any order.

    23. CONSTRUCTOR: defining, call, overloading, no return type.CONSTRUCTOR: defining, call, overloading, no return type.

    24. EXAMPLE 1 // class declaration section class Date { private: int month; //a data member int day; // a data member int year; // a data member public: Date( int = 11, int = 16, int = 2001 ); // the constructor void setdate(int, int, int); // a member function void showdate(); // a member function } ;

    25. SYNTAX OF DEFINING MEMBER FUNCTIONS

    26. EXAMPLE 1 // class implementation section Date::Date( int mm, int dd, int yyyy) { month = mm; day = dd; year = yyyy; } void Date:: setdate(int mm, int dd, int yyyy) { month = mm; day = dd; year = yyyy; return ; }

    27. void Date::showdate() { cout<< “ The date is “; cout<< setfill ( ‘0’) << setw(2)<< month << ‘/ ‘ << setw(2)<< day << ‘/ ‘ << setw(2)<< year % 100 ; // extract last //two year digits cout<< ednl ; return ; }

    28. Class Scope and Accessing Class Members A class data members and function members belong to that class scope. Nonmember functions are defined as file scope. Within class scope members are accessible by using their name Outside class scope, class members are referenced through one of the handles on an object – an object name, a reference to an object or pointer to an object. Variable defined in member function has function scope. You can hide data member in a function by using same name.

    29. Once the class has been defined, it can be used as a type in object, array and pointer definitions. For example class Date can be used in the following main function as follows

    30. int main () { Date a, b, c(4, 1, 1998) // declare three objects – //initialize one of them b.setdate(12, 25, 2002);//assign values to b’s data members a.Showdate( ); // display object a’s values b.Showdate( ); // display object b’s values c.Showdate( ); // display object c’s values return 0 ; } /*output The date is 07/04/01 The date is 12/25/02 The date is 04/01/98 */

    36. See example on page 405 and 406

    37. Constructors A constructor function is any function that has the same name as it s class. Constructors can be over loaded Constructor has no return type If no constructor is written compiler will provide default constructor

    38. Default Constructor Any constructor that does not require any parameters when it is called. This can be because no parameters are declared All parameters have been given default values. For example Date(int=7, int =4, int = 2001 )

    39. Destructor The name of the destructor for a class is the tilde(~) character followed by the name of the class. There can be only one destructor Destructor take no parameter and it has no return value Destructor is called when a class object is destroyed. A default destructor is provided if not written by programmer.

    40. ANOTHER EXAMPLE:

    43. NEW CONCEPTS

More Related