Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining Classes.
Copyright © 2003 Pearson Education, Inc. Slide 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining 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.
Chapter 10 Introduction to Classes
Chapter 10 Defining Classes. Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
1 Chapter 10 Defining Classes. 2 Overview Introduction Classes (10.2) Abstract Data Types and Structures (10.1)(10.3) Abstract Data Types and Structures.
Chapter 8 Operator Overloading, Friends, and References.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 6 Defining Classes. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Introduction Structures (6.1) Classes (6.2)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
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.
C/C++ Basics (V) Structures and Classes Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing.
COMP 53 – Week Two Operator Overloading.
Structures and Classes
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
COMP 51 Week Twelve Classes.
More about OOP and ADTs Classes
Classes and Data Abstraction
More about OOP and ADTs Classes
Learning Objectives Classes Constructors Principles of OOP
Constructors and Other Tools
Classes C++ representation of an object
Presentation transcript:

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member data

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Topic 1: Classes  Similar to structures  Adds member FUNCTIONS  Not just member data  Object-oriented programming  Class: Abstract data type that contains data and operations  Object: Instances of a class

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-3 Class Definitions  Similar to structures  Example: class DayOfYear { public: int month; int day; public : void output(); };

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-4 Declaring Objects  Declared same as all variables  Example: DayOfYear today, birthday;  Declares two objects of class type DayOfYear  Objects include:  Data (member data)  month, day  Operations (member functions)  output()

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-5 Class Member Access  Members accessed same as structures  Example: today.month today.day  And to access member function: today.output();  Invokes member function

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-6 Class Member Functions  Must define or "implement" class member functions  Like other function definitions  Must specify class: return_type class_name::function_name(parameters) Example: void DayOfYear::output() {…}  :: is scope resolution operator  Instructs compiler "what class" member is from  Item before :: called type qualifier

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-7 Dot and Scope Resolution Operator  Dot (. ) operator:  Specifies member of particular object  Scope resolution ( :: ) operator:  Specifies what class the function definition comes from

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-8 Class Member Functions Definition  Function used for all objects of the class  Will refer to "that object’s" data when invoked  Example: today.output();  Displays "today" object’s data

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-9 Topic 2: Constructors of Classes  Initialization of objects  Initialize some or all member variables  Other actions possible as well, but not typicall  A special kind of member function  Automatically called when object declared

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor Definitions  Constructors defined like any member function  Except: 1. Must have same name as class 2. Cannot return a value; not even void!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor Definition Example  Class definition with constructor: class DayOfYear { public: int month; int day; public: DayOfYear(int m, int d); //Constructor initializes month & day void output(); };

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor Code  Constructor definition is like all other member functions: DayOfYear::DayOfYear(int m, int d) { month = m; day = d; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor Notes  Notice name of constructor: DayOfYear  Same name as class itself!  Constructor declaration has no return-type  Not even void!  Constructor in public section

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Calling Constructors  Declare objects: DayOfYear date1(7, 4); DayOfYear date2(5, 5);  Objects are created here  Constructor is called  Values in parenthes passed as arguments to constructor

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor Equivalency  Consider:  DayOfYear date1, date2 date1.DayOfYear(7, 4);// ILLEGAL! date2.DayOfYear(5, 5);// ILLEGAL!  Seemingly OK…  CANNOT call constructors like other member functions!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Overloaded Constructors  Can overload constructors just like other functions  Recall: a signature consists of:  Name of function  Parameter list  Provide constructors for all possible argument-lists

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor with No Arguments  Can be confusing  Standard functions with no arguments:  Called with syntax: callMyFunction();  Including empty parentheses  Object declarations with no "initializers":  DayOfYear date1; // This way!  DayOfYear date(); // Not this way!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Topic 3: Principles of OOP  Information Hiding  Details of how operations work not known to "user" of class  Data Abstraction  Details of how data is handled within class not known to user  Encapsulation  Bring together data and operations, but keep "details" hidden

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Public and Private Members  In C++, Principles of OOP are implemented through the qualifier public and private keywords  Private data in class allows manipulation only via member functions  Public items (usually member functions) can be accessed by other functions.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Public and Private Example  This class definition satisfies principles of OOP class DayOfYear { private: int month; int day; public: void input(); void output(); };  Data now private  Objects have no direct access to private data

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Public and Private Example (continued) DayOfYear d; …….. d.month =1; // No longer allowed d.input(); // This is allowed  How data members are handled is hidden

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Topic 4: Class Type Member Variables  Class member variables can be any type  Including objects of other classes!