490 likes | 601 Vues
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.
E N D
Object-Oriented Programming Using C++ CLASS 10 Review
Accessing a public member from main with pointer main( ) { base theclass; base *abaseclass= &theclass; abaseclass -> base_publ = 1;
Overloaded Constructor base ( ) { base_priv=0; } base ( int x) { base_priv=x; }
An attempt by a function which is not a member of a particular class to access a private member of that class
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; }
Attempting to declare a return type for a constructor int base (int y) { base_publ=0; return (0) ; }
Attempting to pass parameters to a destructor ~ledger(int x) { free list; x = 0; }
Declaring default function arguments in the function prototype and not in the definition
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;
A friend function modifying private data void getitem(ledger &object, int y) { object.count=y; }
Defining as const a member function that modifies a data member of an object
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;
Calling a non-const member function for a const object main( ) oneobject.printitem( ); }
Mixing new and delete style dynamic memory allocation with malloc and free
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; }
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
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;
Static data members and static member functions exist and can be used even if no objects of that class have been instantiated
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;
Member functions defined in a class definition are automatically inlined
Member functions defined in a class definition are automatically inlined void printitem( ) { for (int x=0; x < count; x++) cout << list[x]; }
Making data members private and controlling access, especially write access, to those data members through public member functions helps ensure data integrity
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;
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; }
4. Class membera are accessed via the operator.
5. Members of a class specified as are accessible only to member functions of the class and friends of the class.
6. An object’s member functions maintain a pointer to the object called the pointer.
7. To use operators such as +, -, *, /, <<, >> on class objects, they must be .
8. In C++ only system defined operators can be overloaded TRUE FALSE
9. Name four advantages of using objects in the design of computer programs.
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;
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;
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;
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;
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;
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;
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;
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;