CLASS OBJECTS CONSTRUCTOR DESTRUCTOR Object Oriented Programming COMSATS

CLASS OBJECTS, CONSTRUCTOR & DESTRUCTOR Object Oriented Programming COMSATS Institute of Information Technology

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 outside int pages; the class float price; – Primary mechanism to hide data is to public: make it private void change. Name(char *n){ – Private data and function can only be strcpy(name, n); Class access from within the class } Definition void change. Pages(int p){ pages = p; } void change. Price(float p){ price = p; } void display(){ cout<<"name = "<<name<<" pages = "<<pages<<" price = "<<price<<endl; } }; 2
![CLASS DATA The class book contain three data items char Object Oriented Programming name[15]; CLASS DATA The class book contain three data items char Object Oriented Programming name[15];](http://slidetodoc.com/presentation_image_h2/9b87051e47aa9c25086656704e0a24ff/image-3.jpg)
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

MEMBER FUNCTION These functions are included in a class There are four member functions in class book *n) change. Pages(int p) change. Price(float p) display() There functions are followed by a keyword public, so they can be accessed outside the class 4 Object Oriented Programming change. Name(char

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

DEFINING OBJECTS The first statement in main() void main(){ book b 1; defines an objects, b 1. change. Name("Operating System"); of class book b 1. change. Pages(500); Remember that the definition b 1. change. Price(150. 56); of the class book does not b 1. display(); } 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.

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 b 1; ) 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

CALLING MEMBER FUNCTIONS • The next four statements in main() call the member function b 1. change. Name("Operating System"); – b 1. change. Pages(500); – b 1. change. Price(150. 56); – b 1. 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 – change. Name("Operating System"); because a member function is always called to act on a specific object, not on the class in general 8 Object Oriented Programming •

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

MESSAGE Object Oriented Programming Some object-oriented languages refer to calls to member functions as messages. Thus the call b 1. display(); can be thought of as sending a message to s 1 telling it to show its data. 10

EXAMPLE PROGRAMS Object Oriented Programming Distance as object 11

Object Oriented Programming 12

Object Oriented Programming 13

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

Object Oriented Programming A COUNTER EXAMPLE 15

Object Oriented Programming 16

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 c 1; //every time we do this, c 1. zero_count(); //we must do this too 17

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 c 1, c 2; 18

CONSTRUCTOR NAME First, constructor name must be same as the name of class This is one way the compiler knows they are constructors. Second, no return type is used for constructors Object Oriented Programming Why 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

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

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. – some. Class() : m 1(7), m 2(33), m 2(4) ←initializer list Object Oriented Programming Counter() : count(0) { } {} 21

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

CONSTRUCTOR (CONT…) Constructor with no argument Constructor with one argument Object Oriented Programming class Circle { private: double radius; public: Circle(); Circle(int r); void set. Radius(double r); double get. Diameter(); double get. Area(); double get. Circumference(); }; 23

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

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

DESTRUCTORS (CONT…) destructor Object Oriented Programming class Circle { private: double radius; public: Circle(); ~Circle(); void set. Radius(double r); double get. Diameter(); double get. Area(); double get. Circumference(); }; 26
- Slides: 26