03 Classes & Obj 20-21-Converted
03 Classes & Obj 20-21-Converted
Class
Class is user defined data type which is collection of data member and member functions.
Class can be declared or defined by using keyword ‘class’ followed by ‘class_name’ which is an
identifier.
Syntax to declared or defined class:
class class_name
{
data_type datamem1;
Data members
data_type datamem2;
data_type datamemN;
return_type mem_fun1( );
return_type mem_fun2( ); Member functions
return_type mem_funN( );
};
• By default all the data of class are private therefore this data cannot be accessed outside
the world.
• Multiple objects can be created for the single class whenever required and each object
hold individual record.
• A class provides the blueprints for objects.
• When you define a class, you define a blueprint for an object. That is what an object of
the class will consist of and what operations can be performed on such an object.
class emp
{
int emp_no;
char name[20], add[20]; Data members (Attributes)
float salary;
Object:
• An Object is an instance or variable of class.
• An object can be thought of as an entity which can represent person, place, animal etc.
• Object is such run time entity which holds entire data and functions of class.
• When program is executed, the objects interact with each other by sending messages to
one another. That is due to object message passing is also done.
• There are three properties related with objects:
o Identity: This property of an object that distinguishes it from other objects
o State: This property describes the data stored in the object
o Behavior: This property describes the methods in the object's interface by which
the object can be used
• Multiple objects can be created for the single class whenever required and each object
hold individual record.
Syntax to create object: Class_name object; OR class Class_name object;
Here, class_name is name of the class which is an identifier.
‘Object’ is name of object which is also an identifier.
e.g. For above class ‘emp’ we can create object as follows:
1
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
emp x,y,z;
Here, x, y and z are objects of class ‘emp’
# include<iostream.h>
#include<conio.h>
int x =10;
void main( )
{
int x=50;
clrscr( );
cout<<“Local value=”<<x;
cout<<“\nGlobal value=”<<::x;
getch( );
}
Output:
Local value= 50
Global value= 10
➢ Member functions:
The functions which are declared or defined under class body and which are called with
the help of object of class only such functions are called as ‘Member functions’.
2
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
class add
{
int a,b,c;
void get( )
{
cout<< “Enter any two no:”;
cin>>a>>b;
}
void show( )
{
c = a + b;
cout<< “Addition= ”<<c;
}
};
Note that:
Whenever we define functions inside class body then by default compiler treats that
functions as inline function.
Body of function;
class add
{
int a,b,c;
void get( );
void show( )
};
void add::get( )
{
cout<< “Enter any two no:”;
cin>>a>>b;
}
void add::show( )
{
c = a + b;
cout<< “Addition= ”<<c;
}
Note that:
Whenever we define functions outside class body then compiler does not treats that
functions as inline function and this is major difference between defining functions inside class
body and outside class body.
Inline Function:
• Definition: “The functions having easy and small coding and which are not pushed into
systems stack by the compiler while its calling such functions are called as inline
functions.”
• We know that function calling is one of the most important things in execution of
function body.
• About non-inline functions, whenever function call is made then compiler push that
function in systems stack and then its execution is happen.
But, inline function is such function that does not push into systems stack by
the compiler i.e. inline function calling is replaced by its definition therefore
execution of inline function becomes fast (Due to avoidance of push into stack)
• Characteristics of Inline function:
o Inline functions having easy & small coding.
o An Inline function does not contains any looping statement, switch statement,
static variables.
3
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
Note that:
We know that, when we define functions inside class body then by default compiler treats
that functions as inline function. And hence its definition should be easy & small with no use of
looping, switch statements, static variable etc.
* If forcefully programmer defines its body with looping, switch statements or static variable etc.
then compiler executes that defined functions as normal functions and push it into systems stack
for execution.
Body of function;
class add
{
int a,b,c;
void get( );
void show( )
};
inline void add::get( )
{
cout<< “Enter any two no:”;
cin>>a>>b;
}
inline void add::show( )
{
c = a+ b;
cout<< “\n Addition= “<<c;
}
2) Private:
Private data accessed by member functions and friend functions only.
Private data is not accessed by non member functions (except friend function)
and derived class functions.
3) Protected:
Protected data accessed by member functions, friend functions and also derived
class functions.
Protected data is not accessed by non member functions (except friend function).
Note that:
A Friend function is such a non member function that can access all data i.e. public,
private and protected data easily.
Reference Variable:
We know that, there are three kinds of variable present in C++ language.
1) Value variable:
➢ This type of variable is used to store only value.
➢ It is easy to use but it is less powerful than pointer variable.
➢ Only copy of value of one variable can be assigned to another variable.
➢ Therefore making operation on such variable does not affects on value of
existing variable.
e.g.
int x,y;
x = 10 ;
y = x;
y = 100;
cout<< x;
output:
10
2) Pointer variable:
➢ This type of variable is used to store address of another variable.
➢ It is hard to use (because we use * or & operator along with pointer variable)
but it is powerful than value variable.
➢ It holds address of variable in it.
➢ Therefore making operation on such variable affects on value of existing
variable.
e.g.
int x =10, *y;
y = &x;
*y = 100;
cout<< x;
output:
100
3) Reference variable: (Alias)
➢ This type of variable is used to give alternative name (alias) for existing
variable.
➢ It is easy to use same as value variable and has power of pointer variable.
➢ Therefore making operation on such variable affects on value of existing
variable also.
➢ Syntax for reference variable:
5
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
6
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
7
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
Array of Object:
• We know that class variable (Object) can hold only one record at a time.
• We know that to store multiple values in single variable, we create array.
• As like normal array, we also able to create arrays of class variable (Arrays of Object) to
store multiple records at a time.
Syntax to Create Arrays object:
here;
‘class’ is keyword
‘tag_name’ is class name which is an identifier
‘array_Name’ is name of array’s of object which is an identifier
size1, size2,………, sizeN are integer values which shows number of records stored in
arrays of class.
Following program shows the concept of arrays of object to accept and display two cricketer
records:
void cricket::show( )
{
cout<< “\nPlayer no:”<<pno;
cout<< “\nPlayer name:”<<name;
cout<< “\nPlayer Bat AVG:”<<bat_avg;
cout<< “\nPlayer Ball AVG:”<<ball_avg;
}
void main( )
{
cricket x[2]; // arrays of object
int i;
clrscr( );
for( i= 0 ; i<=1 ; i++)
{
x[i].get( );
}
8
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
In above example two cricketer records are accepted from user and displayed the
accepted records.
Both records are stored in array x at individual index i.e. first record is stored at index 0 and
second record is stored at index 1 following manner:
pno = 54
x[0] name = Rahul
bat_avg = 45.63
ball_avg = 15.36
pno = 99
x[1] name = Sachin
bat_avg = 54.63
ball_avg = 25.36
Also we can access individual elements from above array of object as fallow:
x[0].bat_avg will access 45.63
x[0].ball_avg will access 15.36
x[1].name will access Sachin
x[1].bat_avg will access 54.63
Note:
To access the data member of structure by using its pointer, the
arrow (→ ) operator is used instead of dot ( . ) operator along with pointer object.
Following program shows the concept of pointer to object:
9
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
Nested Class :
• If we define or declare one class inside another class then such a form is called as
“class within class or Nested class”
• Using this facility complex data type can be created.
• Nested class is used to access elements of class which is part of another class.
Syntax for nested class:
class outerclass_name
{
-
-
-
class innerclass_name
{
};
};
}
Syntax to create object of inner class:
outerclass_name::innerclass_name object;
10
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
Default Argument:
❖ We know that, in ‘C’ language to call the function we need to pass every argument.
❖ While calling function, if we don’t pass argument then ‘C’ function can’t be invoked.
❖ But, C++ language supports such a concept of default argument which assigns default
value for the formal parameter of called function if no value passed while calling.
❖ C++ compiler assigns default value to the formal parameter if no parameter passed to
function while calling.
❖ Syntax to declare a function with default argument:
E.g.
void accept(int=40,char[ ]= “Mumbai”);
In above, function declaration accept() function declared with two default values 40 and
“Mumbai”.
While calling accept( ) function, if we don’t pass arguments then default values i.e 40 and
“Mumbai” is automatically assigns to corresponding formal parameters by C++ compiler.
Following program shows the use of default argument concept:
#include<iostream.h>
#include<conio.h>
class concept
{
public:
void get(int=10,char[ ]="Mumbai"); // funct. With default arg
};
void concept::get(int x, char y[ ])
{
cout<<"\n Value= "<<x;
cout<<"\n Name= "<<y;
}
void main()
{
clrscr( );
concept a,b;
a.get( ); //function called without arg.
b.get(50); // function called with one arg.
getch( );
}
OUTPUT:
Value=10
Name= Mumbai
Value=50
Name= Mumbai
Memory Allocation:
Allocation of memory space for particular data is called as “Memory allocation”
Memory allocation is done at two times viz:
1) Compile time memory allocation (Static memory allocation)
2) Runtime memory allocation (Dynamic memory allocation)
Let us see all this in briefly….
1) Compile time memory allocation (Static memory allocation):
Allocation of memory space for particular data at compile time of program is called as
“Compile time” or “Static” memory allocation.
Disadvantages of Compile time memory allocation:
1) In compile time memory allocation there is memory wastage problem.
E.g.
Consider, we declare one array as int x[50];
➢ In above declaration, compiler allocates 100 bytes of memory for 50 integers
of ‘x’ array at compile time.
➢ But, actually at runtime of program we accept and store only 10 integers in
array ‘x’ therefore only 20 bytes of memory we used at runtime.
➢ Thus, at compile time 100 bytes of memory are reserved but at runtime we
use only 20 bytes. What about remaining 80 bytes? i.e. it is wastage.
2) Due to compile time memory allocation for array, we maximum stores elements
equal to the size of array. That is size of array cannot grow at runtime.
11
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
To overcome the drawback of static memory allocation, the concept of dynamic memory
allocation is introduced.
2) Runtime memory allocation (Dynamic memory allocation):
Allocation of memory space for particular data at run time of program is called as
“Runtime” or “Dynamic” memory allocation.
Advantages of Runtime memory allocation:
1) In runtime memory allocation there is no memory wastage problem. This is due
to memory is allocated at runtime.
2) At runtime of program we stores lots of elements in variable because there no fix
size of variable like array.
In C++ language, there are two operators which participated in dynamic memory allocations.
Memory Management operators in C++:
1) ‘new’ operator
2) ‘delete’ operator
Let us see all this in briefly….
1) ‘new’ operator:
➢ ‘new’ operator is unary operator i.e. it operates on only one operand.
➢ ‘new’ operator is used to allocate memory space for particular data at runtime of program.
➢ Syntax for ‘new’ operator:
delete pointer;
12
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
Function overloading:
➢ In C++ language we are able to take or define many functions having same name but
all these functions are differ from each other according to signatures (type of argument
and number of argument accept by function) such concept is called as “Function
Overloading”
➢ “Overloading” means one name having multiple meaning i.e. a function having distinct
multiple meaning.
➢ Function overloading is type of “Compile time polymorphism” i.e. all the overloaded
functions are get selected by the compiler at compile time of program.
➢ At compile time, compiler selects such functions according to their type and number of
arguments.
➢ Consider following function prototype:
void add();
void add(int);
int add(int,int);
In above declaration, add( ) function is overloaded three times and all these functions
are differ from each other according their signatures.
#include<iostream.h>
#include<conio.h> void over::show( )
class over {
{ cout<<"\n Result= "<<c;
int a,b,c; }
public: void main( )
void add(); {
void add(int); clrscr( );
void add(int,int); over z;
void show(); z.add( );
}; z.show( );
void over::add( ) z.add(50);
{ z.show( );
cout<<"\n Enter two no: "; z.add(40,60);
cin>>a>>b; z.show( );
c=a+b; getch( );
} }
void over::add(int x)
{ OUT PUT:
cout<<"\nEnter one no: "; Enter two no: 70 80
cin>>b; Result= 150
c=x+b; Enter one no: 40
} Result= 90
void over::add(int x,int y) Result= 100
{
c=x+y;
}
13
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
➢ Friend function:
• We know that by default all the data of class is private therefore such type of data is not
accessed by non-member function.
• But in some situation we have to access such private data inside non member function.
For that purpose friend functions are used.
• Definition: “The non member function that has ability to access all kind of data (public,
private and protected) of class such function is called as ‘Friend function’
• Characteristic of friend function:
1) The scope of friend function is unlimited i.e. we can declare it in any section
(public or private or protected) of class.
2) In side friend function, data of class only accessed by using object of class.
3) Generally, friend function accept object of class as argument.
4) Friend function cannot define inside class body. Because it is non-member
function.
5) Friend functions are defined outside the class body like normal function i.e.
there is no need of class name or scope resolution operator (::) etc.
6) Friend functions are not accessed or called with the help of object of class
because it is non member function.
7) Friend functions are not considered class members; they are normal external
functions that are given special access privileges of class.
8) ‘friend’ keyword is used to declare a function as friend.
• Syntax to declare friend function:
Here,
‘friend’ is keyword that tells compiler the declared function is friend.
‘return_type’ is any valid data type in C++ that shows value return from function.
‘fun_name’ is name of friend function.
14
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
#include<iostream.h>
#include<conio.h>
class company; //advance declaration
class emp
{
int empno;
char name[20];
float sal;
friend void show(emp,company); //friend fun. declaration
public:
void eget( );
};
class company
{
char cname[20];
char cadd[20];
friend void show(emp,company); //friend fun. declaration
public:
void cget( );
};
void emp::eget( )
{
cout<<"\n Enter emp no: ";
cin>>empno;
cout<<"\n Enter emp name: ";
cin>>name;
cout<<"\n Enter emp Salary: ";
cin>>sal;
}
void company::cget( )
{
cout<<"\n Enter Company Name: ";
cin>>cname;
cout<<"\n Enter Company Address: ";
cin>>cadd;
}
void show(emp x, company y) //friend fun. definition
{
cout<<"\n Emp No= "<<x.empno;
cout<<"\n Emp Name= "<<x.name;
cout<<"\n Emp Salary="<<x.sal;
cout<<"\n Company Name="<<y.cname;
cout<<"\n Company Address="<<y.cadd;
}
void main( )
{
clrscr( );
emp p;
company q;
p.eget( );
q.cget( );
show(p,q); //call to friend fun.
getch( );
}
15
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
➢ Friend Class:
• As like friend function, we are also able to declare a class as ‘friend’ of another class.
• If ‘first’ class declared, ‘second’ class as my friend then all the data of ‘first’ class can
easily accessed by the member function of ‘second’ class.
• But the data of ‘first’ class is accessed by member functions of ‘second’ class by using
only object of ‘first’ class.
• The friend class is declared in any section of class either in public, protected or private
• Friend class is declared by using the ‘friend’ keyword.
• In short, If class ‘A’ declares class ‘B’ is my friend then the member functions of class
‘B’ access all the data of class ‘A’.
• Syntax:
Here,
‘friend’ is keyword which is used to declare a friend class.
‘class’ is also keyword.
‘class_name’ is name of class which is an identifier.
Following program shows the use of friend class:
#include <iostream.h>
#include<conio.h>
class A
{
// ‘A’ declares ‘B’ as a friend... therefore A class data is accessed in B
int a,b,c;
friend class B; // declaration of friend class
public:
void get( );
};
class B
{
public:
void all(A);
};
void A::get( )
{
cout<< “\nEnter two no” ;
cin>>a>>b;
}
void B::all(A z)
{
z.c = z.a + z.b;
cout<<”\n Addition=”<< z.c;
}
void main( )
{
clrscr( );
A p;
B q;
p.get( );
q.all(p);
getch( );
}
16
B.Sc.II (Sem-iii) Year: 2020 OOPS lalita bhosale
Assignment: 2
1. What is class and object? How they differ from each other?
2. Define: “Member function” and “Non member function”
3. In how many ways we define member function class? Write difference between them.
4. What is inline function? Write its characteristics and advantage. Write a program to
make outside define function as inline.
5. What is access specifier (Visibility mode)? Explain all accessibility in details
6. What is array of object? Explain with example.
7. What is pointer to object? Explain with example.
8. What is nesting of class? Explain with example.
9. What is variable? Explain types of variable in C++ language.
10. Explain different parameter passing techniques in C++ language.
11. What is default argument? Explain its advantage with one example.
12. Write a program which demonstrates the function accepting and returning object.
13. What is dynamic memory allocation? How it is done in C++ language?
14. What is function overloading? List out its characteristics with one example.
15. What is friend function? List out its characteristics with one example.
16. Explain in details “Friend class”
17. Write the difference between friend function and member function.
18. Write a program that shows use of single friend function for multiple classes.
19. Write a program that demonstrate passing object as pass by value, pass by pointer and
pass by reference.
17