Object Oriented Programming, Lecture 07.pdf
Document Details
Uploaded by ImprovedEnglishHorn
Zagazig University
Tags
Related
Full Transcript
Object Oriented Programming Ch.8: Introduction to Classes Ⅱ Prepared By Dr. Ibrahim Attiya © 2023 Zagazig University Ch.8: Outline ❑ Procedural vs. Object-Oriented Programming ❑ Introduction to Classes ❑ Defining an Instance of a Class ❑ Inline Member Functi...
Object Oriented Programming Ch.8: Introduction to Classes Ⅱ Prepared By Dr. Ibrahim Attiya © 2023 Zagazig University Ch.8: Outline ❑ Procedural vs. Object-Oriented Programming ❑ Introduction to Classes ❑ Defining an Instance of a Class ❑ Inline Member Functions ❑ Constructors ❑ Destructors ❑ Overloading Constructors ❑ Arrays of Objects ❑ Friends of Classes Destructors Destructors A destructor is a member function that is automatically called when an object is destroyed. Destructors are member functions with the same name as the class, preceded by a tilde character (~). For example, the destructor for the Rectangle class would be named ~Rectangle. Has no return type; takes no arguments. Only one destructor per class, i.e., it cannot be overloaded. A common use of destructors is to free memory that was dynamically allocated by the class object. Program Ⅰ Contents of Distance.h Program Ⅰ (Cont.) Contents of main program Constructors, Destructors, and Dynamically Allocated Objects If a class object has been dynamically allocated by the new operator, its memory should be released when the object is no longer needed. When an object is dynamically allocated with the new operator, its constructor executes: Rectangle *rectptr; rectptr = new Rectangle(10, 20); When the object is destroyed, its destructor is automatically called. delete r; The delete operator being used to destroy the dynamically created object. Overloading Constructors Overloading Constructors A class can have more than one constructor. A class’s member functions may be overloaded, including the constructor. One constructor might take an integer argument, for example, while another constructor takes a double. There could even be a third constructor taking two integers. Overloaded constructors in a class must have different parameter lists: Rectangle(); Rectangle(double); Rectangle(double, double); Overloading Constructors- An Example class StockItem { private: string description; // The item description double cost; // The item cost int units; // Number of units public: StockItem() {description = ""; cost = 0.0; units = 0; } StockItem(string desc) {description = desc; cost = 0.0; units = 0; } StockItem(string desc, double c, int u) {description = desc; cost = c; units = u; } Only One Default Constructor and One Destructor Do not provide more than one default constructor for a class: one that takes no arguments and one that has default arguments for all parameters. Square(); Square(int = 0); // will not compile If there were more than one constructor that could be called without an argument, the compiler would not know which one to call by default. Since a destructor takes no arguments, there can only be one destructor for a class. Member Function Overloading Member functions other than constructors can also be overloaded. This can be useful because sometimes you need several different ways to perform the same operation. void setCost(double c); void setCost(string c); Must have unique parameter lists as for constructors. Using Private Member Functions Using Private Member Functions A private member function may only be called from a function that is a member of the same class. It is used for internal processing by the class, not for use outside of the class. For example, a class might have a member function that performs a calculation only when a value is stored in a particular member variable and should not be performed at any other time. In this case, the member function should be declared private. Arrays of Objects Arrays of Objects You may define and work with arrays of class objects. Objects can be the elements of an array: StockItem stock; Default constructor for object is used when array is defined. Must use initializer list to invoke constructor that takes arguments: StockItem stock = {"Hammer", "Wrench", "Pliers"}; Arrays of Objects If the constructor requires more than one argument, the initializer must take the form of a function call: StockItem stock = { StockItem("Hammer", 7.83, 20), StockItem("Wrench", 9.63, 15), StockItem("Pliers“, 4.50, 10) }; This definition statement calls the third constructor in the StockItem class declaration for each object in the stock array. Arrays of Objects It isn't necessary to call the same constructor for each object in an array: StockItem stock[] = { "Hammer", StockItem("Wrench", 9.63, 15), "Pliers" }; This statement calls the second constructor for stock and stock, and calls the third constructor for stock. Accessing Objects in an Array Objects in an array are accessed with subscripts, just like any other data type in an array. Member functions are referenced using dot notation: stock.setUnits(25); cout