CLASSES AND OBJECTS

 A class is a way to bind the data describing an
entity and its associated functions together. In
C++ , class makes a data type that is used to
create objects of this type.
Need for classes
Classes are needed to represent real-world
entities that not only have data type properties
(Characteristics) but also have associated
operations (their behaviour)
 Class class_name
{
private:
data_members
member_functions
[public:
data_members
member_functions]
[protected:
data_members
member_functions]
}
Static Class Member
 In a class there may be static data member and static
functions member.
 A static member variable has certain characteristics. These
are :
 It is initialized to zero when the first object of its class is
created.
 Only one copy of that member is created for the entire
class and is shared by all the objects of that class, no
matter how many objects are created.
 It is visible only within the class, but its lifetime is the
entire program.
 Static variables are normally used to maintain values
common to the entire class.
Static Member variable
 The declaration of a static data member in the member list of a class
is not a definition. You must define the static member outside of the
class declaration because they are stored separately rather than as a
part of an object. For example:
class X
{public:
static int i;
};
int X::i = 0; // definition outside class declaration
//int X::i;
//int X::i=10;
 Also called as class variables.
Static Member Functions
 A member function that is declared static has the
following properties:-
A static function can have access to only other static
members(functions or variables) declared in the same
class.
A static member function can be called using the class
name (instead of its objects) as follows:
 class-name:: functionmame();
 We can declare a static member by prefixing a word
static in front of the function return type
Scope of class and its Members-
 The public member is the members that can be
directly accessed by any function, whether member
functions of the class or non member function of the
class.
 The private member is the class members that are
hidden from outside class. The private members
implement the oop concept of data hiding. They are
only accessible through class member functions.
 A class is known to be a global class if it defined outside the body of
function i.e. not within any function.
class cl { //* A class declared globally *//
….
};
cl ob; //global object
void main()
{
….
cl ob1; //local object of type cl
}
void function()
{
cl ob2; //local object of type cl
}
 A class is known to be a local class if the definition of the class occurs
inside (body) a function. For example
void main()
{
Class cl { //* A class declared locally*//
….
}
cl ob;
}
void function()
{
………
cl ob2; //invalid
}
In the above example the class ‘cl’ is available only within main()
function therefore it can not be obtained in the function
function().
 A object is said to be a global object if it is
declared outside all the function bodies it
means the object is globally available to all the
function in the code.
A global object can only be declared using global
class type.
A object is said to be a local object if it is
declared within the body of any function it
means the object is available within the function
and can not be used from other function.
A local object can be created from both class
types : global as well as local.
my_class
{
int a;
public :
void fn()
{
a=5;
cout<<”The answer is “<<a;
}
};
// * Global object *//
my_class ob1;
void kv()
{
//* Local object *//
my_class ob2;
}
void main()
{
ob1.read();
//* ob1 can be used as it is
global *//
ob2.read();
//* ob2 can not be used as
it is local *//
}
Nested Classes
 When a class declared within another class then class declared within
(inside/inner class) is called ‘Nested class’ and the outer class is called
‘Enclosing class’.
class encl {
int a;
class nest{


};

public :
int b;
};
 In the given example ‘encl ‘ is an enclosing class and ‘nest’ is the nested
class. The object of nested class can only be declared in enclosed class.
Object as Function Argument-
 An object can be passed both ways:
i) By Value
 When an object is passed by value, the function creates
its own copy of the object to work with it, so any
change occurs with the object within the function does
not reflect to the original object.
ii) By Reference
 when we use pass by reference then the memory
address of the object passed to the function therefore
the called function is directly using the original object
and any change made within the function reflect the
original object.
class my_class {
public :
int a;
void by_value(my_class cv)
{ cv.a=cv.a+5;
cout<<"\n After pass by value";
}
void by_ref(my_class &cr)
{ cr.a=cr.a+5;
cout<<"\n After pass by reference";
}
void disp(my_class di)
{
cout<<"The result is "<<di.a;
}};
void main()
{ clrscr();
my_class ob,mc;
ob.a=12;
mc.by_value(ob);
mc.disp(ob);
mc.by_ref(ob);
mc.disp(ob);
getch();
}
Function Returning Object/ class type
function
To return an object from function we have
to declare the function return type as class
type .
For example my_class fn();
Here my_class is the class name and fn() is
the function name.
Constant Member Functions
 If a member function of a class does not change any
data in the class then the member function can be
declared as constant member by post fixing the word
const at the time of function declaration.
 Example- void fn() const;
 Once a member function declared as const, it cannot
alter the data values of the class. Compiler will
generate error message.