Class in CPP

Class Process
Class Process

In C and other procedural programming languages, programming tends to be action-oriented, whereas ideally in C++ programming is object-oriented. In C, the unit of programming is the function. In C++, the unit of programming is the class from which objects are eventually instantiated(i.e. created).

C programmers concentrate on writing functions.Groups of actions that perform some task are formed into functions, and functions are grouped to form programs. Data are certainly important in C, but the view is that data exist primarily in support of the actions that functions perform. The verbs in a system specification help the C programmer determine the set of functions that will work together to implement the system.

C++ programmers concentrate on creating their own user-defined types called classes. Classes are also referred to as programmer-defined types. Each class contains data as well as the set of functions that manipulate the data. The data component of a class is called data members. The functional components of a class are called member functions (or methods in other object-oriented languages). Just as an instance of a built-in type such as int is called a variable, an instance of a user-defined type(i.e. a class) is called an object. In the C++ community, the terms variable and object are often used interchangeably. The focus of attention in the C++ programmer determines the set of classes that will be used to create the objects that will work together to implement the system.

Implementing Abstract Data type Time with a Class

class Time
Time();     //constructor
void setTime(int, int, int);   //set hour, minute, second
void printUniversal();     //print universal-time format
void printStandard();     //print standard-time format

int hour;   
int minute;
int second;

The class definition contains prototypes for the following four-member functions after the public member access specifier -Time, setTime, printUniversal, and printStandard.These are the public member functions of the class(also known as the public services, public behavior or interface of the class). These functions will use by the clients(i.e. portions of a program that are users) of the class to manipulate the class’s data.

Class Scope and Accessing Class Members

A class’s data members(variables declared in the class definition) and member functions(functions declared in the class definition) belong to that class’s scope. Nonmember functions are defined at file scope.

Within a class’s scope, class members are immediately accessible by all of that class’s member functions that can be referenced by name. Outside a class’s scope, class members are referenced through one of the handles on an object- an object name, a reference to an object or a pointer to an object. Member functions of a class can be an overloaded function, and provide a separate function definition for each version of the function. Variables declared in a member function has function scope-they is known the only to that function. If member function defines a variable with the same name as a variable with class scope, the class-scope variable is hidden by the function-scope variable in the function scope. Such a hidden variable can be accessed by preceding the variable name with the class name followed by the scope resolution operator(::).Hidden global variables can be accessed with the unary scope resolution operator.

The operators used to access class members are identical to the operators used to access structure members. The dot member selection operator(.) is preceded by an object’s name or with a reference to an object to access the object’s members. The arrow member selection operator(->) is preceded by a pointer to an object to access that object’s members.

For examples of Class Scope and Accessing Class Members

using std::cout;
using std::endl;
class Count
int x;
void print()
int main()
Cout counter;
Cout *counterPtr=&counter;
Cout &counterRef=counter;
cout<<"Assign 1 to x and print using the object's name:";
cout<<"Assign 2 to x and print using a reference:";
cout<<"Assign 3 to x and print using a pointer:";
return 0; 


 Assign 1 to x and print using the object's name: 1
 Assign 2 to x and print using a reference: 2
 Assign 3 to x and print using a pointer: 3


Use a simple class called Count with public data member x of type int and public members of a class with the member selection operators. There are three variables related to type Count-counter(a Count object),counterRef(a reference to a Count object) and counterPtr(a pointer to a Count object).Variable counterRef refers to counter, and variable counterPtr points to counter. It is important to note that class Count declares a data member x as public here simply to demonstrate how public members are accessed off handles(i.e. a name, a reference or a pointer). As we have stated, data typically are made private, as we will do in most subsequent examples. Inheritance, we will sometimes make data protected. Note that the program can access member variable x and invoke member function print by using the name of the object(counter) or a reference to the object(counterRef) together with the dot(.) member selection operator. Similarly, the program can access a member variable x and invoke a member function print by using a pointer(countPtr) and the arrow(->) member selection operator.

Translate »