1 / 21

Object-Oriented Programming Using C++

Object-Oriented Programming Using C++. CLASS 3. Objectives. To understand the software engineering concepts of encapsulation and data hiding To understand the notions of data abstraction and abstract data types (ADTs) To be able to create, use, and destroy class objects. Objectives.

bond
Download Presentation

Object-Oriented Programming Using C++

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. Object-Oriented Programming Using C++ CLASS 3

  2. Objectives • To understand the software engineering concepts of encapsulation and data hiding • To understand the notions of data abstraction and abstract data types (ADTs) • To be able to create, use, and destroy class objects

  3. Objectives • To be able to control access to object data members and member functions • To begin to appreciate the value of object orientation

  4. Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method };

  5. Declaring a Class class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Keyword class to specify that we’re declaring a class called ledger.

  6. Declaring a Class class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • The body of declaration is delimited with curly brackets and terminated with a semicolon.

  7. Declaring a Class class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Within the class the variables list and count are declared

  8. Declaring a Class • In a class, we can declare functions as well as variables. Functions in class are called methods. Declare the methods initCount( ), enterItem( ), and printTotal( ). class ledger// declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method };

  9. Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Some members of class ledger are private, and some are public.

  10. Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • The data items are all private

  11. Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • The methods are all public

  12. Declaring a Class • Some members of class ledger are private, and some are public. The data items are all private and the methods are all public. Public can be accessed from outside the object and private can be accessed only from within the object. The keyword private is the default. class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method };

  13. Declaring a Class class ledger // declaration of class ledger { private: float list[100]; //define array for items int count; //define number of items entered public: void initCount(void); //decLare method void enterItem(void); //decLare method void printTotal(void); //decLare method }; • Declaration of ledger shown above does not define any objects of class ledger. It only specifies what objects will contain if they are defined.

  14. void ledger::intCount(void) //a function in class ledger //initializes count to 0 { count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; }

  15. void ledger::intCount(void) //a function in class ledger //initializes count to 0 {count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; } • initCount( ) simply sets the variable count to 0

  16. void ledger::intCount(void) //a function in class ledger //initializes count to 0 { count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; } • enterItem( ), prints a prompt to the user, and then assigns the amount typed by the user to the array member list[count].

  17. printTotal( ) finds the total of the amounts and prints out the total void ledger::intCount(void) //a function in class ledger //initializes count to 0 { count = 0; } void ledger::enterItem(void) //a function in ... //puts entry in list { cout << “\nEnter amount:”; cin >> list[count++]; } void ledger::printTotal(void) // a function in... //prints total of all { float total = 0.0; for(int j=0; j<count; j++) total + = list[ j ]; cout << “\n\nTotal is: “ << total << “\n”; }

  18. Expense.cppOverhead

  19. Main DriverOverhead

  20. Create a Time Class Overhead

  21. Q & A

More Related