March 19, 2018 Topics basic concept of class. Object declaration Accessing class member Access modifier(public and private) Static... Object and Classes in C++ Unknown/ 0 Comments Topics basic concept of class. Object declaration Accessing class member Access modifier(public and private) Static data members & members Function The basic concept of class. A class is a template that specifies the attributes and behavior Of things or objects. A class is a blueprint or prototype from which objects are created. A class is the implementation Of an abstract data type (ADT). It defines attributes and methods. When the object is created then memory allocated to the object, not to class. The class is like a Locker if we want to use any things( member of class) of the locker we need key means Object. Object declaration: In the following example, the class employee is created and 'a' is the object of this class. class item { // data members and member functions }a; In above syntax, the class name is the item, and a is object Of that class. when object created in main() then only memory allocated to the class. Object declaration can be also done in main() function as follows: int main ( ) { item a; } Accessing class member: • Private members Of the class can only be accessed by the members of that class. • Public members Of the class can be accessed outside the class also. • For accessing class member outside class can be done by using dot operator and object Of that class using the following syntax, object— name. function—name (actual—arguments) ; • Member functions Of the class can be declared outside class definition also as follows, Syntax: class item{ public: void getdata(); } Void item::getdata(); Example: Example: # include <iostream.h> #include<conio.h> class employee // class { char name [ 10 ] ; / / data member int i d; / / data member public : void getdata() ; // prototype declaration void putdata ( ) ; // prototype declaration }; void employee: : getdata() // member function { cout<<"enter name and id Of employee :"; cin>>name>>id; } void employee: : putdata() // member function { cout<<"name and id Of employee : "; cout<<"name"<<name<<"id"<<id; } void main ( ) { employee x; x. getdata ( ) ; x. putdata ( ) ; getch(); } Public and Private Access modifier (specifier) for C++ classes. Public: • Public members Of the class are accessible by any program from anywhere. • There are no restrictions for accessing public members Of a class. Class members that allow manipulating or accessing the class data are made public. Private: • Private members Of the class can be accessed within the class and from member functions Of the class. • If we do not declare Access modifier then it will become private by default. • They cannot be accessed outside the class or from other programs, not even from the inherited class. • Encapsulation is possible due to the private access modifier. • If one tries to access the private members outside the class then it results in a compile-time error. If any other access modifier is not specified then member default acts as the Private member. Example: # include <iostream.h> #include<conio.h> class employee // class { private: int a; public : int b; }; void main ( ) { employee x; x. a=7; cout<<"Value of a"<<x.a; getch(); } • In above program a is private member Of class employee. • so we cannot use it with object Of employeeoutside the class, Means we cannot use x.a in main as x.b shown above. Static data members: • Data members Of the class which are shared by all objects are known as static data members. Only one copy Of a static variable is maintained by the class and it is common for all objects. • Static members are declared inside the class and defined outside the class. • It is initialized to zero when the first Object Of its class is created. NO other initialization is permitted. • It is visible only within the class but its lifetime is the entire program. • Static members are generally used to maintain values common to the entire class. Static member functions: • Static member functions are associated with a class, not with any object. They can be invoked using the class name, not object. They can access only static members Of the class. They cannot be virtual. They cannot be declared as constant or volatile. A static member function can be called, even when a class is not instantiated. There cannot be static and non-static version Of the same function. A static member function does not have this pointer. Example: # include < iostream.h> #include<conio.h> class item { int number; static int count; //static variable declearation public : void getdata (int a) { number =a; count++ ; } static void getcount ( ) // the only difference from above program { cout<<"value of count"<<count; } }; void main() { item a,b,c; a.getdata(100); b.getdata(200); c.getdata(300); a.getcount(); b.getcount(); c.getcount(); getch(); } Output: value of count:3 value of count:3 value of count:3 You may also like About Post Author Unknown I am from India. I am Information technology student.I am working on IT and cyber Security from last four year.