4 Classes & Objects -I

Jyoti Pareek

 

Classes are basic building blocks of object oriented applications. Class is a tool to implement Abstract Data Type. A well-crafted class can create powerful user defined data type.

 

As we have seen that all programming languages provide many built-in data type. But sometimes we need to deal with data which cannot be conveniently stored in built-in data type. For dealing with such data most of the programming languages provide constructs which allows us to create our own data types. But before we attempt to create our own data type, we need to understand what is meant by data type or in other words what needs to be defined to define our own data type. To define a data type we need to define the constants that can be stored in variables of that data type and the operations that we can be perform on these values. The logical description of these two things is called Abstract Data Type.

 

Abstract Data Type (ADT)

 

An abstract data type (ADT) is a mathematical model for data types where a data type is defined from the point of view of a user of the data. Specifically in terms of

 

•       possible values that the variable of that data type can store.

•       possible operations on data of this type, and the behavior of these operations.

 

ADT is logical description of the data type that we intend to create and use, but to use these data type in our program we need to implement these data types. Programming languages provide constructs to implement these Abstract Data Types.

 

Defining User Defined Data Type in C

 

You may recall that in C we can define user defined data type using

•       Typedef

•       Enumerated data type

•       Structure

 

But all of these constructs define the ADT’s partially. They only implement data aspect of data type that is they only define what constants variables of defined data type can hold. But these constructs do not allow us to define the operations that we wish to be performed on these values. C++ provides constructs which allow us to define the ADT in more complete sense.

 

Defining User Defined Data Type in C++

 

In C++ we can define user defined data type using

    ·         Structure

·         Class

 

The structure in C++ has been extended in C++ to allow us to define our own data type in more complete fashion. Let us recall how we define user defined data type in C using structure

 

If we wish to store time we can declare a variable of type Time as Time current_time;

 

We can access individual members of current_time using (.) operator.

 

current_time.hours = 3;

current_time.minutes= 20;

current_time.seconds = 35;

current_time.displayTime();

 

Structure in C++ is fundamentally different from structure in C therefore C++ provides another construct called Class for the same purpose, probably to have clear demarcation of the difference.

 

Using Class to define Time

 

Structure in C++ is fundamentally different from structure in C therefore C++ provides another construct called Class for the same purpose, probably to have clear demarcation of the difference. We can define the Time data type using class in almost the same way as we can define it with structure.

 

class Time

{

int hours;

int minutes;

int seconds;

void readTime();

void displayTime();

Time differenceTime(Time t)

};

 

This defines new data type Time almost in similar fashion as we define using struct. If we wish to store time we can declare a variable of type Time as

 

Time current_time;

 

In class variables also individual members can be accessed using . operator. However following statements will not work fine for variable of Time Class.

  current_time.hours = 3;           //    this will not work

current_time.minutes= 20;      //    this will not work

current_time.seconds = 35;      //    this will not work

current_time.displayTime();     //    this will not work

 

 

This all worked well in struct then why it will not work in class?

 

Well the reason is that struct and class members have access specification associated with their members namely

 

•       Private

•       Public

•       Protected

 

We will discuss about protected access specification in later modules. In this module we will discuss about private and public members.

 

Access Specification

 

Private Members: Private members of class are accessible to the member functions of the class only. They cannot be accessed in any function outside the class. Members of the class by default are private. Public Members: Public members of class can be accessed by member functions of the class as well as non-member functions. Members of structure by default are public.

 

Access Specification in Structure

 

struct Time

 

{

 

int hours;

int minutes;

int seconds;

void readTime()

{

 

cin >> hours ;

cin >> minutes;

cin >> seconds;

}

void displayTime()

{

cin >> hours ;

cin >> minutes;

cin >> seconds;

}

};

 

Void main()

{

Time current_time;

current_time.hours = 3;

current_time.minutes= 20;

 

current_time.seconds = 35;

current_time.displayTime();

}

 

 

Note : As no access specification is specified , members by default are public

 

Let us completely define our Time class

 

Time addTime(Time t)

 

{

Time total_time ;

 

total_time.seconds = seconds + t.seconds;

if (total_time.seconds > 60)

{

total_time.seconds = total_time.seconds – 60;

total_time_minutes = 1 ;

}

total_time.minutes = total_time.minutes+ minutes+ t.minutes;

 

if (total_time.minutes > 60)

{

total_time.minutes = total_time.minutes – 60;

total_time_ hours = 1 ;

}

total_time.hours = total_time.hours+ hours+ t.hours;

}

 

};   //  end of class

 

 

Defining Member functions Outside The Class

 

 

class Time

 

{

int hours;

int minute;

int seconds;

public :

 

void readTime();

void displayTime();

 

time differenceTime(Time t);

time addTime(Time t);

};

 

void Time :: readTime()

{

cin >> hours;

 

cin >> minutes;

cin >> seconds;

}

void Time :: displayTime()

{

 

cout << hour;

cout << minute;

cout << second

}

 

Time Time :: differenceTime(Time t)

{

Time time;

int seconds_1 , seconds_2 , seconds_diff ;

 

// convert time into seconds

 

seconds_1 = (hours *3600) + (minutes * 60) + seconds ;

seconds_2 = (t.hours * 3600) + (t.minutes * 60) + t.seconds ;

seconds_diff = seconds_1 – seconds_2 ;

time.hours = seconds_diff / 3600 ;

seconds_diff = seconds_diff % 3600 ;

time.minutes = seconds_diff /60 ;

seconds_diff = seconds_diff % 60 ;

time.seconds = seconds_diff ;

return time;

}

};

 

Time Time :: addTime(Time t)

 

{

Time   total_time ;

 

total_time.seconds = seconds + t.seconds;

if (total_time.seconds > 60)

 

{

total_time.seconds = total_time.seconds – 60;

total_time_minutes = 1 ;

 

}

total_time.minutes = total_time.minutes+ minutes+ t.minutes;

if (total_time.minutes > 60)

{

 

total_time.minutes = total_time.minutes – 60;

total_time_hours = 1 ;

}

total_time.hrs = total_time.hours+ hours+ t.hours;

}

 

The public members of the class form the interface of the class, through which the class can be used.

 

Public Interface of Time Class

void readTime();

void displayTime();

time differenceTime(Time t);

time addTime(Time t);

 

 

Data Members should be private.

Member functions should be public (those functions which are created to assist other member functions and not consistent with interface of the class should be made private).

 

 

Unions in C++

 

•       Members of unions in C++ have access specification, default being the public as in structure.

•       Unions in C++ can also have member functions along with data members.

•       All data members ( but not the member functions ) share memory.

•       A union without a name, known as anonymous union is possible in C++.

 

Syntax of Union in C++

 

union union-name

 

{

datatype var1;

datatype var2;

 

– – – – – – – – – –

– – – – – – – – – –

datatype varN;

return_type function1();

return_type function 2();

 

return_type function3();

– – – – – – – – – –

 

– – – – – – – – – –

};

 

Anonymous Union

 

Anonymous union can be declared

 

•       Locally

•       Globally

A global anonymous union must be static. A local anonymous union must be either static or automatic, not external.

 

Defining Anonymous Union locally

 

#include<iostream>

using namespace std;

void main()

{

union

{

int data1;

float data2;

};

data1 = 5;

cout << data1;

}

 

Note : The name of each member must be unique within the scope where the union is declared. You can directly access the members of an anonymous union as shown in the example.

Summary

 

  • Classes are powerful tool to create your own data type.
  • They help us achieve object oriented concepts like abstraction and encapsulation and data hiding.
  • C++ provides many features using which we can make our classes behave similar to built in data type. We will discuss them in successive modules.