CH-1C: CLASSES
CLASSES AND OBJECTS
• User defined data type
• Combines logically related data and the code
that manipulates data into a single unit
• The data items enclosed within a class are
called data members
• The functions that operate on data and
enclosed within a class are called member
functions
• Used for creating user defined data items
• Very similar to structures
• But, all the structure members are public by
default and all the class data members are
private by default
• It allows data(and functions ,if required) to be
hidden
• Biding data and code that operate on data into a
single unit is called Encapsulation
• Encapsulation helps to hide data and hence
protect it from unauthorized access
• Syntax:
class class_name
{
access_specifier1:
variable declaration;
function declaration;
access_specifier2:
variable declaration;
function declaration;
………..;
}
• Class Student
• Data members: Data members:
• roll, marks roll, marks
• Member Functions: Member Functions:
• getdata()
getdata()
• display() display()
• Generally, class specification has two parts-
-data member declaration
-member function declaration/definition
• Access specifier describes the scope of members
• public, private, protected
• private data members can only be accessed by
the member functions and friend functions of the
class.These members are not accessible to
outside world
• By default, class members are private
• public data members can be accessed by
member functions of class and any function
outside the class
• Member functions of class can be defined
within the class or outside the class
• For defining member functions outside the
class, the scope resolution operator (::) is used
eg
void Student::getdata()
{
--------------;//code
}
• Here, :: informs the compiler that getdata() is a
member function of Student class.
• Creating objects:
• Objects of a class can be created in main() by
using class name as data type as
class_name object1, object2;
• eg
Student s1,s2;
creates two objects of Student class
Dynamic Memory Allocation
• Allocation of memory during runtime on demand
is called as Dynamic Memory Allocation
• C++ provides two special operators for Dynamic
Memory Allocation
-new operator for Dynamic Memory Allocation
-delete operator for Dynamic Memory deallocation
• new operator:
• Allocates memory and returns pointer to that
memory
• If, memory can not be allocated, it throws an
exception
• Syntax:
ptrvar = new Datatype[size];
eg
int *ip;
ip= new int[100];
• delete operator:
used to deallocate (free) the memory allocated
by new operator back to memory pool.
Syntax-
delete ptr_var;
eg delete ip;
Static Data Members
• Normally, each object of a class has its own
set of public/private data members.
• Each member function accesses objects
own data.
• In some situations, it is desirable to have
one or more common data fields, which are
accessible to all the objects of the class.
• The members which need to be common
can be declared as static.
class class_name
{
----------;
static datatype variable/data member;
----------;
};
datatype class_name :: data_member=value;
Static data members can be initialised during
their definition outside all the member
functions.
Static Member Functions
• C++ allows to define static functions, which
can only access static members (data and
functions) declared in the same class.
• The non static members are not accessible
to these functions.
• Static member functions declared in the
public part of a class can be accessed
without reference to object of class.
• Static member functions can also be defined
in the private part of a class. Such functions
can access only static data members and can
invoke static member functions .
• Important points about static members:
• Only one copy of static data member exists
for all objects of a class
• Static member functions can access only static
members of its class
• Static data members can be defined &
initialised like global variables.
• Static members defined as public can either
be accessed through scope resolution operator
as
classname::membername or
it can be accessed through the object of a
class as
objectname.membername
i.e. static members can be accessed using
only the class name, without reference to any
object.
Array
• An array in any programming language is a
collection of similar data items stored at
contiguous memory locations and elements can be
accessed randomly using indices of an array.
• They can be used to store the collection of
primitive data types such as int, float, double, char,
etc of any particular type.
• To add to it, an array in C/C++ can store derived
data types such as structures, pointers, etc.
A[0] a[1] a[2]
Int a[7];
An array of Objects
• An array of objects is declared the same as any other
data-type array. An array of objects consists of class
objects as its elements. If the array consists of class
objects it is called an array of objects.
• The Array of Objects stores objects. An array of a class
type is also known as an array of objects.
Storing more than one Employee data
#include<iostream> void Employee::putdata()
class Employee {
{ cout<<id<<" ";
int id; cout<<name<<" ";
char name[30]; cout<<endl;
public: }
void getdata(); int main()
void putdata(); {
}; Employee emp;
void Employee::getdata() emp.getdata();
{ emp.putdata();
cout<<"Enter Id : "; return 0;
cin>>id;
cout<<"Enter Name : "; }
cin>>name;
}
#include<iostream> void Employee::putdata()
{
class Employee cout << id << " ";
{ cout << name << " ";
cout << endl;
int id;
}
char name[30]; int main()
public: {
// This is an array of objects having maximum limit of 30 Employees
Employee emp[30];
void getdata(); int n, i;
void putdata(); cout << "Enter Number of Employees - ";
cin >> n;
}; // Accessing the function
void Employee::getdata() for(i = 0; i < n; i++)
emp[i].getdata();
{
cout << "Employee Data - " << endl;
cout << "Enter Id : ";
cin >> id; // Accessing the function
for(i = 0; i < n; i++)
cout << "Enter Name : "; emp[i].putdata();
cin >> name; }
}