Classes and Objects
Classes and Objects
(LAB-02)
Page 1
Introduction to Classes and Objects
Table of Contents
1. Introduction 15
4. Concept Map 16
4.1 Object 16
4.2 Data Members 16
4.3 Member Functions 16
4.4 Constant Member Functions 17
4.5 Class 17
4.6 Encapsulation 17
7. Practice Tasks 20
7.1 Practice Task 1 20
7.2 Practice Task 2 20
7.3 Practice Task 3 20
7.4 Outcomes 20
7.5 Testing 20
9. Evaluation Criteria 21
Lectures: 3, 4
Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert Lafore
o Pages: 195-201
4. Concept Map
4.1 Object
In OOP an object is a very much like a real world object. An object can be defined as a collection of state and
behaviour. For example, consider the example of a cat. A cat is has both a state and behaviour. The state of a cat is
its attributes namely colour, breed, gender, age, weight, height, etc. Whereas the behaviour of a cat is its sound, eating,
sleeping, yawning, walk, etc. Hence a cat can be completely identified by its unique characteristics and behaviours.
In programming an object is a collection of variables and functions that together have a unique purpose ofidentifying
a distinctive entity.
Again referring to the concept of an object and the example of a cat. The cat had a number of characteristics or
attributes that can be used to identify a cat. In programming these attributes can be programmed by using regular
variables that are called data members. A class can be composed of a number of data members of various types. The
data members of a class are private by default i.e. they are not directly accessible outside the class.
Here it is important to point out that often people casually refer to these as variables, which is a wrong terminology.
These should only be called data members or class variables.
Again referring to the concept of an object and the example of a cat. The cat had a number of behaviours or things
that a cat does. In programming these behaviours can be programmed by using functions that are called member
functions. A class can be composed of a number of member functions of various types. Overloading of member
functions is also permitted in C++. The implementation section of member functions can be separated whereby the
body of the function is created outside the class but the function prototype has to exist in the body of the class. The
implementation section is separated by writing the return type followed by class name. This is further followed by
scope resolution operator :: and then the remaining function definition.
int datamember;
void main( )
4.5 Class
A class is a collection of data members and member functions. A class is used to define an abstract data type. This
abstract data type is used in the construction of an object which is used to access all the data members and member
functions. A class has to be created before it can be used. Provided below are the syntax for creating a class.
4.6 Encapsulation
Encapsulation is a very important design goal because of which OOP is preferred over conventional programming.
Encapsulation is a quality because of which data and function are stored in a single unit commonly known as a class.
This unit/ bundle ensures that the data is not openly accessible to the outer world. The access is provided by the
functions that are part of the unit/bundle. This means that the functions work like doors in a room. You can prevent
thieves from breaking in. Only those people can enter who come through the door.
5.2.1 Task-1
Identify the data members and member functions for a pizza class. The class should have all the relevant attributes
and qualities required for a pizza object. Try to be imaginative in your design.
6.1 Tools
Visual Studio 2008.
Figure 1: Class for creating a cylinder with its relevant data members
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\Lab02
7.4 Outcomes
After completing this lab, students will be able to design a basic class with data members and member functions.
7.5 Testing
Test Cases for Practice Task-1
Sample Inputs Sample Outputs
Set the following values Brand = DELL
Brand = DELL Model = 1565D
Model = 1565D Serial = 123456
Serial = 123456 Colour = Silver
Colour = Silver Price = 64500.5
Price = 64500.5 Processor = 2.8
Processor = 2.8 RAM = 3
RAM = 2 Screen = 15.6
Screen = 15.6
Number: 5 Number = 5
Factorial = 120
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$\