1 / 49

Object-Oriented Programming Using C++

Object-Oriented Programming Using C++. CLASS 10 Review. Accessing a public member from main with pointer. Accessing a public member from main with pointer. main( ) { base theclass; base *abaseclass= &theclass; abaseclass -> base_publ = 1;. Overloaded Constructor.

ermin
Télécharger la présentation

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 10 Review

  2. Accessing a public member from main with pointer

  3. Accessing a public member from main with pointer main( ) { base theclass; base *abaseclass= &theclass; abaseclass -> base_publ = 1;

  4. Overloaded Constructor

  5. Overloaded Constructor base ( ) { base_priv=0; } base ( int x) { base_priv=x; }

  6. An attempt by a function which is not a member of a particular class to access a private member of that class

  7. An attempt by a function which is not a member of a particular class to access a private member of that class void getdata(base &b) { b.base_priv=0; }

  8. Attempting to declare a return type for a constructor

  9. Attempting to declare a return type for a constructor int base (int y) { base_publ=0; return (0) ; }

  10. Attempting to pass parameters to a destructor

  11. Attempting to pass parameters to a destructor ~ledger(int x) { free list; x = 0; }

  12. Declaring default function arguments in the function prototype and not in the definition

  13. Declaring default function arguments in the function prototype and not in the definition void ledger::enteritem(int x, int y) const { list[count++]= x+y;

  14. A friend function modifying private data

  15. A friend function modifying private data void getitem(ledger &object, int y) { object.count=y; }

  16. Defining as const a member function that modifies a data member of an object

  17. Defining as const a member function that modifies a data member of an object void ledger::enteritem(int x, int y) const { list[count++]= x+y;

  18. Calling a non-const member function for a const object

  19. Calling a non-const member function for a const object main( ) oneobject.printitem( ); }

  20. Mixing new and delete style dynamic memory allocation with malloc and free

  21. Mixing new and delete style dynamic memory allocation with malloc and free public: int number; ledger( ) { count = 0; list = new float[100]; ~ledger( ) { free list; x = 0; }

  22. Declaring objects const helps enforce the principle of least privilege. Accidental attempts to modify the object are caught at compile time rather than causing execution-time errors

  23. Declaring objects const helps enforce the principle of least privilege. Accidental attempts to modify the object are caught at compile time rather than causing execution-time errors const ledger oneobject; oneobject.number=0;

  24. Static data members and static member functions exist and can be used even if no objects of that class have been instantiated

  25. Static data members and static member functions exist and can be used even if no objects of that class have been instantiated int base :: z = 0;

  26. Member functions defined in a class definition are automatically inlined

  27. Member functions defined in a class definition are automatically inlined void printitem( ) { for (int x=0; x < count; x++) cout << list[x]; }

  28. Making data members private and controlling access, especially write access, to those data members through public member functions helps ensure data integrity

  29. Making data members private and controlling access, especially write access, to those data members through public member functions helps ensure data integrity void ledger::enteritem(int x, int y) const { list[count++]= x+y;

  30. Call an initialization function from a constructor

  31. Call an initialization function from a constructor base(int x, int y) { setbase(x, y); } setbase(int a, int b) { base_priv=a; base_priv=b; }

  32. Review

  33. 1. What is the purpose of the scope resolution?

  34. 2. What are constructors and destructors?

  35. 3. reclaims memory previously allocated by new.

  36. 4. Class membera are accessed via the operator.

  37. 5. Members of a class specified as are accessible only to member functions of the class and friends of the class.

  38. 6. An object’s member functions maintain a pointer to the object called the pointer.

  39. 7. To use operators such as +, -, *, /, <<, >> on class objects, they must be .

  40. 8. In C++ only system defined operators can be overloaded TRUE FALSE

  41. 9. Name four advantages of using objects in the design of computer programs.

  42. 10. Use the following code to answer the next 7 questions #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

  43. A. Write a valid constructor #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

  44. B. Write main which instantiates an object of type Book. #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

  45. C. Write the show_book function which displays the # of the total books on hand, the title, the author, and the total # of pages. #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

  46. D. Write a valid destructor #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

  47. E. Write a copy constructor #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

  48. F. Write the code for an overloaded assignment operator. #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

  49. G. Overload the << operator to display the author and the title only. #include <iostream.h> #include <sring.h> class book { public: Book (char *title, char *author, int pg); void show_book(void); private: char title[64]; char *author; int pages; static int number_of_books;

More Related