Classes in c similar to structures only access

This preview shows page 15 - 32 out of 32 pages.

Classes in C++ Similar to structures, only access specifier changes Data hiding is achieved by using access specifiers By default all members of structures in C++ has public access whereas members of classes have private access by default
Image of page 15

Subscribe to view the full document.

Image of page 16
Image of page 17

Subscribe to view the full document.

Image of page 18
Access Specifiers in C++ Three Public Private Protected
Image of page 19

Subscribe to view the full document.

Image of page 20
Image of page 21

Subscribe to view the full document.

Public Access Specifier is used in the program Definition of member functions are similar to structures
Image of page 22
Constant member functions A function becomes const when const keyword is used at the end of function’s declaration The idea of const functions is not allow them to modify the object on which they are called. It is recommended practice to make as many functions const as possible so that accidental changes to objects are avoided.
Image of page 23

Subscribe to view the full document.

Constant member functions Objects can also be constants Constant functions can be accessed by both constant and non-const member functions
Image of page 24
Static Data Members Class members static using static keyword Shared by all objects of the class Shall be initialized outside the class using scope resolution operator Static data members can be referred to without referring to an object of class type but by using class name and scope resolution operator
Image of page 25

Subscribe to view the full document.

Each object has a copy of normal variables whereas static variables are shared among the objects In figure, Variable1, Variable2 etc are normal variables
Image of page 26
Static Member Functions can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator :: . can only access static data member, other static member functions and any other functions from outside the class.
Image of page 27

Subscribe to view the full document.

Image of page 28
Image of page 29

Subscribe to view the full document.

Image of page 30
Image of page 31

Subscribe to view the full document.

Image of page 32

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern