lecture 3
TRANSCRIPT
![Page 1: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/1.jpg)
Copyright 2006, The Ohio State University 1
Classes
• Defining Classes– Date Members– Member Functions
• Static vs. Non-static members• Constant functions• Access Control Mechanisms• Constructors & Destructors
![Page 2: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/2.jpg)
Copyright 2006, The Ohio State University 2
Classes & Objects• Four pillars of OOP are achieved from classes
– Encapsulation– Data hiding– Inheritance– Polymorphism
• Class– Consists of both data and functions/methods– Defines properties and behavior of set of entities– Defines a new type (like int, float etc.)
• Object– An instance of class – Has identity, state, and behavior
![Page 3: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/3.jpg)
Copyright 2006, The Ohio State University 3
class Rectangle{
private: int width; int length;public: void set(int w, int l); int area();
};
Classes & Objects
Rectangle r1;Rectangle r2;Rectangle r3;
……
int a;
![Page 4: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/4.jpg)
Copyright 2006, The Ohio State University 4
Defining a class
class class_name { Access_Control_label: members; (data & code) Access_Control_label : members; (data & code)
};
class Rectangle{
private: int width; int length;public: void set(int w, int l); int area();
};
Header
![Page 5: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/5.jpg)
Copyright 2006, The Ohio State University 5
Class Definition – Data Members• Abstracts the general attributes of the entity defined by class• Type can be anything: built-in or user-defined
Non-Static Member Static MemberExistence Local to object – each
object has its own copy
Global to class – all objects share the same copy
Initialization
object level in member functions or constructor
Class level
Access Qualified by object <object name>.<var>
Qualified by class<class name>.<var>• Static data members and functions MUST be defined
somewhere
![Page 6: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/6.jpg)
Copyright 2006, The Ohio State University 6
class Rectangle{
private: int width; int length; static int count;public: void set(int w, int l); int area();}
Static Data Member Rectangle r1;Rectangle r2;Rectangle r3;
widthlength
widthlength
widthlength
r1
r3
r2
count
![Page 7: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/7.jpg)
Copyright 2006, The Ohio State University 7
Class Definition – Member Functions
• Non-static Member Function– Invoked from objects [ object.function() ]– Can access both static and non-static data members
• Static Member Function – Invoked using class [ classname::function() ]– Can only access static data members (but not non-
static)• Constant Member Function
– Does not modify the state of the object !• Const function can be invoked from both const
& non-const objects• Non-const function can be invoked only from
non-const object
![Page 8: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/8.jpg)
Copyright 2006, The Ohio State University 8
Define a Member Functionclass Rectangle{
private: int width, length;public: void set (int w, int l); int area() {return width*length; }
};
void Rectangle :: set (int w, int l){
width = w;length = l;
}
inline
class name
member function name
scope operator
r1.set(5,8);
rp->set(8,10);
![Page 9: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/9.jpg)
Copyright 2006, The Ohio State University 9
Constant Member Functions• const member function
– declaration• return_type func_name (para_list) const;
– definition• return_type func_name (para_list) const
{ … }• return_type class_name :: func_name
(para_list) const { … }– Makes no modification about the data members
(safe function)– It is illegal for a const member function to modify a
class data member
![Page 10: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/10.jpg)
Copyright 2006, The Ohio State University 10
Example of Const Member Functionclass Time{ private :
int hrs, mins, secs ;
public : void Write ( ) const ;
} ;
void Time :: Write( ) const{ cout <<hrs << “:” << mins << “:” << secs << endl;}
function declaration
function definition
![Page 11: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/11.jpg)
Copyright 2006, The Ohio State University 11
Access Control Specifiers• Mechanism to achieve Information Hiding • To prevent the internal representation from direct
access from outside the class• Public
– Can be accessible from anywhere in the program• Private (default)
– Can be accessible only from member functions and friends• Protected
– Acts as public for objects of its own class and derived classes
– Acts as private to rest of the program• Public functions – Class Interface• Private functions – helper functions (can be accessed by
class objects and friends)
![Page 12: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/12.jpg)
Copyright 2006, The Ohio State University 12
class Time Specificationclass Time{
public :
void Set ( int hours , int minutes , int seconds ) ;void Increment ( ) ;void Write ( ) const ;Time ( int initHrs, int initMins, int initSecs ) ; // constructor Time ( ) ; // default constructor
private :
int hrs ; int mins ; int secs ;
} ; 12
![Page 13: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/13.jpg)
Copyright 2006, The Ohio State University 13
Class Interface Diagram
Private data:
hrs
mins
secs
Set
Increment
Write
Time
Time
Time class
![Page 14: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/14.jpg)
Copyright 2006, The Ohio State University 14
What is an object?
OBJECT
Operations
Data
set of methods(public member functions)
internal state(values of private data members)
![Page 15: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/15.jpg)
Copyright 2006, The Ohio State University 15
class Rectangle{
private: int width; int length;public: void set(int w, int l); int area();
};
Declaration of an Object
int main(){
Rectangle r1;Rectangle r2;
r1.set(5, 8); cout<<r1.area()<<endl;
r2.set(8,10);cout<<r2.area()<<endl;
return 0;}
![Page 16: Lecture 3](https://reader036.vdocuments.site/reader036/viewer/2022081900/577cca1b1a28aba711a5629f/html5/thumbnails/16.jpg)
Copyright 2006, The Ohio State University 16
Take Home Message
• Class can be considered as a user-defined data type, while an object is just a variable of certain class.
• There are three parts in the definition of a class: data members, member functions, and access control.