Member Functions And Member Data Computer Science Essay

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

There are many languages for writing programs. We shall have an overview of C++ programming language and use it to write programs. Modern programs are usually designed using a method known as object-oriented programming (OOP). Object Oriented Method of programming is based on the hierarchy of classes, and well defined objects. OOP is organized around the objects. In this method a program is viewed as a collection of interacting objects. The main characteristics of OOP are encapsulation, inheritance and polymorphism. C++ accommodates OOP by providing classes, an expanded concept of a data structure, which instead of holding data only, can hold both data and functions.

A class is a data type whose variables are objects. Where as an object is a variable that has member functions as well as the ability to hold data values. [Problem Solving with C++ by Walter Savitch, Sixth Edition]. The major properties of the class are Data Encapsulation and Inheritance. Encapsulation is a form of information hiding or abstraction. Combining a number of variables and functions, into a single package, as an object of a class is called encapsulation. Objects have algorithms that describe how it should behave in different situations.

A class can contain members that are of a class type or pointers, references to a class type. Data members include members that are declared with the fundamental types, including pointers, reference, array types, bit fields and user defined types.


There are many kinds of functions that can be declared only as class members. These are called special member functions. These functions affect the way objects of a class are created, destroyed, copied and converted into objects of other types. when a member function is declared as constant by using a keyword const, then this function is just a read only function. These functions can be called implicitly.

In this context we shall have a look about the following special member functions:



Copy constructor

Assignment operator

Friend function


Access specifier determines the access to the member function or member data that follows it, up to the next access specifier or the end of the class declaration. The class allows us to use fine control over access rights to class components. The access rights to each class component is indicated by using the key words public, private and protected. The keywords divide the class scope into segments. There can be any number of public, protected and private segments in any order you want. Public access specifier affects all members until the next access specifier. Whereas private access specifier affects all members until the class end.

The default access to class members is private; the default access to struct and union members is public. Class members which are declared as private can be used only by member functions and friends of the class. Class members declared as protected can be used by member functions, friends of the class and classes derived from the class. Class members declared as public can be used by any function. Protected access is an intermediate level of data protection between private and public.

It is very common for some part of an object to require initialization before it can be used. C++ allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor function. When defining a class a special kind of member function can be defined known as constructor.


"A constructor function is a special function that is a member of a class and has the same name as that class." [C++ The Complete Reference by Herbert Schildt, Second Edition]

A constructor is a function that is automatically called when an object of the class is declared. A constructor is defined in the same way as member function, except the two steps:

Both the class name and the constructor name should be same.

Neither a constructor declaration nor the constructor definition includes a return value.

Constructors cannot be called explicitly as regular member functions. Constructors will be executed when the new object of class is created. The definition of the constructor is same as member functions. Suppose we wanted to add a constructor for initializing the side for an object, we include a constructor which is shown below.

class square


int *side; <----------- pointer variable


square(int); <----------- constructor


int area()


return(*side * *side);



In this class we notice that the name of constructor is the same as the class. Finally the constructor is places in the public section of the class definition. Here we observe that the variable is declared as a pointer.

"A pointer is the memory address of a variable". There are two special pointer operators : ' *' and '&'. The '&' is a unary operator that returns the memory address of its operand. The '*' is a unary operator that returns the value located at the address that follows.

Example using a constructor:


using namespace std;

class square <-------- class


int *side;


square(int); <-------- constructor

~square(); <-------- destructor

int area()


return(*side * *side);



square::square(int s) <-------- constructor definition


side= new int;



int main()


square sqr(4); <-------- object

cout<<"AREA OF THE SQUARE IS : "<<sqr.area()<<endl;





This program calculates the area of the square using constructor and destructor. Here we observe that a class named square is defined where in it has the declaration of variables and a method area. A constructor by class name is defined and declared along with a destructor. An object is been created using the class and the area of square is calculated by using the method from the class.

A constructor that do not have any arguments is called DEFAULT CONSTRUCTOR. If we define a class and include absolutely no constructors of any kind, then a default constructor will be automatically created. This default constructor does not do anything, but it does give an uninitialized object of the class type, which can be assigned to a variable of the class type. If the class definition includes one or more constructors of any kind, no constructor is generated automatically.

The new element shown on the line of the constructor definition is called the initialization section. The initialization section goes after the parenthesis that ends the parameter list and before the opening brace of the function body. The initialization section consists of a colon followed by a list of some or all the member variables separated by commas. Each member variable is followed by its initializing value in parentheses.

Aside from performing the special role of initialization, constructor functions are no different from other types of functions. This includes overloading.

Dynamically allocated variables do not go away unless the program makes a delete. If the dynamic variables are not eliminated, then the dynamic variables will continue to occupy memory space. Which may cause the program to abort by using up all the memory in the free store manager. This can be handled by using a member function called a destructor.


"A destructor is a member function that is called automatically when an object of the class passes out of scope."[Absolute C++ by Walter Savitch, Third Edition]

The destructor has the same name as constructor, but it is preceded by a '~'. Like a constructor, a destructor has no return type, not even the type void. A destructor has no parameters. Thus, a class have only one destructor which cannot be overloaded.


Classes that have objects of other classes as their data members are called composite classes. In object oriented design, using objects of one class as components for objects of another class is called CLASS AGGREGATION or CLASS COMPOSITION.

The term aggregation is used to describe the structure of a class or an object whose encapsulated data includes instances of other user defined classes. The encapsulated objects are refered with the word subobject.


The values from one object are copied to another existing object by using the assignment operator. The assignment operator is of low precedence. It has to be performed after all other expressions are completed.

A constructor which is called whenever a new variable is created from an object is called a COPY CONSTRUCTOR. A copy constructor is generally called when an object is created from another object of same type, passed by value as a parameter to a function and returned from a function.

If it is not defined in a class, the compiler itself defines a copy constructor. []

Both copy constructor and assignment operator are mostly alike, i.e., both copy one object to another. However, the assignment operator copies to existing objects, and the copy constructor copies to newly created objects.

If a new object has to be created before the copying occurs, then the copy constructor is used. If a new object does not have to be created before the copying occurs, then the assignment operator is used.

The copy constructor is called instead of the assignment operator in the following situations:

1. When instantiating one object and initializing it with values from another object.

2. When passing an object by value.

3. When an object is returned from a function by value.


A friend function is a function that is not member of the class but has access the class's private and protected members. Friend functions are normal external functions which are given special access privileges but, they are not considered as class members. These functions are not in class's scope, and not called using member selection operators unless they are members of other class.


using namespace std;

class com



int aa, bb,m,n;

public :

void areaa(int m) <-------- Function Overloading

{cout<<"\narea of the square is : "<<m*m<<endl;}

void areaa(int m, int n)

{if(m>n)cout<<m<<" is greater "<<m<<" and "<<n<<endl;

else cout<<n<<" is greater among "<<m<<" and "<<n<<endl;}

void setd(int r,int i) <-------- Inline function(implicit)


cout<<"Enter real part "<<endl;


aa= r;

cout<<"Enter imaginary part "<<endl;




friend com add (com fd1,com fd2); <------- Friend Function

inline void display()


cout<<"\nThe complex number after adding the two real and imaginary parts is : "<< aa<< " + i "<< bb<< endl;



com add (com fd1,com fd2)


com fd3;




void main()


int l=0;

int m=0;

com K1,K2,K3;









The above program shows the usage of a friend function. Using the friend function we can access the classes private and public data. The private variables are used to read the values of the user . Using the friend function the variables are added and it displays the output using the display function from the class. In this program we find the overloading function. Over loading a function is not but giving same name to two functions with more or less parameters passed. The functions which are defined in the class are called inline functions. They are of two types "implicit inline " and "explicit inline" The output as shown below.


By the end of this assignment it is clear, how to use the constructors, destructor, access specifier, member data and functions, friend function, assignment operator, copy constructor, overloading of functions in classes in C++.


C++ The Complete Reference by Herbert Schildt, Second Edition

Problem Solving with C++ by Walter Savitch, Sixth Edition

Absolute C++ by Walter Savitch, Third Edition