CSE 2A DAC Abstract Data Types & Group of Programmers at A Software Firm Program

CS 2A Object-Oriented Programming in C++Lesson 15: Classes
SYLLABUS
ASSIGNMENTS
LESSONS
SOLUTIONS
Section 15.1: Introduction to Classes
The concept of a class in C++ is attached to several Computer Science concepts that are more theoretical in nature such as abstract data types, encapsulation, and object-oriented programming. The concepts are important
and you should understand them before we are done. However, I think that a better way to learn about classes is to start from the concrete and move to the abstract, rather than the other way around. So in this lesson I’m
going to show you a class. You’ll need to make sure and read about the more theoretical concepts in chapter 11 of the text.
In order to do this, I’m going to present classes simply as a way for a programmer to define a new type. Until now, every type that we have used has been a pre-defined C++ type. Now we are going to discuss how
programmers can define their own types and then use them. For example, suppose that a group of programmers at a software firm are working on a project that involves a great deal of date processing. Rather than having
three variables to represent a single date (a month, a day, and a year), it would be convenient to have a data type called Date which would, with a single variable, represent a date. Once we have this new data type defined,
we could declare variables of type Date like this:
Date date1;
Date date2;
Each Date variable would store all of the information necessary to represent a single date. The group leader might assign one of the group members the task of defining the new type. That programmer would use a class to
do this.
When we declare a variable of type Date (or any other class), the variable is called an object. Object is a loaded term in C++, but don’t let it confuse you. An object is simply a special kind of variable. If you see or hear the
word “object” and you start to feel panicky because you aren’t sure you really understand objects, try just substituting the word “variable”. Most of the time that will work fine.
To put it another way: A class is a special kind of type. An object is a special kind of variable whose type is a class instead of a simple type.
Let me digress for a moment and point out that we have actually been using classes for a long time already. Although classes can be defined by programmers in situations like the one described above, some classes come
pre-defined. For example, the string data type that we have been using since the first week of the semester is actually not a simple data-type, but is a pre-defined class. Here’s how you can tell the difference: You may
have noticed that the syntax of using string objects (remember: objects == variables) is a bit different than it is for the other data types we have used. For example, when you declare a string object you can then call a
function that operates on that object by putting a dot between the string object and the name of the function. The third line below is an example of this:
string str1;
str1 = “hi there”;
cout

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER