0% found this document useful (0 votes)
6 views29 pages

Class and Object

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

Class and Object

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Presentation slides for

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.

 Class ­all members are private by default. 3


CLASS SPECIFICATION

 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).

• Private : Accessed only from within the class.


• Public : Accessed from outside the class also.
• Protected: Accessed from within the class and the members of the inherited classes.

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

• Member Functions can be defined into two places.


– Outside the class specification
– Inside the class specification

• Define Member Functions Outside The Class Body:


– A member function is defined outside the class by using scope

resolution operator(::).
– Syntax: Return_type class_name::function_name(arguments)
{
Body of function
} 9
Defining Member Functions(Cont)

Define Member Functions Inside The Class Body:


The function is defined along with the function declaration, which is declared inside
the class. When a function is defined inside a class, it is treated as an Inline Function.
Syntax:
class classname
{
//Body of function
};

10
Defining Member Functions(Cont)
EXAMPLE

Outside the class specification Inside the class specification


E-g: class memberoutside E-g: class memberinside
{ {
int a,b; int a,b;
public: public:
void put(); void put();
}; {
void mycls::put () if(a>b)
{ cout<<“A is Big”;
if(a>b) else
cout<<“A is Big”; cout<<“B is Big”;
else }
cout<<“B is Big”; };
} 11
ACCESSING MEMBER FUNCTIONS
WITHIN A CLASS

If a member function can accessing another member function then it is called as


nesting of member functions.
E-g;
int max(int a, int b)
{
if(a>b)
return a;
else
return b;
}
void printmax()
{
cout<<“\n Maximum:”<<max(); // Nesting of member function
} 12
MEMORY ALLOCATION OF OBJECTS

• 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:

Objects Data Members Memory Alloted


c1 a,b 4bytes
c2 a,b 4bytes

14
ARRAY OF OBJECTS

• Once a class is defined, any number of objects can be created.


– E-g; student s1,s2;
• But sometimes it is required to create large number of objects. In that case
array of objects can be used.
• Since array can be easily manipulated with the help of loops this is a
convenient method of handling large number of objects.
• E-g; void main()
{
student a[100]; //100 objects created
for(int i=0;i<100;i++)
a[i].accept();
for(int j=0;j<100;j++)
a[j].compute(); 15

}
FUNCTION PROTOTYPE

• It is nothing but function declaration.


• It is the ordinary function definition appears below the main() function
then provide the function prototype declaration in advance.
• The prototype declaration is requires before the function call is
encountered.
• Syntax:

return_type function_name(args);

16
Function Prototype(cont)

• E-g; void main()


{
void add(int x,int y); //Function Prototype Declaration
int a,b;
cout<<“Enter A and B values:”;
cin>>a>>b;
add(a,b); //Function Call
}
void add(int x,int y) Function Definition
{
cout<<“ADD=“<<(x+y);
} 17
FUNCTION CALLING

• Actual and formal Arguments:

• The data is passed by the calling function as actual arguments


• Formal or dummy arguments are the names of the parameter in the called
function.
• A Function can be called into two ways;

I. Call by Value

II. Call by Reference

• 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

• These are the functions designed to speed up program execution.


• In case of normal function, the compiler have to jump to another location
for the execution of the function and then the control is returned back to
the instruction immediately after the function call statement. So execution
time taken is more in case of normal functions.
• Always inline function starts with keyword “inline”.
• Syntax:
inline function_header
{
body of the 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

 It is a stand-alone function but having rights to access the class members.


 They can be used as a bridge between two different class objects.

Class Class
1 2

Friend
Function

 A friend function must take up the class object as argument.


 It can be friend to more than one class. 25
Class first Class second
{ {
int a; int b;
public: public:
void get() void put()
{ {
cout<<“\n Enter First No:”; Cout<<“\n Enter Second No:”;
cin>>a; cin>>b;
} }
friend void add(first,second); friend void add(first,second);
}; };

Void add(first ff,second ss)


{
Friend cout<<“\n ADD:”<<ff.a+ss.b;
Function(cont) }
Void main()
{
first f;second s;
f.get();
s.put();
26
add(f,s);
}
Friend Function(cont)

• Points to remember while using friend function:


 May be declared under private or public.
 Called without the help of any object.

E-g; add(f,s);
Friend Function

 It can access the member data directly.

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

You might also like