The Anatomy of a Class

In programming, a class is a fundamental concept in object-oriented programming languages. A class is a blueprint or template that defines the structure, behavior, and attributes of a particular type of object. It encapsulates data (variables, called attributes or properties) and functions (methods) that operate on the data.

An object is an instance of a class, created from the class blueprint. Each object can have its own unique set of data and can use the methods defined in the class. Classes promote modularity, reusability, and maintainability in software development.

Class Members:

Methods

Methods define what an object can do or the behavior of the object.

Constructors

Constructors don’t actually construct the object. The class makes the object and then executes a constructor to initialize the values of the fields (instance variables).

Member Access Specifiers

The members of a class are classified into three categories: private, public, and protected. In C++, private, protected, and public are reserved words and are called member access specifiers.

Following are some facts about public and private members of a class:

  • By default, all members of a class are private.

  • If a member of a class is private, you cannot access it directly from outside of the class.

  • A public member is accessible outside of the class.

  • To make a member of a class public, you use the member access specifier public with a colon, :

In object-oriented programming, public, private, and protected are access modifiers used to specify the accessibility of class members, such as properties and methods. These access modifiers define the level of access to a class member from outside the class.

  1. Public: Public members are accessible from anywhere, including outside the class. They can be accessed using the dot (.) operator on the object of the class. Public members can be used by any code that has access to the object of the class.UML

  2. Private: Private members are only accessible within the class they are defined in. They cannot be accessed from outside the class. Private members can only be used by the class itself.

  3. Protected: Protected members are similar to private members, but they can be accessed by derived classes. Protected members are not accessible outside the class, but they can be accessed by any derived class that inherits from the class

Encapsulation

  • There are two views of an object:

    • internal  -  the details of the variables and methods of the class that defines it

    • external  -  the services that an object provides and how the object interacts with the rest of the system

  • From the external view, an object is an encapsulated entity, providing a set of specific services

  • These services define the interface to the object

 

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark