ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
1 Welcome  CSCI Object-Oriented Design and Programming Mon. and Wed., 6:00–8:29 p.m.  Instructor: Charles Moen Web page.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 16 is a copy of C++ How to Program Chapter.
Chapter 3 Introduction to Classes, Objects and Strings C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects and Strings
Introduction to Classes and Objects
Dr Shahriar Bijani Winter 2017
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
COMS 261 Computer Science I
Classes, Objects, Methods and Strings
Object Oriented Programming in java
Visual Programming Lecture 4.
Introduction to Classes and Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Classes and Objects Systems Programming.
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes

Lecture outline Announcements / reminders  Lab 3 due Monday, 10/01 Last time  Data members  Set/get functions Today  Constructors  Using classes 4/16/2015 ECE 264: Lecture 10 2

Classes: Programmer Defined Types Classes allow programmer to define their own types  Objects: instances of a class Each class typically contains  Data members: attributes for each object Each object has own copy of data members  Member functions: Tasks specific to class Discussed “set” & “get” (mutator/accessor) functions Data/functions can be public or private  Private members only accessible within member functions  Private functions also known as helper functions 4/16/2015 ECE 264: Lecture 10 3

Example: data members (GradeBook.h) // Adapted from Fig. 3.5: fig03_05.cpp // NOTE: See web for #includes // GradeBook class interface class GradeBook { public: // function that sets the course name void setCourseName( string courseName ); // function that gets the course name string getCourseName(); // function that displays a welcome message void displayMessage(); private: string name; // course name for this GradeBook }; // end class GradeBook 4/16/2015 ECE 264: Lecture 10 4

Example: data members (GradeBook.cpp) // Adapted from Fig. 3.5: fig03_05.cpp // NOTE: See web for #includes // GradeBook class implementation #include “GradeBook.h” // function that sets the course name void GradeBook::setCourseName( string courseName ) { name = courseName; // store the course name in the object } // end function setCourseName // function that gets the course name string GradeBook::getCourseName() { return name; // return the object's courseName } // end function getCourseName // function that displays a welcome message void GradeBook::displayMessage() { cout << "Welcome to the grade book for\n" << name << "!" << endl; } // end function displayMessage 4/16/2015 ECE 264: Lecture 10 5

Example (cont.) // function main begins program execution int main() { string nameOfCourse; // string of characters to store the course name GradeBook myGradeBook; // create a GradeBook object named myGradeBook // display initial value of courseName cout << "Initial course name is: " << myGradeBook.getCourseName() << endl; // prompt for, input and set course name cout << "\nPlease enter the course name:" << endl; getline( cin, nameOfCourse ); // read a course name with blanks myGradeBook.setCourseName( nameOfCourse ); // set the course name cout << endl; // outputs a blank line myGradeBook.displayMessage(); // display message with new course name return 0; // indicate successful termination } // end main 4/16/2015 ECE 264: Lecture 10 6 Initial course name is: Please enter the course name: ECE 264 Welcome to the grade book for ECE 264! Input/Output:

Constructors  Functions used to initialize an object’s data when it is created Call made implicitly when object is created Must be defined with the same name as the class Cannot return values  Not even void  Default constructor has no parameters The compiler will provide one when a class does not explicitly include a constructor  Compiler’s default constructor only calls constructors of data members that are objects of classes 4/16/2015 ECE 264: Lecture 10 7

Example: constructors (GradeBook.h) // NOTE: See web for #includes // GradeBook class interface class GradeBook { public: GradeBook(); // Default constructor GradeBook(string courseName); // Parameterized constructor // function that sets the course name void setCourseName( string name ); // function that gets the course name string getCourseName(); // function that displays a welcome message void displayMessage(); private: string courseName; // course name for this GradeBook }; // end class GradeBook 4/16/2015 ECE 264: Lecture 10 8

Example: constructors (GradeBook.cpp) Add the following to GradeBook.cpp: // Default constructor GradeBook() { courseName = “”; } // Parameterized constructor GradeBook(string courseName) { name = courseName; } 4/16/2015 ECE 264: Lecture 10 9

Example (cont.) // function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; return 0; // indicate successful termination } // end main 4/16/2015 ECE 264: Lecture gradeBook1 created for course: CS 101 Introduction to C++ Programming gradeBook2 created for course: CS 102 Data Structures in C++ Output:

Examples: using classes Using GradeBook, which statements would be valid in a main() program written in the same file? #include using std::string; class GradeBook { public: GradeBook( ); GradeBook( string name ); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string name; }; // end class GradeBook 4/16/2015 ECE 264: Lecture a.GradeBook g1(264); b.GradeBook g2; c.setCourseName(g2); d.g2.name = “ECE 461”; e.string s = g2.getCourseName(); f.g2.displayMessage;

Examples: using classes a. GradeBook g1(264);  Invalid—constructor takes string as argument  Valid alternative: GradeBook g1(“264”); b. GradeBook g2;  Valid—creates new GradeBook object using default constructor c. setCourseName(g2);  Invalid—improper way to call member function  Valid alternative: g2.setCourseName(“ECE 264”); 4/16/2015 ECE 264: Lecture 10 12

Examples: using classes (cont.) d. g2.name = “ECE 461”;  Invalid— name is private data  Must use public “set” function to assign value e. string s = g2.getCourseName();  Valid—correct syntax for calling member function, and type for s matches return type for getCourseName(); f. g2.displayMessage;  Invalid— displayMessage is a function and therefore needs parentheses after the function name: g2.displayMessage(); 4/16/2015 ECE 264: Lecture 10 13

Example: Creating a class Say we have a class to represent a point in a 2-dimensional plane  What data should this class hold?  What should the constructor look like?  How would we write the mutator function(s)?  How would we write the accessor function(s)? 4/16/2015 ECE 264: Lecture 10 14

Final notes Next time Code examples: creating a class Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 4/16/2015 ECE 264: Lecture 10 15