Class and Object
Class and Object
Classes and
Objects
1
CLASSES
&
OBJECTS
2
INTRODUCTION
A class is a way to bind the data and its associated
functions together.
Data is called Members and the Functions are called
Member Function or Method.
Defining variables of a class data type is known as a
class instantiation and such variables are called objects.
They are syntactically an extension of structures. The
difference is,
Structure all members are public by default.
The keyword “class” specifies user defined data type class name.
The body of a class is enclosed within braces and is terminated by a
semicolon.
The class body contains the declaration of variables and functions.
The class body has three access specifiers(private, public and protected).
4
class class_name
{
private:
variable declaration
function declaration
Syntax protected:
variable declaration
function declaration
public:
variable declaration
function declaration
};
5
E-g;
class student
{ Class name
char name[10];
int rno,m1,m2,tot; Data Members
void accept();
public:void compute();
Methods
}; 6
CLASS OBJECTS
Once if the class specification & implementation is over, the object can be
created out of the class. This is same as creating variables.
Object Creation: class_name object;
One or more objects can be created from a single class.
class_name object1,object2….objectn;
E-g;
//One object //More Objects
student s1; student s1,s2;
s1.accept(); s1.accept();
s1.compute(); s2.compute();
7
ACCESSING CLASS MEMBERS &
FUNCTIONS
The member of a class are accessed using the dot(“.”)operator. This
process is called as sending message to the object.
Accessing Class Members: Syntax: object_name.variable;
E-g: s1.rno=29;
Accessing Member Functions: Syntax: object_name .
memberfunction();
s1.accept();
E-g;
Member Function
Dot operator
Object Name 8
DEFINING MEMBER FUNCTIONS
resolution operator(::).
– Syntax: Return_type class_name::function_name(arguments)
{
Body of function
} 9
Defining Member Functions(Cont)
10
Defining Member Functions(Cont)
EXAMPLE
• Memory space required for the member variables are only allocated
separately for each object.
• Separate memory allocations for the objects are essential because the
member variables will hold the different data values for different objects.
• E-g;
class mycls
{
int a,b;
public:
void put();
};
void main()
{
mycls c1,c2; 13
}
Memory Allocation Of Objects(cont)
• Member functions get() and put() belong to the both object c1 and c2.
• Memory for objects c1 and c2:
14
ARRAY OF OBJECTS
}
FUNCTION PROTOTYPE
return_type function_name(args);
16
Function Prototype(cont)
I. Call by Value
• Call By Value:
In this method the values of the actual parameters (appearing in the function call)
are copied into the formal parameters (appearing in the function definition), i.e.,
18
the function creates its own copy of argument values and operates on them.
Function Calling(Cont)
E-g;
void main()
{
void val(int);
int a=20; // Variable Declaration
val(a); // Function Calling
cout<<“\n Call By Value Invoke:”;
cout<<“\n a=“<<a;
}
void val(int x) // Function Definition
{
x=30;
}
O/P;
Call By Value Invoke:
a=20
19
Function Calling(Cont)
Call by reference:
• In this method, a reference to the actual arguments(s) in the calling
program is passed (only variables).
• So the called function does not create its own copy of original value(s) but
works with the original value(s) with different name.
• Any change in the original data in the called function gets reflected back to
the calling function.
20
Function Calling(Cont)
E-g;
void main()
{
void ref(int *);
int a=20;
ref(&a);
cout<<“\n Call By Ref Invoke:”;
cout<<“\n a=“<<a;
}
void ref(int *x)
{
x=40;
}
O/P;
Call By Ref Invoke:
a=40 21
• Return By Reference: Function Calling(Cont)
• Also a function may return reference.
• A function may return an object or variable that can be used as a value
for an expression.
E-g; void main()
{
int returnref(int *,int *);
int x,y,z;
cout<<“Enter X and Y”;
cin>>x>>y;
z=returnref(x,y);
cout<<“\n Add:”<<z;
}
int returnref(int *a,int *b)
{ Output:
int c; Enter X and Y: 20 5
c=a+b; Add: 25
return(c); 22
}
INLINE FUNCTION
23
Inline Function(Cont)
inline int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void main()
{
int x=25,y=35,z;
cout<<“\n First No:”<<x<<“\n Second No:”<<y;
z=max(x,y) //Inline Function invocation
cout<<“\n Maximum=“<<z;
}
O/P:
First No:25
Second No:35
Maximum:35
24
FRIEND FUNCTION
Class Class
1 2
Friend
Function
E-g; add(f,s);
Friend Function
E-g; ff.a+ff.b;
27
Constant Parameter And Member Function
Constant Parameter:
• A C++ function may have constant arguments. These arguments is treated as
constant. These values cannot be modified by the function.
• For making the arguments constant to a function, we should use the keyword
const.
• E-g; void max(const float x, const float y, const float z);
Constant Member Function:
• Used when that function doesn’t change any attributes of the class.
• Declared with the help of const keyword.
• E-g; void put()const; 28
Thank You 29