Chat with us, powered by LiveChat Tarrant County College District Lab Programming Fundamentals Worksheet - Uni Pal

DescriptionCOSC 1437 Programming Fundamentals II
By FirstName LastName
Introduction to Classes
To introduce object-oriented programming
To introduce the concept of classes
To introduce the concept of constructors and destructors
To introduce arrays of objects
Students should read Chapter 13 of the text.
Students should read the Pre-lab Reading Assignment before coming to lab.
Students should complete the Pre-lab Writing Assignment before coming to lab.
In the lab, students should complete labs assigned to them by the instructor.
Pre-lab Reading Assignment
Chapter 13 of text
20 min.
Pre-lab Writing Assignment
Pre-lab reading
10 min.
Basic understanding of
structures and classes
10 min.
Completion of Pre-lab
Reading Assignment
40 min.
Understanding of private 20 min.
data members of
classes and files
Understanding of
Lab 13.1
Square as a Class
Lab 13.2
Circles as a Class
Lab 13.3
Arrays as Data Members
of Classes
Lab 13.4
Arrays of Objects
20 min.
Introduction to Classes
P R E – LA B R E A D I N G A S S I G N M E N T
Introduction to Object-Oriented Programming
Up until now, we have been using the procedural programming method for writing all our programs. A procedural program has data stored in a collection of variables (or structures) and has a set of functions that perform certain operations.
The functions and data are treated as separate entities. Although operational,
this method has some serious drawbacks when applied to very large real-world
situations. Even though procedural programs can be modularized (broken into
several functions), in a large complex program the number of functions can
become overwhelming and difficult to modify or extend. This can create a level of complexity that is difficult to understand.
Object-Oriented Programming (OOP) mimics real world applications by
introducing classes which act as prototypes for objects. Objects are similar to
nouns which can simulate persons, places, or things that exist in the real world.
OOP enhances code reuse ability (use of existing code or classes) so time is not
used on “reinventing the wheel.”
Classes and objects are often confused with one another; however, there is
a subtle but important difference explained by the following example. A plaster
of Paris mold consists of the design of a particular figurine. When the plaster is
poured into the mold and hardened, we have the creation of the figurine itself.
A class is analogous to the mold, for it holds the definition of an object. The
object is analogous to the figurine, for it is an instance of the class. Classes and
structures are very similar in their construction. Object-oriented programming is
not learned in one lesson. This lab gives a brief introduction into this most important concept of programming.
A class is a prototype (template) for a set of objects. An object can be
described as a single instance of a class in much the same way that a variable is
a single instance of a particular data type. Just as several figurines can be made
from one mold, many objects can be created from the same class. A class consists of a name (its identity), its member data which describes what it is and its
member functions which describe what it does.1 Member data are analogous to
nouns since they act as entities. Member functions are analogous to verbs in that
they describe actions. A class is an abstract data type (ADT) which is a user
defined data type that combines a collection of variables and operations. For
example, a rectangle, in order to be defined, must have a length and width. In
practical terms we describe these as its member data (length, width). We also
describe a set of member functions that gives and returns values to and from the
member data as well as perform certain actions such as finding the rectangle’s
perimeter and area. Since many objects can be created from the same class, each
object must have its own set of member data.
As noted earlier, a class is similar to a structure except that classes encapsulate
(contain) functions as well as data.2 Functions and data items are usually designated
In other object-oriented languages member functions are called methods and member data
are called attributes.
Although structures can contain functions, they usually do not, whereas classes always
contain them
Pre-lab Reading Assignment 245
as either private or public which indicates what can access them. Data and functions that are defined as public can be directly accessed by code outside the
class, while functions and data defined as private can be accessed only by functions belonging to the class. Usually, classes make data members private and
require outside access to them through member functions that are defined as
public. Member functions are thus usually defined as public and member
data as private.
The following example shows how a rectangle class can be defined in C++:
using namespace std;
// Class declaration (header file)
class Rectangle // Rectangle is the name of the class (its identity).
// The following are labeled as public.
// Usually member functions are defined public
// and are used to describe what the class can do.
void setLength(float side_l);
// This member function receives the length of the
// Rectangle object that calls it and places that value in
// the member data called length.
void setWidth(float side_w);
// This member function receives the width of the Rectangle
// object that calls it and places the value in the member
// data called width.
float getLength();
// This member function returns the length of the Rectangle
// object that calls it.
float getWidth();
// This member function returns the width of the Rectangle
// object that calls it.
double findArea();
// This member function finds the area of the Rectangle object
// that calls it.
double findPerimeter();
// This member function finds the perimeter of the Rectangle
// object that calls it.
Introduction to Classes
The following are labeled as private.
Member data are usually declared private so they can
ONLY be accessed by functions that belong to the class.
Member data describe the attributes of the class
float length;
float width;
This example has six member functions. It has two member functions for each
private member data: setLength and getLength for the member data length
and setWidth and getWidth for the member data width. It is often the case that
a class will have both a set and a get member function for each of its private data
members. A set member function receives a value from the calling object and
places that value into the corresponding private member data. A get member
function returns the value of the corresponding private member data to the object
that calls it. In addition to set and get member functions, classes usually have other member functions that perform certain actions such as finding area and perimeter in the Rectangle class.
Client and Implementation Files
It is not necessary for someone to understand how a television remote control
works in order to use the remote to change the stations or the volume. The user
of the remote could be called a client that only knows how to use the remote
to accomplish a certain task. The details of how the remote control performs
the task are not necessary for the user to operate the remote. Likewise, an automobile is a complex mechanical machine with a simple interface that allows
users without any (or very little) mechanical knowledge to start, drive, and use
it for a variety of functions. Drivers do not need to know what goes on under the
hood. In the same way, a program that uses Rectangle does not need to know
the details of how its member functions perform their operations. The use of an
object (an instance of a class) is thus separated into two parts: the interface
(client file) which calls the functions and the implementation which contains
the details of how the functions accomplish their task.
An object not only combines data and functions, but also restricts other parts
of the program from accessing member data and the inner workings of member
functions. Having programs or users access only certain parts of an object is
called data hiding. The fact that the internal data and inner workings can be hidden from users makes the object more accessible to a greater number of programs.
Just like an automobile or a remote control, a piece of commercial software
is usually a complex entity developed by many individuals. OOP (Object-Oriented
Programming) allows programmers to create objects with hidden complex logic
that have simple interfaces which are easily understood and used. This allows
more sophisticated programs to be developed. Interfacing is a major concern
for software developers.
Pre-lab Reading Assignment 247
User of an object
Private Internal Data
(length, width)
Implementation of the member functions
Types of Objects
Objects are either general purpose or application-specific. General purpose
objects are designed to create a specific data type such as currency or date. They
are also designed to perform common tasks such as input verification and graphical output. Application-specific objects are created as a specific limited operation
for some organization or task. A student class, for example, may be created for
an educational institution.
Implementations of Classes in C++
The class declaration is usually placed in the global section of a program or in a
special file (called a header file). As noted earlier, the class declaration acts very
much like a prototype or data type for an object. An object is defined much like
a variable except that it uses the class name as the data type. This definition creates an instance (actual occurrence) of the class. Implementation of the member functions of a class are given either after the main function of the program
or in a separate file called the implementation file. Use of the object is usually in the main function, other specialized functions, or in a separate program file
called the client file.3
Creation and Use of Objects
Rectangle, previously described, is a class (prototype) and not an object (an
actual instance of the class). Objects are defined in the client file, main, or oth-
er functions just as variables are defined:
Rectangle box1,box2;
box1 and box2 are objects of class Rectangle.
box1 has its own length and width that are possibly different from the length
and width of box2.
To access a member function (method) of an object, we use the dot operator, just
as we do to access data members of structures. The name of the object is given first,
followed by the dot operator and then the name of the member function.
The following example shows a complete main function (or client file) that
defines and uses objects which call member functions.
int main()
Rectangle box1;
Rectangle box2;
// box1 is defined as an object of Rectangle class
// box2 is defined as another Rectangle class object
More will be given on header, implementation, and client files later in the lesson.
Introduction to Classes
box1.setLength(20); // This instruction has the object box1 calling the
// setLength member function which sets the member data
// length associated with box1 to the value of 20
box2.setLength(9.5);// This instruction has the object box2 calling the
// setLength member function which sets the member data
// length associated with box2 to the value of 9.5
Purchase answer to see full

error: Content is protected !!