CLASSES & OBJECTS
PDPU
1
REVISITING STRUCTURES…
struct book b1, b2 or b3 are
{ objects.
char TITLE [20]; TITLE, AUTHOR , …
char AUTHOR [20]; PRICE are elements of
those objects.
int EDITION;
Identifier book is used
char ISBN[14]; to create variables of
char PUBLISHER[20]; type book.
float PRICE; dot operator is used
}; to access elements of
2 struct book b1, b2, b3; the object.
LIMITATIONS OF C STRUCTURE
STRUCT DATA TYPE IS NOT BUILT-IN
DATA TYPES.
(Arithmetic operations are not directly
possible.
DOES NOT PERMIT DATA HIDING.
Structure members (variables) can be directly
accessed by any function anywhere in their
scope.
Structure members are public members.
3
EXTENSIONS TO STRUCTURES…
C++ expands capabilities of structure to
suit OOP philosophy.
C++ attempts to bring the user-defined
data types as close as possible to the
built-in data types.
Allows mechanism to hide the data.
Allows Inheritance.
4
EXTENSIONS TO STRUCTURES…
In C++, structures can have both variables
and functions as members.
Some of its members will behave as
private so that they cannot be accessed
directly by the external functions.
All these extensions through user defined
data type…class
5
SPECIFYING A CLASS …
A class is a way to bind the data and its
associated functions together.
Allows the data and functions to be
hidden, if necessary, from external use.
6
A CLASS SPECIFICATION…
It has two parts:
Class Declaration
Describes the type and scope of its members.
Class Function Definitions
Describes how the class functions are
implemented.
7
THE GENERAL FORM OF A CLASS
DECLARATION …
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
8 };
THE GENERAL FORM OF A CLASS
DECLARATION …
class is a keyword.
It specifies an abstract data of type
class_name
Class body contains variables and
functions.
Variable and functions are known as class
members.
Class members are grouped under two
9 sections: private & public.
REPRESENTATION OF A CLASS
circle
CLASS: circle
DATA setradius( )
radius
area
calc_area( )
FUNCTIONS
setradius( )
calc_area( )
10
EXAMPLE…
DECLARING A CLASS & ITS MEMBERS
class circle float calc_area(void)
{ {
area = 22.0 / 7 *
private: radius * radius;
int radius; float area; return area;
public: }
void setradius (int r) }; // CLASS ENDS HERE.
{ /* SEMICOLON AT THE
END IS IMPORTANT. */
radius = r;
}
11
EXAMPLE…CREATING OBJECT
void main()
{
int r = 5; circle c1; c1.setradius(r);
cout << c1.calc_area() << endl;
// cout << "The value of radius of c1 is " << c1.radius;
// error: not accessible outside circle.
// cout << setradius(10);
// again an error : setradius ( ) should have prototype.
getch();
12 }; Program object1.cpp
UNDERSTANDING EXAMPLE…
private and public keywords are visibility
labels.
They are followed by a colon.
The class members declared as private can
be accessed only from within the class.
Data hiding is possible because of private
declaration.
public members can be accessed from
13 outside the class.
UNDERSTANDING EXAMPLE…
The use of private keyword is optional.
By default, the members of a class are
private.
Through these member functions only, we
can access the object.
GENERALLY, DATA MEMBERS ARE
DECLARED PRIVATE WHILE MEMBER
14 FUNCTIONS AS PUBLIC.
UNDERSTANDING EXAMPLE…
circle c1; Creates an object of class circle.
c1 Class variable or object.
So, we can say that
C1 is an object of type circle.
15
ANOTHER METHOD OF CREATING
OBJECTS…
class circle
{
…
…
} c1, c2, c3, c[3];
16
GENERAL FORMAT TO ACCESS
CLASS MEMBERS
object-name . function-name (arguments);
e.g.
c1.calc_area( );
c1.radius = 5; // illegal. why?
radius can be accessed by member functions
only and not by object directly.
17
Exercise 1…
Create a class called student_data containing following
information only.
Roll No.,
name,
marks of physics, maths and Chemistry, and
total.
Create one instance from this class.
Accept roll no., name, and marks of three subjects from
the user and calculate total.
Display all the information.
18
Exercise 2…
Create a class to specify data on students
given below.
Roll number, Name, Course Name, Major and
Minor Subjects.
Assume that there are 10 students.
a) Print name of all students.
b) Print the data of a student whose roll number is
given.
19
Exercise 3…
Create a class to specify data of customers in a bank.
The data to be stored is: Account Number, Name,
Balance in account.
Assume maximum 10 customers in the bank.
a) Print the account number and name of each
customer with balance below Rs. 100.
b) If a customer request for withdrawal or deposit, it
is given in the form: Account Number, Amount, Code
( 1 for deposit, 0 for withdrawal)
The function should give a message, " The balance is
20 insufficient for the specified withdrawal ".
DEFINING MEMBER FUNCTIONS
Can be defined in two places:
– Inside the class definition.
– Outside the class definition.
21
DEFINING MEMBER FUNCTION
OUTSIDE THE CLASS DEFINITION
return-type class-name ::function-name(arg)
{
Function Body;
}
:: is scope resolution operator.
:: tells the compiler that the function
function-name belongs to class class-name.
22
DEFINING MEMBER FUNCTION
OUTSIDE THE CLASS DEFINITION
class circle
{
private:
int radius;
float area;
public:
void setradius(int);
float calc_area(void);
23 };
DEFINING MEMBER FUNCTION
OUTSIDE THE CLASS DEFINITION
void circle::setradius(int r)
{
radius = r;
cout << "The value of radius is set to " <<
radius << endl;
area = calc_area();
cout << area << endl;
}
24
DEFINING MEMBER FUNCTION
OUTSIDE THE CLASS DEFINITION
float circle::calc_area(void)
{
area = 22.0/7*radius*radius;
return area;
}
25 Program object2.cpp
MEMORY ALLOCATION FOR
OBJECTS
Earlier we stated that memory space for objects
is allocated when they are declared and not
when the class is specified.
This statement is partially true.
Actually,
The member functions are created and
placed in the memory space only once,
common for all objects.
26
MEMORY ALLOCATION FOR
OBJECTS
Only space for member variables is
allocated separately for each object.
It is essential because all these
variables will hold different data for
different object.
27
OBJECTS OF CLASS CIRCLE
CREATED…
class circle
{
…
…
} c1, c2, c3;
28
MEMORY ALLOCATION FOR
OBJECTS
COMMON FOR ALL OBJECTS
set_radius ( )
calc_area ( )
MEMORY CREATED WHEN
FUNCTIONS DEFINED
c1 c2 c3
radius radius radius
MEMORY CREATED
area area area WHEN
29 OBJECTS DEFINED
OBJECTS AS FUNCTION ARGUMENTS
Like any other data type,
objects can be passed as function argument.
Two ways:
A copy of entire object is passed to the
function. (Pass by value)
Address of the object is transferred to the
function. (Pass by reference)
30
OBJECTS AS FUNCTION ARGUMENTS
… PASSED BY VALUE…
Copy of object is passed.
Changes in the function does not change the
values of transferred object.
Function prototype
Return-type function-name ( class-name );
e.g. void calc_all ( circle);
Here, calc_all is function and circle is the
name of class whose object will be passed to
31 calc_all ( ).
OBJECTS AS FUNCTION ARGUMENTS
… PASSED BY REFERANCE…
Address of object is passed.
Changes in the function changes the values
of object directly.
Function prototype
Return-type function-name ( class-name *);
e.g. void inc_rad ( circle *, int);
Calling function.
e.g. Inc_rad ( &c1, 10);
32 Program obect3.cpp
FRIENDLY FUNCTIONS…
Generally, private members can not be
accessed from outside the class.
However, in certain situations, we would like
to share a particular function between
different classes.
Use keyword friend
It allows the function to access to the private
data of these classes.
Function need not be a member of any of
33 these classes.
FRIENDLY FUNCTIONS.
DECLARATION…
class CLASS-NAME
{
…
…
public:
…
…
friend return-type function-name (arg);
34 }; Program obect4.cpp
FRIENDLY FUNCTIONS.
FEW POINTS…
FUNCTION DECLARATION SHOULD BE
PRECEDED BY THE KEYWORD friend.
The function is declared elsewhere in the
program like a normal c++ function.
The function definition does not use either
the keyword friend or the scope operator ::.
A function can be declared as a friend in any
number of classes.
35
FRIENDLY FUNCTIONS.
FEW POINTS…
Although not a member function, it has full
access rights to the private members of the
class.
Usually, it has the objects as arguments.
Often used in operator overloading.
A friend function can also be called by
reference. (Useful to change the values of
private members) (against the principle of
data hiding).
36
FRIENDLY FUNCTIONS.
FEW POINTS…
Member function of one class can be friend
functions of another class.
Defined using scope resolution operator.
class X class Y
{ {
… …
int func1( ); friend int X::func1();
}; };
37
FRIENDLY FUNCTIONS.
FEW POINTS… (FRIEND CLASS)
We can declare all the member functions of
one class as the friend functions of another
class.
class x class y
{ {
… …
int func1( ); friend class x;
}; };
38
FUNCTIONS
RETURNING OBJECTS…
A function cannot only receive objects as
arguments but also can return them.
39 Program obect5.cpp
SUMMARY…
A class is an extension to the structure data
type.
A class can have both variables and
functions as its members.
By default, members of the class are private
whereas that of structure are public.
Only the member function can have access to
the private data members and private
functions.
40
SUMMARY…
Public members can be accessed from
outside the class.
Objects are class variables.
The public members of any objects can be
accessed with the help of dot operator.
We can define member functions inside or
outside the class.
41
SUMMARY…
The memory space for variables is allocated
separately for each object, but no separate
space is allocated for member functions.
We can use objects as arguments.
Function can return object also.
Friendly function can be shared between
different objects and classes.
42