Access Specifiers, Constructors and Destructors
Access Specifiers, Constructors and Destructors
(LAB-03)
Access Specifiers, Constructors and Destructors
Table of Contents
1. Introduction 24
4. Concept Map 25
4.1 Access Specifiers – Public and Private Access 25
4.2 Constructors 25
4.3 Destructors 26
7. Practice Tasks 29
7.1 Practice Task 1 29
7.2 Outcomes 30
7.3 Testing 30
9. Evaluation Criteria 31
Page 2
University of Management and Technology
Department of Computer Science, SST
Access Specifiers, Constructors and Destructors
Lectures: 3, 4, 5, 6
Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert Lafore
o Pages: 201-212
Page 3
University of Management and Technology
Department of Computer Science, SST
Access Specifiers, Constructors and Destructors
4. Concept Map
Access specifier also known as access modifier provides a technique for enforcing access control to class members
(data members and member functions). The use of access specifiers enforces encapsulation and data hiding. C++
provides three access specifiers i.e. public, private and protected. In this lab we will only cover the public and the
private access specifier. The protected specifier is left for future discussions.
The public access specifier is the one that provides unrestricted access to the class members. While the private access
specifier provides a very strict/ restricted access to class members. All the class members that are written under the
public access can be accessed both inside and outside the class without any restriction. On the other hand all the class
members written as private are accessible inside the class but are not accessible outside the class. The best and most
common way of accessing private data members is through the use of a public functions.
When we are discussing access specifiers it must be pointed out that by default classes are private whereas structures
are public. Hence if you do not write private then your listed class members are considered private in a class.
The correct convention for the use of access specifiers is that data members are kept private whereas functions are
kept public. Hence you are providing a public interface for accessing restricted items in a class.
private:
int datamember; //Private data member
public:
int memberfun(int); // public member function
};
int myclass :: memberfun (int x) // This function is still public because its prototype is public
datamember=x;
void main( )
myclass obj;
//Syntax Error: private member
obj.memberfun(10);
4.2 Constructors
A constructor is a function that is automatically called when an object is created. This function can exhibit the regular
behaviour of any function. The reason why constructors are needed is that unlike regular functions which need to
deliberately called, a constructor will be automatically called when an object is created. Every constructor has a body
from where we can call regular member functions.
A very important question which is often asked is that how does the compiler know that the constructor function
Page 4
University of Management and Technology
Department of Computer Science, SST
Access Specifiers, Constructors and Destructors
needs to be called automatically? The answer is very simple. A constructor is a function that has the same name as the
class. Whenever an object is created the compiler searches for a function having the same name as the class i.e. the
constructor. Given below is a sample code that shows the class constructor. Generally the constructor is defined as
public. Also the constructor can be overloaded like a regular member function. An important point regarding a
constructor is that it cannot return a value. In fact writing the keyword void is strictly prohibited.
private:
int datamember; //Private data member
public:
myclass( ); //Class constructor
};
void main( )
myclass obj;
4.3 Destructors
Constructors are designed to help initialize/ create an object. Destructors on the other hand do exactly the opposite.
Destructors are called whenever an object goes out of scope. When this happens it is necessary to perform cleanup
procedures especially when you have used dynamic memory or you have been working with pointers in your code.
The destructor function can be used to free up memory that you have allocated or dereference pointers that were
referenced. The rules for a destructor are as follows:
They have the same name as the class just simply preceded by a tilde (~)
They can take no arguments
They cannot return anything, not even void.
private:
int datamember; //Private data member
public:
myclass( ); //Class constructor
};
Page 5
University of Management and Technology
Department of Computer Science, SST
Access Specifiers, Constructors and Destructors
class student
{
int age;
int cnic;
int semester;
char name;
public:
int setall(int a, int c, int s, int sem, char n) const;
{
age=a;
c=cnic;
semester=s;
name=n;
}
}
void main( )
{
Student obj;
obj::setall( );
obj.displayall( );
obj.setage( );
Student anotherobj;
Student::anotherobj::setall( );
}
6.1 Tools
Visual Studio 2008.
Page 6
University of Management and Technology
Department of Computer Science, SST
Access Specifiers, Constructors and Destructors
class pizza
{
private:
int size, price, thickness;
string topping;
public:
void setsize()
{
cout<<"Enter size of pizza: ";
cin>>size;
}
void setprice()
{
cout<<"Enter price of pizza: ";
cin>>price;
}
void setthickness()
{
cout<<"Enter thickness of pizza: ";
cin>>thickness;
}
void settopping()
{
cout<<"Enter toppings of pizza: ";
cin>>topping;
}
void display() const
{
cout<<"The ordered pizza details are: ";
cout<<"\nSize: "<<size;
cout<<"\nPrice: "<<price;
cout<<"\nTopping:"<<topping;
cout<<"\nThickness:"<<thickness<<"\n";
}
pizza() //class constructor: cannot have a return type
{
setsize();
setprice();
setthickness();
settopping();
}
};
void main()
{
pizza obj;
obj.display( );
}
Figure 1: The pizza class demonstrating the use of a constructor
Page 7
University of Management and Technology
Department of Computer Science, SST
Access Specifiers, Constructors and Destructors
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to finish the
tasks in the required time. When you finish them, put these tasks in the following folder:
\\dataserver\assignments$\OOP\Lab03
LeCream allows its customers to purchase a vanilla wafer with their ice cream. If the customer wants to
purchase the wafer he will have to pay an additional Rs 10. This amount should be added to the total amount
payable by the user.
If the customer asks for chocolate flavour then he will have to pay an additional amount i.e. Rs 120 for two
scoops and Rs 180 for three scopes. Design a function that will be called if the customer chooses flavoured
ice cream.
The program should show a menu that asks the customer for his requirements and then displays the final
payable amount with full details about the flavour, number of scoops and wafer
Page 8
University of Management and Technology
Department of Computer Science, SST
Access Specifiers, Constructors and Destructors
In the end create a class destructor that displays a thank you message to the user.
Design your program using sound OOP practices. Carefully determine the data members, member functions, access
specifiers, activities to be performed in the constructor. Make sure that you use good naming conventions in your
code. A good design can earn you higher marks.
7.2 Outcomes
After completing this lab, students will be able to design a class that correctly implements class members by observing
access specifiers. The students will also be familiar with the use of a constructor and destructor.
7.3 Testing
Test Cases for Practice Task-1
Sample Inputs Sample Outputs
Flavour = chocolate Your choice of ice cream is as follows
Wafer required = yes Flavour = chocolate
Number of scoops = 3 Number of scoops = 3
Wafer is required
Total price is 190
Page 9
University of Management and Technology
Department of Computer Science, SST
Exception Handling
9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is assigned
marks which will be evaluated by the instructor in the lab depending on the accomplishment of the assigned tasks.
10.1 Books
- Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\
Page 32
University of Management and Technology
Department of Computer Science, SST