Lesson 6: Class data structure
6.1. Introduction
A class is a template for creating objects. It provides mechanism that allows you to
combine data and the function in a single unit is called a class. A class variable is called
object or instance.
6.2. Lesson objectives
By the end of this lesson the learner will be able to
• Define and explain applications of class data structure
• Explain advantages and disadvantages of class Declare and use class data
structure Describe class access specifiers.
• Declare and use member functions
6.3. Lesson outline
This lesson is structured as follows:
6.1. Introduction
6.2. Lesson objectives
6.3. Lesson outline
6.4. Declaration of class
6.5. Object declaration
6.6. Access class members
6.7. Constructor
6.8. Destructor
6.9. Revision questions
6.10. Summary
6.11. Suggested reading
6.4. Class Declaration:
The class data structure is declared as follows:
class class_name
{
private:
members1;
protected:
members2; public:
members3;
};
Explanation:
Class: This is a keyword for class declaration.
Class name: This is an identifier for the class.
{}: Encloses the body of the class declaration (class members).
Class members: Class members data members and/or member functions declarations.
Access specifiers: The class member access specifiers modify the access rights that the
members following them acquire.
Private: Requires that members of a class can only be accessed by other functions
within the class.
Protected: Requires that members are accessible by other functions of the same class
and also functions from a derived class.
Public : Requires that members can be accessed by any function in the program NB:
By default, all members of a class declared with the class keyword have private
access for all its members.
The class declaration must end with semi colon.
6.5. Object Declaration
Objects are usually declared inside the main function. The syntax for declaring a object
is the same as that for declaring any other variable. The following statements declare
two objects of type student:
syntax class_name
object_name;
6.6. Accessing Class Members
Once an object of a class is declared, it can access the public members of the class.
Syntax:
objectname.member;
Example:
Student1.getdata();
Declaration of Member functions
Member functions must be declared inside the class. During declaration the body of
the function is not given. Only the function prototype is specified.
Syntax:
Return type function_name(argument(s)); //declaration
Explanation:
Return type: Is a data type which can be int, float, double, void e.t.c.
Function_name: Is a valid function identifier.
(): Open and closing brackets to enclose the function arguments.
Arguments: Includes the formal parameters (optional).
Examples:
int getdata(int a, int b); // function prototype/declaration
void getdata(); // function prototype/declaration
Definition of Member functions
Member functions can be defined inside a class or outside the class.
a)Definition inside the class
A class member is declared and defined inside the class declaration. This means body of
the function must be specified(what the function will do).
Syntax: return type function_name (arguments);
{
statement(s);
}
Example:
int getdata(int a, int b);
{
cout<<”please enter the value of a:”<<endl; cin>>a; cout<<”please
enter the value of b:”<<endl;
}
b) Definition outside the class
A function is declared inside the class declaration (function prototype) but is defined
outside the class. This means the body of the function is specified outside the class
declaration. When we define a function outside the class we cannot reference them
(directly) outside of the class. In order to reference these functions, we use the scope
resolution operator, :: (double colon).
Syntax: return type class_name::function_name(arguments)
{
statement(s);
}
Example:
In this example, we are defining function getdata outside the class:
void student :: getdata()
{
cout<<"Please enter the value of a: ";
cin>>a; cout<<"Please enter the value of b: ";
cin>>b;
}
6.7. Constructor
It is a member function having same name as it’s class and which is used to initialize
the objects of that class type with a legel initial value. Constructor is automatically
called when object is created.
Default Constructor-: A constructor that accepts no parameters is known as default
constructor. If no constructor is defined then the compiler supplies a default
constructor.
student :: student()
{
rollno=0;
marks=0.0;
}
Parameterized Constructor -: A constructor that receives arguments/parameters is
called parameterized constructor.
student :: student(int r)
{
rollno=r; }
Copy Constructor-: A constructor that initializes an object using values of another
object passed to it as parameter, is called copy constructor. It creates the copy of the
passed object.
student :: student(student &t)
{
rollno = t.rollno;
}
There can be multiple constructors of the same class, provided they have different
signatures.
6.8. Destructor
A destructor is a member function having same name as that of its class preceded by
~(tilde) sign and which is used to destroy the objects that have been created by a
constructor. It gets invoked when an object’s scope is over.
~student() { }
The following program demostrates the general feature of classes. Member functions,
constructor, destructor, getdata() and showdata() defined outside the class.
#include<iostream> using
namespace std;
class student //Declare student class
{
private :
int rollno; //class data
members float marks; public:
void getdata(); //member function to get data from user
void showdata();// member function to show data student();
//constructor to initialize object
~student(); //destructor to delete object from memory
};
student::student() //define constructor
{
cout<<"Created object"<<endl;
}
void student :: getdata() //reading the values
{
cout<<"----------------------"<<endl;
cout<<"Please enter student details:"<<endl;
cout<<"Enter Roll Number :"; cin>>rollno;
cout<<"Enter Marks : ";
cin>>marks;
cout<<"----------------------"<<endl;
} void student :: showdata() //printing the
values
{
cout<<"Displaying student
details:"<<endl; cout<<"Roll number :
"<<rollno<<endl; cout<<" Marks :
"<<marks<<endl;
}
student::~student() //define destructor
{ cout<<"Deleting the
object"<<endl;
}
void main()
{
student john;
john.getdata(); //call member function to get data
john.showdata(); //call member function to print data
}
Example : In the following program constructors, destructor and other member
functions are defined inside class definitions. Since we are using multiple constructor
in class so this example also illustrates the concept of constructor overloading
#include<iostream> using
namespace std;
class student //specify a class
{
private :
int rollno; //class data members
float marks;
public:
student() //default constructor
{
rollno=0;
marks=0.0;
}
student(int r, int m) //parameterized constructor
{
rollno=r;
marks=m;
}
student(student &t) //copy constructor
{
rollno=t.rollno;
marks=t.marks;
}
void getdata() //member function to get data from user
{
cout<<"Enter Roll Number : ";
cin>>rollno;
cout<<"Enter Marks : ";
cin>>marks;
}
void showdata() // member function to show data
{
cout<<"\nRoll number: "<<rollno<<"\nMarks: "<<marks;
}
~student() //destructor
{}
};
int main()
{
student st1; //defalut constructor invoked student st2(5,78);
//parmeterized constructor invoked student st3(st2); //copy
constructor invoked st1.showdata(); //display data members of
object st1 st2.showdata(); //display data members of object st2
st3.showdata(); //display data members of object st3 return 0;
}
6.9. Revision questions.
a)Define the following terms.
i). Class
ii). Object
iii). Access specifier
iv). Member function
b) Explain the following access specifiers
i). Public
ii). Private
iii). Protected
c)Create a class employee whose private members are int age, float salary and float
weight while the public members are getinfo() to get employee information and
display() to display employee information. The member functions must be
defined outside the class.
d) Create a class employee whose private members are int age, float salary and float
weight while the public members are getinfo() to get employee information and
display() to display employee information.The member functions must be
defined inside the class.
e) Create a class employee whose private members are int age, float salary and float
weight while the public members are employee () which is a constructor to
initialize employee age, ~employee() a destructure which will destroy employee
object(de-allocate memory), getinfo() to get employee information and display()
to display employee information. The member functions must be defined outside
the class.
f) Discuss the differences between:
i. Private and public data members
ii. Class and union
iii.Destructor and constructor
6.10. Summary
In this lesson, we have learnt that a class is a template used to create objects. An
object is an instance of a class. The objects bind together data and methods that
will act on the data. Examples of classes can be tree, linked list, queue, stack e.t.c.
The members of a class can be classified into data members and member
functions. The member functions can be defined inside class declaration or
outside. Constructor and destructor are two important member functions used to
initialize an object and delete an object from memory respectively.
6.11. Suggested reading
[1]. Data structures using C and C++, 2nd Edition by Yedidyah Langsam, Aaron
J.Augenstein and Aaron M.Tenebaum: Pubslisher: Pearson.
[2]. Data structures and algorithms in c++ by Michael T.Goodrich,Robertio Tamassia and
David Mount: Publisher: Wiley
[3]. Fundamentals of data structures in c++ by Ellis Horowitz,Sartaj Sahni and Dinesh
Mehta. Publisher:Galgotia
[4]. Introduction to data structures and algorithms with c++ by Glenn W.Rowe .
Publisher: Prentice Hall.