02. classes and objects
TRANSCRIPT
-
7/31/2019 02. Classes and Objects
1/22
1
Abdul Wahab [email protected]
University of Science and Technology Bannu
C++ObjectOrientedProgrammingUsing
-
7/31/2019 02. Classes and Objects
2/22
2
Object
Any thing which has some properties and some behaviors.
Object is an instance of a class.
-
7/31/2019 02. Classes and Objects
3/22
3
Class
A class is group of objects having identical properties, common behavior
and shared relationship.
Class: Car
Properties: Company, Model, Color and Capacity etc.
Action: Speed() and Break().
Class: Computer
Properties: Brand, Price, Processor Speed and Ram etc.
Action: Processing(), Display() and Printing().
The entire group of data and code of an object can be built as a user-defined data type using class.
-
7/31/2019 02. Classes and Objects
4/22
4
Access Specifiers
Private:
The private members of a class are accessible only by the memberfunctions or friend functions of the class.
Public:The public members are accessible from any where in the program.
Protected:
The protected members of a class are accessible by the memberfunctions of the class and the classes which are derived from this class.
-
7/31/2019 02. Classes and Objects
5/22
5
General Form of a Class
classclass_name
{
private:
//private data and functions
public:
//public data and function
}object_list;
-
7/31/2019 02. Classes and Objects
6/22
6
Declaring Objects
Defining objects of class is known as class instantiation
When objects are created only during that moment, memory is allocatedto them
intx, y;
charch;
itema, b, *c;
-
7/31/2019 02. Classes and Objects
7/22
7
Properties of Objects
It is individual
It holds data as well as operation method that handles data
Its scope is limited to the block in which it is defined
-
7/31/2019 02. Classes and Objects
8/22
-
7/31/2019 02. Classes and Objects
9/229
Accessing Class Members
Object can access public member variables and functions by usingoperators dot( . ) and arrow ( -> )
[object name] [operator] [member name]
e.g. a.show(); Where a is normal object
b->show(); Where b is pointer object
-
7/31/2019 02. Classes and Objects
10/2210
Example
classitem
{
int codno;
float price;
int qty;
};
void main()
{
classitem a;
clrscr();
a.codeno=123; // Direct
a.price=150.00; // Access
a.qty=15; // Not allowed
}
Optional
-
7/31/2019 02. Classes and Objects
11/2211
The Public Keyword
The keyword publiccan be used to allow object to access the membervariables of a class directly
Public is written inside the class terminated by colon. e.g.
class Sample
{
public:
// Public Members
};
-
7/31/2019 02. Classes and Objects
12/2212
Example
classitem
{
public:
int codno;
float price;int qty;
};
void main()
{
classitem a;
clrscr();
a.codeno=123;a.price=150.00;
a.qty=15;
}
-
7/31/2019 02. Classes and Objects
13/2213
The Private Keyword
It is used to prevent direct access to members of the class
By default private access specifier is used
Private members can be accessed through the public member function
-
7/31/2019 02. Classes and Objects
14/2214
Example
void item :: set_show()
{codeno=123;
price=150.00;
qty=15;
cout
-
7/31/2019 02. Classes and Objects
15/2215
Output
123150.00
15
-
7/31/2019 02. Classes and Objects
16/2216
The Protected Keyword
Access mechanism of protected keyword is same as private keyword
It is used in inheritance
-
7/31/2019 02. Classes and Objects
17/2217
Access Mechanism
Access Specifier Member Functions Class Objects
Public Allowed Allowed
Private Allowed Not Allowed
Protected Allowed Not Allowed
-
7/31/2019 02. Classes and Objects
18/2218
Defining Member Functions
The member functions must be declared inside the class. They can bedefined:
In Public or Private section of a class
Inside or Outside the class
-
7/31/2019 02. Classes and Objects
19/2219
Member Function Inside the Class
A. In Public Section:
class item{int codno;
float price;
int qty;
public:
void show()
{
codeno=125;
price=150.00;
qty=200;
cout
-
7/31/2019 02. Classes and Objects
20/2220
Member Function Inside the Class
B. In Private Section
class item{int codno;
float price;
int qty;
void values()
{
codeno=125;price=150.00;
qty=200;
}
public:
void show()
{
values();
cout
-
7/31/2019 02. Classes and Objects
21/2221
Member Function Outside the Class
class item{
int codno;float price;
int qty;
public:
void show();
};
void item :: show()
{
codeno=125;
price=150.00;
qty=200;cout
-
7/31/2019 02. Classes and Objects
22/22
Have a Nice Day!