EGR 2261 Unit 13 Classes Read Malik, Chapter 10.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

Chapter 12: Classes and Data Abstraction
Chapter 14: Overloading and Templates
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 11: Classes and Data Abstraction
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CLASSES AND DATA ABSTRACTION
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 12: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
CITA 342 Section 1 Object Oriented Programming (OOP)
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Structures and Classes
Procedural and Object-Oriented Programming
EGR 2261 Unit 11 Pointers and Dynamic Variables
Classes C++ representation of an object
Chapter 13: Overloading and Templates
EGR 2261 Unit 9 One-dimensional Arrays
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Programming: Guided Learning with Early Objects
EGR 2261 Unit 4 Control Structures I: Selection
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Introduction to Classes
Chapter 15: Overloading and Templates
The dirty secrets of objects
User-Defined Classes and ADTs
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Separate Compilation and Namespaces
Introduction to Classes
Classes and Data Abstraction
Object-Oriented Programming Using C++ Second Edition
Chapter 9 Objects and Classes
Chapter 8: User-Defined Classes and ADTs
Introduction to Classes and Objects
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
7 Arrays.
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
Chapter 12: Classes and Data Abstraction
Introduction to Classes and Objects
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
(4 – 2) Introduction to Classes in C++
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

EGR 2261 Unit 13 Classes Read Malik, Chapter 10. Homework #13 and Lab #13 due next week. Final Exam next week. -Handouts: Quiz 12 -Preview final exam.

Review: What is a struct? Recall that a struct is a collection of a fixed number of components (called members), accessed by name. The members may be of different types. structs are very useful because they let us design complex data structures that represent real-world objects. Recall example (on next slide) of houseType. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Review: Example of a struct C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Data Members versus Function Members In our examples so far, structs have contained data members but not function members. In other words, the structs in our examples have contained variables, but not functions. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Limitations of structs In the C language, structs could only contain data members. C programmers came to see this as a limitation, and realized that it would be nice to be able to define structs that contain function members as well as data members. This realization, along with other ideas about how to improve structs, led to the development of C++. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Classes and Object-Oriented Programming The main new idea in C++ is the class, which is a generalization of the struct. Hand-in-hand with the introduction of classes is a whole new way of thinking about a program—as a collection of interacting objects. This new way of thinking is called object-oriented programming (OOP), in contrast to the procedural programming used in C and other older languages. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Defining a Class A class definition looks a lot like a struct definition. Syntax: Just as with struct definitions, a class definition defines a new data type, but no memory is allocated until we declare a variable of this type. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Defining a Class (cont’d.) Each class member in classMemberList can be a variable (data member) or a function (function member). Data members are like the members we’ve used in structs. For a function member, usually just the function prototype is listed is listed in the class definition. The function definition is given elsewhere in the program. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Defining a Class: Example Seven function members Three data members They'll build upon this class in Lab 13. Note the new keywords public and private, which are called member access specifiers.

A New Idea: Code Within a Class The ability to create functions inside a class gives rise to a new distinction that we haven’t had to think about until now: some code (such as the code inside setTime) “lives” inside a class while other code (such as the code in a program’s main function) lives outside of any class.

Member Access Specifiers Each class member falls into one of the following categories, as designated by the access specifiers: public The member can be accessed by code inside or outside the class. private (This is the default.) The member be accessed by code inside the class, but not by code outside the class. protected "In between" public and private. Only needed if you use inheritance (discussed in Chapter 11). Do practice question 1. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Member Access Specifiers: Example Since hr is a private member of clockType, code in clockType’s function members (such as setTime) can access hr, but code outside of the class (such as in main) cannot.

Member Access Specifiers: Example (cont'd.) On the other hand, since setTime() is a public member, any code can call it. In this example, all of the function members are public and all of the data members are private. This is fairly common, but it's not always the case.

Implementation of Member Functions As seen above, usually only the prototypes of a class's function members are included in the class definition itself. Your program must also contain the code for these functions. This code is called the function's implementation. Note the syntax on the next slide, which uses the scope resolution operator :: We previously saw the scope resolution operator when we discussed global variables. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Implementation of Member Functions: Example Class name followed by scope resolution operator This code is outside of the class definition, so you must precede the function's name with clockType:: to indicate that this is a member function of the clockType class, rather than just a regular function.

Separating the Definition from the Implementation Though not required, in programs that use classes we usually separate the code into the following files: A header file that holds the class definition. An implementation file that holds the implementations of the class’s member functions. A file that holds the client program, which uses the class defined in the other two files. Here is where you’ll put your program’s main() function. For a program that uses more than one class, files 1 and 2 will be repeated for each class.

Separating the Definition from the Implementation: Example

Including the Header File in the Other Files On the previous slide, note that the implementation file (clockType.cpp) and the client program (myClockProgram.cpp) include the header file (clockType.h). To do this, we use the same #include preprocessor directive that we use for library files, but we enclose the header file’s name in double quotes instead of angle brackets: #include <iostream> #include "clockType.h" Including a library file Including our own header file

Getting Started with a Program that Uses clockType Create a new project named clockPractice and start a new source-code file named clockPractice.cpp. Download the files named clockType.h and clockType.cpp from the website and place them in your project’s folder that contains clockPractice.cpp. In your project’s Solution Explorer, right-click Source Files and select Add > Existing Item…. Then select clockType.cpp and click Add. In clockPractice.cpp, type the code shown on the next slide.

Getting Started with a Program that Uses clockType (cont’d.) Run the program. Modify main to call myClock.incrementSeconds() and then call myClock.printTime() again.

Variable (Object) Declaration As we’ve seen, once a class has been defined and implemented, you can declare variables of that class type: clockType myClock; A class variable is called a class object or class instance, or simply an object. Each object has its own copy of the data members (hr, min, and sec). C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Accessing Class Members From Outside Code outside of the member functions (such as in the main() function of the client program) can access an object's public members, using the syntax: The dot (.) is the member access operator (same as for structs). But code outside of the member functions cannot access an object's private members. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Accessing Class Members From Outside: Example Accessing myClock's public members: okay! Trying to access myClock's private members: no good! Demo with week13Class.cpp. This code is in the client program's main() function, so it is outside of the class's member functions.

Accessing Class Members Within the Class Code Code within a member function of the class can access the object's public and private members directly by name (no dot operator needed). C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Accessing Class Members Within the Class Code: Example Accessing an object's private members: okay! Demo with week13Class.cpp. Do practice question 2 and use week13practiceCircle.cpp to see error messages. This code is in the member function printTime(), so it can access public and private members. Note that we don’t need to precede the member name with the object's name.

Constructors A constructor is a special member function that runs automatically when a new class object is created. Its purpose is to initialize the object’s data members. A constructor’s name must be the same as the name of the class. A constructor has no type (not even void). Unlike other functions, constructors cannot be explicitly called in code. Recall that most variables are uninitialized when you declare them—this can lead to problems. Show that you can call printTime() on a clockType object as soon as you declare it, even if you haven’t called setTime() on it.. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Constructors: Example In the class’s header file: In the class’s implementation file: On previous slides I omitted this constructor to avoid confusion. -Demo by playing around with the values that hr, min, sec are initialized to.

Constructors (cont’d.) Constructors are functions, but they serve a very special purpose. They run automatically whenever you declare a class object. You do not call them directly in your code using the usual function-call syntax. This main() code is illegal because it tries to call a constructor. Constructor is called here. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Constructors (cont’d.) A class can have more than one constructor. This is an example of function overloading, which we looked at briefly in Unit 7. Recall from Unit 7 that each overloaded function must have a different formal parameter list. Example of overloaded constructor functions: clockType(); clockType(int hours, int minutes, int seconds); C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Multiple Constructors: Example In the class’s header file: In the class’s implementation file: Demo using week13Constructors.cpp. (But the file they’re using from the website just has the default constructor.) Do practice question 3.

Invoking a Constructor A constructor is automatically executed when you declare a class object. Which of the constructors is executed depends on whether you specify parameters in parentheses after the class object’s name when you declare it. Examples: clockType yourClock; clockType myClock(4, 3, 16); C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Invoking the Default Constructor To invoke the default constructor, don’t specify any parameters after the class object’s name when you declare it: Example: clockType yourClock; C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Invoking a Constructor with Parameters Syntax: Example: clockType myClock(4, 3, 16); The number and type of arguments should match the formal parameters (in the order given) of one of the constructors. Do practice question 4. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Constructors and Default Parameters A constructor can have default parameters. The rules for declaring formal parameters are the same as for declaring default formal parameters in any function. (Recall Unit 7.) Actual parameters are passed according to the same rules used for any function. Default constructor: a constructor with no parameters or with all default parameters. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Built-in Operations on Classes As with structs, aggregate arithmetic, relational, and input/output operations are not allowed on class objects, but aggregate assignment is allowed: clockType myClock, yourClock; myClock = yourClock + 1; //Error if (myClock >= yourClock) //Error cout << myClock; //Error cin >> myClock; //Error myClock = yourClock; //Okay!! C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Arrays, structs, and Class Objects: A Comparison On this slide I’ve erased the comment that says “(except strings)” in the Array column of the Input/output row because we skipped C-strings. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Class Objects and Functions Objects can be passed as parameters to functions by value or by reference. If an object is passed by value, a copy must be made of the object’s data members. For large objects, this may require a lot of time and memory, degrading performance. If an object is passed by reference, no copy is made—may result in better performance. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Class Objects and Functions (cont’d.) Passing by reference is an efficient way to pass an object as a parameter. Potential Problem: when passing by reference, the actual parameter is changed if the formal parameter is changed. Solution: use const in the formal parameter list. This prevents the function from changing the parameter’s value. Example from clockType: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Another Use of const The previous slide showed how to use const to prevent a function from changing the value of a reference parameter. Another use of const is to prevent a function from changing the object on which the function is invoked. Example from clockType: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Two Uses of const: Example This is an error because of this const. Demo using week13Const.cpp. This is an error because of this const.

Arrays of Class Objects Similar to the arrays of structs that we examined at the end of Unit 11 and used in the third program on Lab 11. C++ Programming: From Problem Analysis to Program Design, Seventh Edition