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
Class
• Definition
• The class is an abstract data type which binds data
and its associated functions together.
• Class is a blue print which reflects the entities
attributes and actions. Technically defining a class
is designing an user defined data type.
3. Prepared By: Asst. Prof. Sejal Jadav
• A class is a new abstract data type.
(info. Hide Ex: Student, Employee)
• A class specification has two parts
• Class declaration
• Class function definition
4. Prepared By: Asst. Prof. Sejal Jadav
•Class declaration that describe the type and
scope of its members.
•Class function definition that describe how the
class functions are implemented.
•The general form of a class declaration is as
following
5. Prepared By: Asst. Prof. Sejal Jadav
The wrapping up of
data and functions
into a single unit,
called class, is
known as
encapsulation.
The process of binding the data
and the functions acting on the
data together in an entity
(class) called as encapsulation.
6. Prepared By: Asst. Prof. Sejal Jadav
•The class declaration is similar to struct
declaration.
•The class body contains the declaration of
variables and functions that are, usually,
grouped in two sections namely ‘private’
and ‘public’.
#3. Details
7. Prepared By: Asst. Prof. Sejal Jadav
•The keywords ‘private’ and ‘public’
are known as visibility labels. And
they are followed by a colon [ : ]
sign.
8. Prepared By: Asst. Prof. Sejal Jadav
•The private members can be
accessed only within the class while
public members can be accessed
from outside also.
9. Prepared By: Asst. Prof. Sejal Jadav
•Here private keyword is optional
because the members are private by
default.
10. Prepared By: Asst. Prof. Sejal Jadav
•The variables declared inside the class
are also known as data members
(Attributes, fields, properties) and the
functions (methods, procedures,
actions, operations, services) are
known as member functions.
11. Prepared By: Asst. Prof. Sejal Jadav
•Thus binding of data and
functions together into a single
class-type variable is known as
encapsulation.
12. Prepared By: Asst. Prof. Sejal Jadav
Abstraction refers to
hiding the internal
implementation and
exhibiting only the
necessary details.
Class Person //class declaration
{
char name[10]; //attribute are by default private
int age;
public:
void get();
void disp();
//member function declaration
};
13. Prepared By: Asst. Prof. Sejal Jadav
• Data members are usually declared as
private and member function are
declared as public.
• The class name [here person] can be
used to declare instances [objects] of
that class type.
14. Prepared By: Asst. Prof. Sejal Jadav
•In above example person class has two
private members name, and age.
•Let’s look at the example: 1.class.cpp
15. Prepared By: Asst. Prof. Sejal Jadav
Define member functions
A. Outside the class definition
•The member functions declared
inside the class need to be defined
separately outside of the class.
16. Prepared By: Asst. Prof. Sejal Jadav
•The important difference in defining a
normal function and member function is
that, a member function definition
includes a membership identity label in
the function header.
17. Prepared By: Asst. Prof. Sejal Jadav
Syntax
return_type class_name : : function_name (argument_list)
{
Function body;
}