9953330565 Low Rate Call Girls In Rohini Delhi NCR
C++ unit-2-part-3
1. Prepared By: Asst. Prof. Sejal Jadav
Unit-2
Classes and Objects, Constructor and Destructor
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
2. Prepared By: Asst. Prof. Sejal Jadav
B.Inside the class definition
•The actual function definition of
member function can also be inside the
class.
•When function defined inside the class,
it is treated as inline function.
3. Prepared By: Asst. Prof. Sejal Jadav
•Therefore all the restriction and limitation
of inline function apply to that member
function.
•Generally, only small member functions are
defined inside class.
4. Prepared By: Asst. Prof. Sejal Jadav
• An instance of the
class is called as
object.
class item
{
int id; float price;
public:
void getdata(int no, float p) //function definition inside class
{
id = no;
price = p;
}
void disp(){
cout << endl << "Item Code : " << id;
cout << endl << "Item Price : " << price; }
};
main()
{
item tv;
tv.getdata(21,20500.00);
tv.disp();
}
//Example: 2.class.cpp
5. Prepared By: Asst. Prof. Sejal Jadav
Local Class
•A class declared inside a function is
known as a local class in C++ as it is
local to that function.
•An example of a local class is given as
follows.
6. Prepared By: Asst. Prof. Sejal Jadav
#include<iostream>
using namespace std;
void func()
{
class LocalClass {};
}
int main()
{ return 0;}
7. Prepared By: Asst. Prof. Sejal Jadav
•In the above example, func() is a
function and class LocalClass is
defined inside the function. So, it is
known as a local class.
8. Prepared By: Asst. Prof. Sejal Jadav
•A local class name can only be used in its
function and not outside it.
•Also, the methods of a local class must be
defined inside it only. A local class cannot
have static data members but it can have
static functions.
9. Prepared By: Asst. Prof. Sejal Jadav
Example a local class in C++ is :
LocalClass.cpp
10. Prepared By: Asst. Prof. Sejal Jadav
Nested Classes
•Nested class is a class defined inside a
class, that can be used within the scope
of the class in which it is defined.
11. Prepared By: Asst. Prof. Sejal Jadav
•In C++ nested classes are not given
importance because of the strong and
flexible usage of inheritance.
•Its objects are accessed using
"Nest::Display”.
12. Prepared By: Asst. Prof. Sejal Jadav
•Let’s look at the Example: Nested.cpp
13. Prepared By: Asst. Prof. Sejal Jadav
Nesting of Member Functions
•Since C++ provides concept of
encapsulation, a member function of a
class can be called only by an object of that
class using a dot operator.
14. Prepared By: Asst. Prof. Sejal Jadav
•But here is an exception. A member
function can be called by another
member function of the same class.
15. Prepared By: Asst. Prof. Sejal Jadav
•And, therefore, when a member function
is called by another member function, it
is known as nesting of member
function.
•In the example the max() is nested inside
the disp() function.
16. Prepared By: Asst. Prof. Sejal Jadav
•Both the functions are the members of the class
num.
•Note that the max() can also be called directly
from the outside using the object of class num
and using dot operator.
17. Prepared By: Asst. Prof. Sejal Jadav
•Let’s look at the Example:
1. NesteMemberFun.cpp
2. nestedFun.cpp
18. Prepared By: Asst. Prof. Sejal Jadav
Private Member Function
•Normally all the data members are
placed in private section and all the
member functions are placed in public
section so as to provide interface
between private data of class and
outside program.
19. Prepared By: Asst. Prof. Sejal Jadav
•But sometimes, situation may require
that even a functions need to be
hidden from the outside program.
•Some functions which perform serious
tasks should have restricted access.
20. Prepared By: Asst. Prof. Sejal Jadav
•And in such situations a function is defined in
private section.
•A private function cannot be called by using dot
operator and the object of that class.
•A private can only be called by nesting it into
another function that is a member of the same
class.
21. Prepared By: Asst. Prof. Sejal Jadav
• Let’s look at the Example:
1. PrivateMemberFun.cpp