0% found this document useful (0 votes)
42 views

Classes and Objects

Classes allow grouping of related data and functions together. A class defines the type of an object and the scope of its members. Objects are instances of a class that allocate memory at runtime. Member functions can access private members of a class. Functions can be defined inside or outside the class. The inline keyword requests to expand function calls inline to reduce overhead.

Uploaded by

Santosh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Classes and Objects

Classes allow grouping of related data and functions together. A class defines the type of an object and the scope of its members. Objects are instances of a class that allocate memory at runtime. Member functions can access private members of a class. Functions can be defined inside or outside the class. The inline keyword requests to expand function calls inline to reduce overhead.

Uploaded by

Santosh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Introduction to

classes and
objects
What is class
A class is an user defined data type.
It contains the data variables and functions.
It is a way to bind the data and its associated
functions together.
It allow data and functions to be hidden from
outside world, if necessary.
The class declaration describes the type and
the scope of its members.
Continue
Class definition:
class class_name
{
public:
Variable declaration;
Function declaration;
private:
Variable declaration;
Function declaration;
};
Visibility modes
Public:
When any member (variable or function) is
defined as public then it is accessible from the out
side the class in which it is defined as well as from
the members of its class.
Private:
When any member (variable or function) is
defined as private then it is accessible only within
class in which it is defined and not from out side
the class.
Continue
By default the members of a class are private.
This type of a class is completely hidden from
the out side world.
The variables declared inside a class are called
member variables.
The functions declared inside a class are called
member functions.
Member functions are allowed to access the
private member variables of a class.
Continue
Simple class example
class item
{
private:
int num;
float cost;
public:
void getData(int a, float b);
void putData();
};
Creating objects
Objects are the variables of class data type.
Objects are declared inside the main()
function body.
Class declaration define only what it contain.
For example:
item x;
It creates a variable x of class item type and in c++ it
is called object of class.
Continue
We can create any number of object of a class
type. For example:
Item x,y,z,t,p;
Objects are also called an instance of a class.
Class declaration does not create any memory
space for the objects.
When objects are created, necessary memory
is created for that object.
Continue
Objects can also be created when class is defined by
placing their names immediately after the closing
brace such as:
class item
{
private:
int num;
float cost;
public:
void getData(int a, float b);
void putData();
} x, y, z, t, p;
Accessing class members
Member functions are accessed through
objects of class with dot operator (.), it has the
following form:
object-name.function-name(actual arguments);
For example:
x.getData(25,45.67);
x.showData();
Defining member function

Member function can be defined in two ways:

1. Inside the class definition


2. Outside the class definition
Inside the class definition
class item
{
int number; //private by default
float cost; //private by default
public:
void getData(int a, float b); //fun declaration
void showData() //fun defined inside the class
{
cout<<number=<<number<<\n;
cout<<cost=<<cost<<\n;
}
};
Outside the class definition
Member functions are defined outside the
class with the help of scope resolution
operator (::)
This label (::) tells the compiler which class the
function belongs to.
General form is:
return-type class-name:: fun-name (arguments)
{
function body
}
Example..
void item::getData (int a, float b)
{
number=a;
cost=b;
}
And
void item::showData()
{
cout<<number=<<number<<\n;
cout<<cost=<<cost<<\n;
}
void add::getData(int a,int b)
#include<iostream>
{
Using namespace std;
x=a;
class add
y=b;
{
z=a+b;
private:
}
int x,y,z;
main()
public:
{
void getData(int a,int b);
int a,b;
void showData()
cout<<"Enter the numbers";
{
cin>>a>>b;
cout<<"the sum is="<<z;
add a1;
}
a1.getData(a,b);
};
a1.showData();
}
Reference variable
This is a new kind of variable introduced in
c++.
It provides an alias (alternate name) for
previously defined variable.
Its general form is:
data-type & reference-name = variable-name
It must be initialized at the time of declaration. Here
& is not as an address operator but it refer to its
data type.
Continue
For example:
float total = 100.00;
float & sum = total;
Here sum is an alternative name of variable total
which is already declared and initialized.
Both refers to the same data object in the
memory.
Now the statement total=total+20 will change the
value of both variable total and sum to 120.00
And sum = 0 will change the value of both
variable to 0.
Call by reference
The major application of reference variable is
in passing arguments to functions.
For example:
void swap (int &x, int &y);
When we pass arguments by reference, the
formal arguments in the called function
become aliases to the actual arguments in the
calling function.
Actual parameters and formal parameters
Continue
The call by reference method of passing
arguments to a function copies the reference
of an argument into the formal parameter.
Inside the function, the reference is used to
access the actual argument used in the call.
This means that changes made to the
parameter affect the passed argument.
Difference between call by value and
call by reference
Program to swap two numbers using call
by reference
class swap void main()
{ {
public: clrscr();
void swapData(int &x, int &y); int a=5,
}; int b=10;
void swap :: swapData(int &x,int &y) cout<<"Before swapping a =
"<<a<<" b = "<<b<<endl;
{
int t; swap s1;
t=x; s1.swapData(a,b);
x=y;
y=temp; cout<<After swapping a =
"<<a<<" b = "<<b<<endl;
}
}
Inline function
Every time a function is called, it takes a lot of extra
time in executing a series of instructions for tasks such
as
jumping to the function
Saving registers
Pushing arguments into the stack and
Returning to the calling function
All the above operation are performed even the
function is small in size, substantial percentage of time
may be spent in such overheads.
An inline function is a function that expanded in line
when it is executed or invoked.
It means that the compiler simply replaces the function
call with corresponding function code.
Continue
The general form of inline function is:
inline function-header
{
function body
}
For example:
inline double cube (double a)
{
return (a*a*a);
}
Continue
It is very easy to define a function inline.
Only we need to add inline keyword as a prefix
to the function definition.
All inline functions must be defined before the
are called.
Make only those function inline which are
small in size (such as one or two line).
Remember that the inline keyword merely
sends a request, not a command, to the
compiler.
Continue
The compiler may ignore this request if the
function definition is too long or too
complicated and compile the function as a
normal function.
Some of situations where inline expression
may not work:
For functions returning values, if a loop, a switch or a
goto exists.
For functions not returning values, if a return statement
exists.
If function contain static variables.
If inline functions are recursive
Example..
#include<iostream>
using namepace std;

inline float mul (float x, float y)


{ return (x*y);}
inline double cube (double a)
{ return (a*a*a);}
void main()
{
cout<<mul (12.4,10.5);
cout<<cube (15.7);
}
Making an out side function inline
We can define a function outside the class and
still we can make it inline by just using the
qualifier inline in the header line of function
definition. Such as:
Class ABC
{
int x, y;
public:
void getdata (int a, int b);
};
inline void ABC :: getdata (int a, int b)
{
x=a;
Y=b;
}

You might also like