Class and object
Class and object
Kashiram Pokharel
Class
• A class is a collection of objects sharing common properties and relationship.
• It is a user defined data type and behave like built-in type of programming language. specifies what data
and what functions will be included in object of that class i.e. They allow variables and methods to be
isolated to specific objects instead of being accessible by all parts of the program.
• A Class is a user defined data-type which has data members and member functions. The data
components of the class are called data members and the function components are called member
functions.
• encapsulates data (attributes) and functions (behavior) into a single unit called classes. i.e Encapsulates
both its data and associated functions together
• Data members are the data variables and member functions are the functions used to manipulate these
variables and together these data members and member functions defines the properties and behavior
of the objects in a Class
• The data and functions of a class are intimately tied together. Class is a blueprint of real world objects. A
programmer can create any number of objects of the same class.
• Classes have the property of information hiding. It allows data and functions to be hidden, if necessary,
from external use. Classes are also referred to as user-defined data types.
• n C++, class is a group of similar objects. It is a template from which objects are created
• A class definition starts with the keyword class followed by the class name; and the class body, enclosed
by a pair of curly braces. A class definition must be followed by a semicolon.
Class: Declaration syntax
• syntax: • a class is a way to bind data and associated function together.
class class_name • it allows data and function to hidden if necessary for external use.
{ • defining a class means creating a user defined data type that
access-specifier: behaves as built in data.
variable declarations; • once a class has been declared we can create any number of
function declarations; object belonging to that class.
………………………………. • class contains both data and function . Data are called data
access-specifier: member and function are called member function.
variable declarations; • Generally data and function are defined in private section are not
function declarations; accessible out of the class.
………………………………… • data and function placed in public section are only accessible from
access-specifier: outside the class.
variable declarations; • the word private is optional . the data in function of class by
function declarations; default private.
………………………………..
};
Class Declaration:
Example:
class student
{
int rollno;
float marks;
public:
void insertdata(int a,float b);
void displaydata(void);
};
Access specifier/visibility level/access modifier:
• These specifiers control the access of the class members within the class. The
specifiers can be public, protected, and private.
• A class prevents the access of the data from the outside world using access
specifiers. It can set permissions to restrict the access of the data.
• Access specifiers define how the members (attributes and methods) of a class
can be accessed.
• Provides property of information hiding. It allows data and functions to be
hidden, if necessary, from external use.
Access specifier
• Private:
• When the private keyword is used to define a function or class, it becomes private.
• Private data and functions can only be accessed from within the member functions of that
class. However, friend classes and friend functions can access private members.
• By default all the members of a class would be private.
• Public:
• The public keyword, on the other hand, makes data/functions public. These are accessible
from outside the class.
• Can set and get the value of public variables without any member function
• Protected
• Protected access modifier is similar to that of private access modifiers, the difference is that
the class member declared as Protected cannot be accessed outside the class however they
are accessible by any derived class or subclass of that class.
• The main difference between private and protected is that protected inheritance allows
continued access to base class members whereas private inheritance prohibits the access of
those members.
Visibility modes:
Some example to create a class:
• Create a class which holds the information of student in a class and
operation which process that information
• Create a class to hold the page no , name, price and member function
which process there information about the book
• Create a class which holds hour, minute and second as a data and a
function to calculate time gap between two time period.
OBJECT:
• An object is the basic runtime entities in the OOPL, which have certain attribute
and behavior
• The attribute represent the data in a program, they have certain specific values
• behavior represents the method (function) which operate on that data
• Objects in C++ are analogous to real-world entities .An object is an instance of
class.
• It is the data item for user defined data type (class name) or variable of type class
• object oriented languages is combine both data and the function into a single
unit that operate on that data. such a unit is called an object
• When a class is defined, no memory is allocated but when it is instantiated (i.e.
an object is created) memory is allocated
• Declaration syntax:
class_name objects_name(variable name)
Defining Member functions:
• There are 2 ways to define a member function:
• Inside class definition
• Outside class definition
Defining Member Functions: Inside the class definition
• Replace function declaration by actual class student
function definition inside the class {
int roll_number;
• When a function is defined inside a float marks;
class, it is treated as an inline function. public:
void insertdata(int a, float b); //declaration
• Therefore, all the restrictions and //inline function
limitations that apply to an inline void displaydata(void) //definition inside the class
function are also application here {
cout<<roll_number<<"\n";
• all the member functions defined cout<<marks<<"\n";
inside the class definition are by }
default inline, but you can also make };
any non-class function inline by using
keyword inline with them.
Defining Member Functions: Outside the class definition
• The member functions that are declared inside a class have to be defined
separately outside the class.
• To define a member function outside the class definition we have to use
the scope resolution :: operator along with class name and function name.
• The general form of this definition is:
return-type class-name :: function-name(argument declaration)
{
Function body
}
class className2
{
// Other Declarations
friend int className1::functionName(); //The functionName1() is a friend of className2
};
Friend Class
• We know that member of one class can not access the private data of
other class. Some time it is needed that we want to make available
private data of one class to other class. In such case we need to make
one class to a friend of other class.
• A class can also be declared to be the friend of some other class.
• When we create a friend class then all the member functions of the
friend class also become the friend of the other class.
• This requires the condition that the friend becoming class must be first
declared or defined (forward declaration).
• It is a member function of one class and uses object of other class as
arguments so the function has to be called using corresponding class
object and other class object as arguments.
using namespace std; class Second
class second; {
class first public:
{ void change( first &obj, int x )
private: {
int Secret; obj.Secret = x;
friend class Second; // Declare a }
friend class };
public:
first() int main()
{ {
Secret=4; first obj1;
} Second obj2;
void printMember() obj1.printMember();
{ obj2.change(obj1, 5 );
cout << Secret << endl; obj1.printMember();
} }
};
*******************Thank You**************************