pm_jat@daiict.ac.in
In abstract sense, it is all about information hiding Informally, you can call it as packaging of data and function together in a single entity called class such that you get implementation independence
C++ has two structuring mechanisms: class and struct that let you specify data and function members have public and private members In a class, members are private by default, while public in struct; for all practical purposes, this is only the difference between struct and class in C++
Data Members Can be static or non-static In C++, non-static data members are called instance variables. Lifetime of instance variables is life of object Scope of data members is within the class, that is all member function of that class Public data members can be accessed through objects using dot operator
static data Members static data members are class data members; that is these variables share common storage for all of its objects For example, you want to have your employee class, such that, It has an additional field emp_no Each time new employee object is created, it automatically gets next employee no! For this we have a class data member next_emp_no, and it is used while constructing new object and incremented, so that new employee no is available for next object construction
data members static data members can be accessed even without having created any object of the class Public static data members can be accessed; for example suppose employee class has organization as a public static data member, then it can accessed- through objects using dot operator harry.organization through class using scope resolution operator Employee::organization
function members - methods Again can be static and non-static Non static functions: are always invoked through objects, also called object methods These functions receive an implicit parameter called this, which is address of the object on which the function has been invoked
function members - methods Static member function can be called without creating any objects of the class Employee::getNextEmployeeNo(); can be called through any object of class emp1.getnextemployeeno(); static member function do not use instance variables of any object these functions do not receive this implicit parameter, therefore access only static data members
accessor and mutator functions Data are kept private.. helps in having implementation independence However most of time you need to know values of data members, for this you have accessor methods, also called as get methodsgetname() getsalary() you have methods that allow you to change values of data members, called mutator methods, also called set methods setsalary( salary ) readconsole()
const member functions Accessor functions are not supposed to modify any data members, and, C++ does not understand which is accessor method and which is mutator method get/set is naming convention and good for human readers However, it allows you to write a const function that can not modify any data memberdouble getsalary() const; void print() const C++ ensures this by making this implicit parameter as constant pointer to such functions
Mutable and immutable data members Data members which can not be changed through out the life of object are immutable In C++, you can have immutable data member by making them constant using const keyword By default data members are mutable, and a constant function can not modify any data member However you can allow them to do so, in some exceptional cases by explicitly making some of variables as mutable mutable int print_count; //suppose you want to have print counter, //now const print method would be able to modify this variable
Example Let us modify our employee class have two private static variables: string organization_name; long next_emp_no; have following static member functionsetorganizationname(string orgname); string getorganizationname() const; long getnextemployeeno() const;
Example Also have a private instance variable: long emp_no; Modify constructors to assign next available employee no to new employee objects, may modify both constructors an accessor method for emp_no: string getemployeeno();
Example Also have print_count a mutable variable in employee class and use it to keep track of times this object has been printed update it in print method - print_count++;
Thanks