Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Chapter 14: Overloading and Templates
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Inline Functions, Operator Overloading and Inheritance COSC 102: Programming in C++
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Case Study - Fractions Timothy Budd Oregon State University.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
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 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.
Chapter 10 Introduction to Classes
P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
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:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Classes C++ representation of an object
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 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Introduction to C++ Systems Programming.
Introduction to Classes
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 Structured Types, Data Abstraction and Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Introduction to Classes
Classes C++ representation of an object
CS 144 Advanced C++ Programming February 21 Class Meeting
Chapter 9 Introduction To Classes
Class rational part2.
Lecture 8 Object Oriented Programming (OOP)
Standard Version of Starting Out with C++, 4th Edition
Classes and Objects Systems Programming.
Presentation transcript:

Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes

Classes The building blocks of object oriented programming Include function members and data members A well designed class is as easy to use as a pre-defined data type

Designing a Class Desired operations –Member functions or methods constructors accessor functions overloaded operators … (functions that do the “work”) Required data members

Writing a Class Definition A class definition has two parts –Class declaration defines name of class, data members, and prototypes for member functions –Class implementation implements the member functions

Class Declaration Name of the class is specified using the key word class Body of the class declaration includes –declaration statements for the data members –function prototypes Keywords public, private and protected specify the accessibility of the class members

Example - Date Class Declaration #include using namespace std; class Date {//class body public: void input(istream&);// I/O methods void print(ostream&) const; void setDate(int mo, int d, int y); int get_day() const;// access methods int get_month() const; int get_year() const; private: int day, month, year; };

Date Class Name of the class is Date Three private data members Six public function members Data members can only be accessed by the member functions

Class Implementation Includes –Function definitions for all function members –Scope Resolution Operator (::) specifies a function as a member of a class

Implementation of Date Class #include “Date.h” … void Date::input(istream& in) {in >> month >> day >> year; } void Date::print(ostream& out) const; {out << month <<‘/’ <<day << ‘/’ << year; } void Date::set_date(int m, int d, int y) {month = m; day = d; year = y; }

Accessor Functions Required to access private data members Complete set should be provided Our Date class requires 3 accessor functions int get_day() const; int get_month() const; int get_year() const; 4const should be placed at the end of any function that is not intended to modify the calling object.

Initializing Objects Constructor functions are used to initialize objects at the time they are declared –Called automatically –Same name as the class name –No return value, not even void

Constructors for the Date Class Default Constructor –constructor with no parameters –Prototype: Date(); Constructor with Parameters –Prototype: Date(int m, int d, int y);

Default Constructor Definition: Date::Date() : month(1), day(1), year(2002) { } or Date::Date() { month = 1; day = 1; year = 2000; } First definition is preferred.

Constructors With Parameters Definition Date::Date(int m, int d, int y):month(m), day(d), year(y) { } or Date::Date(int m, int d, int y) {month = m; day = d; year = y; }

Using Programmer Defined Classes Separate Compilation –Class declaration is saved in a file named classname.h –Class implementation is saved in a file named classname.cpp (or whatever extension your compiler expects) –.h file is included in user program and implementation file –User program is linked to implementation file

Using the Date Class #include “Date.h” int main() {Date today(4,6,2002), birthday; //member functions are called using the dot operator birthday.input(cin); today.print(cout); birthday.print(cout); if(today.get_day() == birthday.get_day() && today.get_month() == birthday.get_month() ) cout << “happy birthday!”

Practice! Given this definition of a class for a rational number implement the default constructor, parameterized constructor, and the input function. class rational {private: int num, denom; public: rational( );// initialize to 1/1 rational(int n, int d); void input (istream & istr ) const; void output (ostream & ostr); // write as num/denom bool improper() const;// true if num >= denom };

Answer to practice! #include "rational.h" rational::rational( ):num(1),denom(1) { } rational::rational(int n, int d):num(n),denom(d) { } void rational::input (istream & istr ) { istr >> num >> denom; }

Operators Assignment operator is defined for objects of the same type Date d1, d2; d1.input(cin); d2 = d1; Other operators are not predefined, but can be overloaded in the class definition(see chapter 10) –arithmetic, relational, logical, input and output

Helper Functions Member functions Called by other member functions Usually specified as private

Example Design a class to implement a unit vector. A vector object will be represented by an anchor point (the base of the arrow), and an orientation(always between 0 and 360 degrees). There are 2 manipulations that you can perform on a vector without changing its length. translate (slide) the vector in the plane, changing its position but not its orientation. rotate a vector, changing its orientation

Translation - change position but not orientation

Rotation - change orientation around anchor point

Class Declaration class UnitVector { public: UnitVector(); // contstructors UnitVector(double init_x, double init_y, double init_orientation); void rotate(double d_orient); // rotate the vector void translate(double dx, double dy); // translate the // vector. private: //helper function void fix_orientation(); // Calculate a legal orientation double x, y; // The anchor point of the object. int orientation; //orientation };

Class Implementation //Constructor functions UnitVector::UnitVector(double initial_x, double initial_y, double initial_orientation) : x(initial_x), y(initial_y), orientation(initial_orientation) { fix_orientation(); } UnitVector::UnitVector( ): x(0), y(0), orientation = 0; { }

Member Function Definitions // rotate the calling vector void UnitVector::rotate(double d_orient) {orientation += d_orient; fix_orientation(); } // translate the calling vector void UnitVector::translate(double dx, double dy) {x += dx; y += dy; }

Helper Function Definition //This function adjusts the orientation value //Original orientation may be = 360) //Adjusted orientation is 0<=orientation < 360. void UnitVector::fix_orientation() { if(orientation < 0) orientation = orientation%360; else orientation = orientation%360; }

Passing Objects To Functions Objects may be passed either "by value" or "by reference" Example: bool UnitVector::ShareBasePoint(UnitVector v)const ( if (x == v.x && y == v.y) return true; else return false; }

Practice! Which of the statements on the right is valid within a main program which contains an include for this header file. class rational {private: int num, denom; int LCD( ); public: rational( ); rational(int n, int d); void input (istream & istr ); void output (ostream & ostr) const; void reduce ( ); };  rational A;  rational B(5, 9);  input(A);  B.output(cout);  int div=B.LCD();  A.denom = 3;  B.reduce;