Classes and Objects

Embed Size (px)

DESCRIPTION

classes and objects

Citation preview

Classes and Objects

Classes and ObjectsIntroductionObject-oriented programming (OOP) encapsulates data and functions into a single unit called classes.

The data components of the class are called data members and the function components are called member functions.

Class is a blueprint of real world objects.

Classes have the property of information hiding. It allows data and functions to be hidden, if necessary.Specifying a Class The specification starts with the keyword class followed by the class name.The body of the class contains the keywords private, public, and protected .Private data and functions can only be accessed from within the member functions of that class. Public data or functions, on the other hand are accessible from outside the class.Usually the data within a class is private and functions are public. Creating Objects The class declaration does not define any objects but only specifies what they will contain. Once a class has been declared, we can create variables (objects) of that type by using the class name.We can create any number of objects from the same class.For eg, the class name is employee then we can create object as employee x;employee A,B,C;Objects can also be created when a class is defined by placing their namesimmediately after the closing brace. For example,class employee {}A,B,C; Accessing Class Members When an object of the class is created then the members are accessed using the . dotoperator. For example,r.getdata(4, 2);r.showdata();Private class members cannot be accessed in this way from outside of the class.Example class Employee { public: int eid,sal; private:void getdata( ){couteid>>sal;}void display( ){ cout