Classes “Absolute C++” Chapter 6

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Chapter 9 Technicalities: Classes, etc. Bjarne Stroustrup
Lecture 18 Templates, Part II. From Last Time: What is a Template? This is the “official” specification for a template. It says that to define a template.
Pointers “Absolute C++” Section 10.1
Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Lecture 5 Constructors Operator Overloads “Absolute C++” Chapters 7.1,8.1,8.2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Lecture 10 Inheritance “Absolute C++” Chapter 14.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Defining New Types Lecture 21 Hartmut Kaiser
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Organizing Heterogeneous Data Arrays allow a programmer to organize lists of values that are all of the same type (homogeneous). But we are often faced.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Classes and Data Structures Jeffrey S. Childs
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Andrew(amwallis) Classes!
Friend functions, operator overloading
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Motivation and Overview
Review: Two Programming Paradigms
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Lecture Set 4 Data Types and Variables
#include "std_lib_facilities
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Lecture 4-7 Classes and Objects
Separate Compilation and Namespaces
User Defined Functions
Defining Classes and Methods
Chapter 9 Objects and Classes
Code Organization Classes
Defining New Types Lecture 22 Hartmut Kaiser
Classes and Objects.
Writing Functions.
Namespaces How Shall I Name Thee?.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Tonga Institute of Higher Education
COMS 261 Computer Science I
Lab4 problems More about templates Some STL
CS 144 Advanced C++ Programming January 31 Class Meeting
Agenda Warmup Lesson 2.2 (parameters, etc)
CS 144 Advanced C++ Programming February 21 Class Meeting
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
The Three Attributes of an Identifier
Code Organization Classes
Miscellaneous Topics I
Lecture 3 More on Flow Control, More on Functions,
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Week 9 - Monday CS222.
Presentation transcript:

Classes “Absolute C++” Chapter 6 Lecture 4 Classes “Absolute C++” Chapter 6

Introduction to Classes Last lecture, we talked about streams and used functions called member functions. These functions operate on the variable they are “attached to” ifstream inFile; inFile.open(“input.dat”); if (inFile.fail()) // fail is a member function { cout << “Couldn’t open file” << endl; } inFile is actually an instance of the ifstream class. But what is a class?

Classes What is a class? A very simple definition: “A class is a user-defined type” Are all user-defined types classes? No. C++ supports the C notion of a “struct” A struct allows programmers to define their own data type structures Similar to RECORDs in Pascal Are all classes user-defined types? Yes There are no “built in classes” in C++ There are provided standard class libaries iostream string OK, that’s great. But, what is a class?

Classes (cont) A class is a traditional data structure with a set of functions. Let’s start with a simple C style structure definition typedef struct{ string name; string instructor; int numStudents; } Course; Once defined I could use this “user defined” data type anywhere in my code: int main() { Course cs213; cs213.name = “COM S 213”; cs213.instructor = “DiNapoli”; cs213.numStudents = 45; }

Classes (cont) Now, where do these functions fit in? The functions (called member functions) are tied to the data structure Any “field” of the data structure may be accessed by any member function as if it were in a global scope. Let’s take a look at this before we go any further... class Course { public: // Define member functions int getStudentCount() { return numStudents; } // Define member variables string name; string instructor; int numStudents; };

Demonstration #1 Very Simple Class

Classes: Public vs. Private Why bother with simple functions like getStudentCount() ? It’s a bad idea to directly access member variables Circumvent error checking, easy to screw up data. Can’t I just use the member variables directly anyway? class Course { public: // These can be seen outside the class // Define member functions int getStudentCount() { return numStudents; } private: // These can be seen inside the class only // Define member variables string name; string instructor; int numStudents; };

Demonstration #2 Public vs. Private

Classes: Public vs. Private (cont) OK, so how do I access private data outside of the class? You don’t, that’s the whole idea! You can use get/set functions (public) to return the values for you class Course { public: // These can be seen outside the class // Define member functions string getCourseName() { return name; } string getInstructor() { return instructor; } int getStudentCount() { return numStudents; } void setCourseName(string theName) { name = theName; } void setInstructor(string theInstructor) { instructor = theInstructor; } void setStudentCount(int count) { numStudents = count; } private: // These can be seen inside the class only ...

Access functions (getters/setters) Demonstration #3 Access functions (getters/setters)

Classes: Lots of Member Functions Doesn’t the class get unruly with all of those member functions? Not really. The class definition only needs to have function declarations, not definitions. class Course { public: // These can be seen outside the class // Define member functions string getCourseName(); string getInstructor(); int getStudentCount(); void setCourseName(string theName); void setInstructor(string theInstructor); void setStudentCount(int count); private: // These can be seen inside the class only ...

Classes: Lots of Member Functions Alright, declarations are cool, but then where do the member functions get defined? Anywhere you want them to be defined :-) No, seriously, with the help of some added notation they can be defined just about anywhere... string Course::getCourseName() { return name; } int Course::getStudentCount() { return numStudents; } Note the use of Course:: to specify the class in question Note how I’m using member variables as if they were some sort of global variable

Member Function Definitions Demonstration #4 Member Function Definitions

Classes: More on Public vs. Private The public and private labels can appear as many times as you want them to in a class definition. class Course { public: // These can be seen outside the class // Getter functions string getCourseName(); string getInstructor(); int getStudentCount(); public: // Setter functions void setCourseName(string theName); void setInstructor(string theInstructor); void setStudentCount(int count); private: // These can be seed inside the class only // Member variables ...

Classes: More on Public vs. Private Member functions can be private as well. class Course { public: // These can be seen outside the class // Getter and Setter functions string getCourseName(); string getInstructor(); int getStudentCount(); void setCourseName(string theName); void setInstructor(string theInstructor); void setStudentCount(int count); private: // These can be seed inside the class only // private member functions bool validateStudentCount(int count); ...

Classes: More on Public vs. Private You can still have public member variables If no public or private label is specified, private is assumed class Course { bool validateStudentCount(int count); // implicit private public: bool isFull; // publicly accessible member variable // Getter and Setter functions string getCourseName(); string getInstructor(); int getStudentCount(); void setCourseName(string theName); void setInstructor(string theInstructor); void setStudentCount(int count); ...

Where should we Define Member Functions? How do you know when to define a member function in the class definition vs defining it outside of the class definition? There is a simple technical explanation. I’m not going to tell you yet :-) A good rule of thumb is: If the definition is simple (one line of code) you should define it in the class definition. Getter/Setter functions are prime examples. Otherwise, define outside of the class definition, usually in a separate file.

What Files Should These Definitions Go In? // Course.h -- Header file for Course class class Course { public: // These can be seen outside the class // Define member functions string getCourseName(); string getInstructor(); int getStudentCount(); void setCourseName(string theName); void setInstructor(string theInstructor); void setStudentCount(int count); private: // These can be seed inside the class only string name,instructor; int count; };

What Files Should These Definitions Go In? // Course.cpp -- Definition file for Course class #include “Course.h” string Course::getCourseName() { return name; } String Course::getInstructor() { return instructor; String Course::getStudentCount() return count; // etc., etc.

Lecture 4 Final Thoughts…