oop objects_classes

181 views
103 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
181
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

oop objects_classes

  1. 1. Objects and Classes
  2. 2. Objectives  To learn how to create objects with new  To learn the use of destructor  To study constructor initializer list  To study accessor and mutator methods  To see the working of Default copy constructor and Member-wise Copy  To learn the scope of variables
  3. 3. A Simple Program – Object Creation class Circle { private: double radius; public: Circle() { radius = 5.0; } double getArea() { return radius * radius * 3.14159; } }; Object InstanceC1 : C1 radius: 5.0 void main() { Circle c1; Circle *c2 = new Circle(); cout<<“Area of circle = “<<c1.getArea()<<c2.getArea(); } Allocate memory for radius Object InstanceC2 : C2 radius: 5.0 Allocate memory for radius
  4. 4. Destructor  A destructor is a member function with the same name as its class prefixed by a ~ (tilde).  Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed.  A destructor is called for a class object when that object passes out of scope or is explicitly deleted.
  5. 5. Destructor  A destructor takes no arguments and has no return type.  Destructors cannot be declared const, volatile, const or static. A destructor can be declared virtual or pure virtual.  If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.
  6. 6. Destructor - Example class Circle { private: double radius; public: Circle(); Circle(double); double getArea(); ~Circle(); }; Circle::Circle() { radius = 1.0; } Circle::Circle(double rad) { radius = rad; } double Circle::getArea() { return radius * radius * 3.14; } Circle::~Circle() { cout<<“Destructor called”; } void main() { Circle c1; Circle *c2 = new Circle(10.7); cout<<“Area of circle = “<<c1.getArea(); cout<<“Area of circle = “<<c2.getArea(); } Class declaration Class Implementation
  7. 7. Constructor Initializer Circle(): radius(1) { } Circle() { radius = 1; } Same as (a) (b)
  8. 8. Member Initialization List  One task of the Constructor is to initialize data members  Example: for class Circle Circle(){ radius = 1.0; } But it is not preferred approach. The preferred approach is Circle(): radius(1.0) { }  If multiple members be to initialize, separate them by commas. The result is the initializer list (also known as member-initialization list) Circle(): radius(1.0), area(0.0) { } Member Initialization List
  9. 9. Reasons for Constructor Initializers  In the initializer list, members initialized with given values before the constructor even starts to execute  This is important in some situations e.g. the initializer list is the only way to initialize const member data and references  However, actions more complicated than simple initialization must be carried out in the constructor body, as with ordinary functions.
  10. 10. Accessor (getter) and Mutator (Setter) Functions  Colloquially, a get function is referred to as a getter (or accessor), and a set function is referred to as a setter (or mutator).  A get function has the following header: returnType getPropertyName(){ }  A set function has the following header: void setPropertyName(dataType propertyValue){ }
  11. 11. Example class VehiclePart{ private: int modelno; int partno; float cost; public: VehiclePart():modelno(0),partno(0),cost(0.0) { } void setModelNo(int mn){ modelno = mn; } void setPartNo(int pn){ partno = pn; } void setCost(float c){ cost = c; } void setVehDetail(int mn, int pn, float c) { modelno = mn; partno = pn; cost = c; } int getModelNo(){ return modelno; } int getPartNo(){ return partno; } float getCost(){ return cost; } void showVehDetail() { cout<<modelno<<partno<<cost; } }; Mutator Functions Accessor Functions
  12. 12. Example class Book{ private: int bookID, totalPage; float price; public: Book():bookID(0),totalPage(0),price(0.0) { } void setBookID(int bid){ bookID = bid; } void setPages (int pn){ totalPage = pn; } void setPrice(float p){ price = p; } int getBookID(){ return bookID; } int getPages(){ return totalPages; } float getPrice(){ return price; } }; Mutator Functions Accessor Functions
  13. 13. Default copy constructor  A type of constructor that is used to initialize an object with another object of the same type is known as default copy constructor.  It is by default available in all classes  syntax is ClassName(ClassName &Variable)
  14. 14. Default Copy Constructor - Example class Distance { private: int feet; float inches; public: Distance():feet(0),inches(0.0) { } Distance(int ft, float in): feet(ft), inches(in) { } Distance(Distance &d) { feet = d.feet; inches = d.inches; } void showDistance() { cout<<“Feet = “<<feet; cout<<“Inches=“<<inches; } }; void main() { Distance dist1(11,22.8); Distance dist2(dist1); Distance dist3 = dist1; dist1.showDistance(); dist2.showDistance(); dist3.showDistance(); }
  15. 15. Memberwise Copy  Another different format has exactly the same effect, causing object to be copied member-by- member into other object.  Remember, object1 = object2 will not invoke the copy constructor. However, this statement is legal and simply assigns the values of object2 to object1, member-by-member. This is the task of the overloaded assignment operator (=).
  16. 16. Memberwise Copy - Example class Distance { private: int feet; float inches; public: Distance():feet(0),inches(0.0) { } Distance(int ft, float in): feet(ft), inches(in) { } void showDistance() { cout<<“Feet = “<<feet; cout<<“Inches=“<<inches; } }; void main() { Distance dist1(11,22.8); Distance dist2(dist1); Distance dist3 = dist1; Distance dist4; dist4 = dist1; dist1.showDistance(); dist2.showDistance(); dist3.showDistance(); dist4.showDistance(); }
  17. 17. Returning Objects from Function - Example class Distance { private: int feet; public: Distance():feet(0) { } Distance(int ft): feet(ft) { } Distance addDist(Distance d) { Distance temp; temp.feet = this->feet + d.feet; return temp; } void showDistance() { cout<<“Feet = “<<feet; } }; void main() { Distance dist1, dist3; Distance dist2(11); dist3 = dist1.addDist(dist2); dist1.showDistance(); dist2.showDistance(); dist3.showDistance(); }
  18. 18. The Scope of Variables Global variables are declared outside all functions and are accessible to all functions in its scope. The scope of a global variable starts from its declaration and continues to the end of the program. Local variables are defined inside functions. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable.
  19. 19. The Scope of Variables The data fields are declared as variables and are accessible to all constructors and functions in the class. Data fields and functions can be declared in any order in a class. For example, all the following declarations are the same: class Circle { public: Circle(); Circle(double); private: double radius; public: double getArea(); double getRadius(); void setRadius(double); }; (a) (b) class Circle { private: double radius; public: double getArea(); double getRadius(); void setRadius(double); public: Circle(); Circle(double); }; class Circle { public: Circle(); Circle(double); double getArea(); double getRadius(); void setRadius(double); private: double radius; }; (c)
  20. 20. The Scope of local variables in a class Local variables are declared and used inside a function locally. In a class, if a local variable has the same name as a data field, the local variable takes precedence and the data field with the same name is hidden.

×