Oop lec 5-(class objects, constructor & destructor)


Published on

Published in: Education, Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Oop lec 5-(class objects, constructor & destructor)

  1. 1. CLASS OBJECTS, CONSTRUCTOR & DESTRUCTOR Object Oriented Programming COMSATS Institute of Information Technology
  2. 2. Book Class Object Oriented Programming #include <iostream.h> #include <string.h> • Data Hiding class book{ – Data is concealed within the class private: char name[25]; – Cannot be access by function int pages; outside the class float price; – Primary mechanism to hide data public: is to make it private void changeName(char *n){ – Private data and function can strcpy(name, n); Class only be access from within the } Definition class void changePages(int p){ pages = p; } void changePrice(float p){ price = p; } void display(){ cout<<"name = "<<name<<" pages = "<<pages<<" price = "<<price<<endl; } }; 2
  3. 3. CLASS DATA  The class book contain three data items  char Object Oriented Programming name[15];  int pages;  float price; There can be any number of data members in a class just as in structure  There data member lie under keyword private, so they can be accessed from within the class, but not outside  3
  4. 4. MEMBER FUNCTION These functions are included in a class  There are four member functions in class book  *n)  changePages(int p)  changePrice(float p)  display()  Object Oriented Programming  changeName(char There functions are followed by a keyword public, so they can be accessed outside the class 4
  5. 5. CLASS DATA AND MEMBER FUNCTION Function are public and data is private  Data is hidden so that it can be safe from accidential manipulation  Functions operates on data are public so they can be accessed from outside the class  Object Oriented Programming 5
  6. 6. DEFINING OBJECTS The first statement in void main(){ book b1; main() book b1; defines an b1.changeName("Operating System"); objects, b1 of class book b1.changePages(500);  Remember that the b1.changePrice(150.56); definition of the class book b1.display(); } does not create any objects.  Object Oriented Programming • The definition only describes how objects will look when they are created, just as a structure definition describes how a structure will look but doesn’t create any structure 6 variables.
  7. 7. CONT. Defining an object is similar to defining a variable of any data type: Space is set aside for it in memory e.g. int x;  Defining objects in this way (book b1;) means creating them, also called instantiating them  An object is an instance (that is, a specific example) of a class. Objects are sometimes called instance variables.  Object Oriented Programming 7
  8. 8. CALLING MEMBER FUNCTIONS • The next four statements in main() call the member function b1.changeName("Operating System"); – b1.changePages(500); – b1.changePrice(150.56); – b1.display(); – • • don’t look like normal function calls This syntax is used to call a member function that is associated with a specific object It doesn’t make sense to say – changeName("Operating System"); because a member function is always called to act on a specific object, not on the class in 8 general Object Oriented Programming •
  9. 9. CONT. To use a member function, the dot operator (the period) connects the object name and the member function.  The syntax is similar to the way we refer to structure members, but the parentheses signal that we’re executing a member function rather than referring to a data item.  The dot operator is also called the class member access operator.  Object Oriented Programming 9
  10. 10. MESSAGE  Object Oriented Programming Some object-oriented languages refer to calls to member functions as messages. Thus the call b1.display(); can be thought of as sending a message to s1 telling it to show its data. 10
  11. 11. EXAMPLE PROGRAMS Object Oriented Programming Distance as object 11
  12. 12. Object Oriented Programming 12
  13. 13. Object Oriented Programming 13
  14. 14. CONSTRUCTORS The Distance example shows two ways that member functions can be used to give values to the data items in an object • It is convenient if an object can initialize itself when it’s first created, without requiring a separate call to a member function • Automatic initialization is carried out using a special member function called a constructor. • A constructor is a member function that is executed automatically whenever an object is created. Object Oriented Programming • 14
  15. 15. Object Oriented Programming A COUNTER EXAMPLE 15
  16. 16. Object Oriented Programming 16
  17. 17. AUTOMATIC INITIALIZATION An object of type Counter is first created, we want its count to be initialized to 0  We could provide a set_count() function to do this and call it with an argument of 0, or we could provide a zero_count() function, which would always set count to 0.  Such functions would need to be executed every time we created a Counter object  Object Oriented Programming Counter c1; //every time we do this, c1.zero_count(); //we must do this too 17
  18. 18. CONT. A programmer may forget to initialize the object after creating it  It’s more reliable and convenient to cause each object to initialize itself when it’s created  In the Counter class, the constructor Counter() is called automatically whenever a new object of type Counter is created  Thus in main() the statement  creates two objects of type Counter. As each is created, its constructor, Counter(), is executed.  This function sets the count variable to 0. Object Oriented Programming Counter c1, c2; 18
  19. 19. CONSTRUCTOR NAME  First, constructor name must be same as the name of class  This Second, no return type is used for constructors  Why Object Oriented Programming  is one way the compiler knows they are constructors. not? Since the constructor is called automatically by the system, there’s no program for it to return anything to; a return value wouldn’t make sense.  This is the second way the compiler knows they are constructors. 19
  20. 20. INITIALIZER LIST One of the most common tasks a constructor carries out is initializing data members  In the Counter class the constructor must initialize the count member to 0.  The initialization takes place following the member function declarator but before the function body.  Initialization in constructor’s function body  Object Oriented Programming Counter() { count = 0; } this is not the preferred approach (although it does work). 20
  21. 21. CONT. • It’s preceded by a colon. The value is placed in parentheses following the member data. • If multiple members must be initialized, they’re separated by commas. – someClass() : m1(7), m2(33), m2(4) ←initializer list initializer list Object Oriented Programming Counter() : count(0) { } {} 21
  22. 22. CONSTRUCTOR  Constructor:  Public  Function overloading Object Oriented Programming function member  called when a new object is created (instantiated).  Initialize data members.  Same name as class  No return type  Several constructors 22
  23. 23. CONSTRUCTOR (CONT…) Constructor with no argument Constructor with one argument Object Oriented Programming class Circle { private: double radius; public: Circle(); Circle(int r); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; 23
  24. 24. DESTRUCTORS  You might guess that another function is called automatically when an object is destroyed. This is indeed the case. Such a function is called a destructor Object Oriented Programming The most common use of destructors is to deallocate memory that was allocated for the object by the constructor class Foo { private: int data; public: Foo() : data(0) //constructor (same name as class) {} ~Foo() //destructor (same name with tilde) {} }; 24
  25. 25. DESTRUCTORS  Destructors member function  Same name as class  Preceded with tilde (~)  No arguments  No return value  Cannot be overloaded  Before system reclaims object’s memory  Reuse memory for new objects  Mainly used to de-allocate dynamic memory locations Object Oriented Programming  Special 25
  26. 26. DESTRUCTORS (CONT…) destructor Object Oriented Programming class Circle { private: double radius; public: Circle(); ~Circle(); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; 26