Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.

Slides:



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

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Using Structures and Classes COSC 1557 C++ Programming Lecture 4.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
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
 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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
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 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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?
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
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.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Introduction to Classes
More about OOP and ADTs Classes
Introduction to Classes
More about OOP and ADTs Classes
Object-Oriented Programming Using C++ Second Edition
Chapter 9 Objects and Classes
Introduction to Classes and Objects
Classes and Objects.
Classes C++ representation of an object
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
SPL – PS3 C++ Classes.
Presentation transcript:

Defining Data Types in C++ Part 2: classes

Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe object behaviors) Class: C++ mechanism used to represent an object Class definition includes: –member functions –member variables

Information Hiding Principle says we should only know what we need to know (to prevent getting bogged down in irrelevant detail) A class that uses information hiding in its design is an Abstract Data Type (ADT –member variables hidden –access to hidden members available only through member functions

Class declaration promotes information hiding class Name { public: //member functions //declared here private: //member variables //declared here }; “Name” is a valid identifier public section includes class parts accessible to outside world private section includes hidden parts Note semicolon!

Example object: the Nesper sign Member variables: –message –time & temperature Member functions: –display message –display time & temp –change message –change time & temp

class Nesper { public: void display_message ( ) const; void display_tnt ( ) const; void change_message (char[] text); void change_tnt (int temp, clock current); private: char message[25]; int temperature; clock time; };

Class definition: public section class Nesper { public: void display_message ( ) const; void display_tnt ( ) const;... Public section contains member function prototypes Keyword “const” indicates that the functions declared here do not change the object

Class definition: public section... void change_message (char[] text); void change_tnt (int temp, clock current); “Change” functions are examples of modifiers - - member functions that alter the calling object change_tnt includes a parameter (current) that is an example of an ADT variable

Class definition: private section … private: char message[25]; int temperature; clock time; }; Member variables declared here Can be variables of built-in types, arrays or pointer, or instances of objects defined elsewhere

Where do they go? Class declaration: header file (xxxx.h) Member function definitions: definition file –xxxx.cpp (same name as header -- different extension) –should not include a main( ) function Class instances: user program

Implementing member functions #include void Nesper::change_message (char[] text) { assert (strlen(text)<25); message = text; } Preprocessor directives for all needed library routines Class name and scope operator (::) indicate this is a member function

Notes on member functions Every instance of a class (object) has its own copies of all class members, including member functions A member function is called by the object that owns it (can have several objects of same class in a program) Member functions may call other member functions

Using a class in a program #include “nesper.h” int main( ) { Nesper sign1, sign2;... Preprocessor directive indicates class definition should be included Declaring Nesper variable instantiates the class sign1 and sign2 are objects of type Nesper

Using a class in a program … sign1.change_message (“This is my sign”); sign1.display_message( );... Member function is activated by calling object Syntax: calling_object. function_name (argument(s));

Constructors Special member function Automatically called when an object is instantiated Unique characteristics: –Constructor name is same as class name –Constructor has no return value (not even void) Can have multiple constructors for a class -- example of function overloading

Why define constructors? Can define a class without one; in this case, compiler uses automatic default constructor –memory allocated for member variables –constructors for individual members are called, if they exist With defined constructor, can do more -- including initializing member variables

Constructor Prototypes class Nesper { public: Nesper (string msg); // initializes message Nesper (int temp); // initializes temperature Nesper (clock tm); // initializes time Nesper ( ); // default constructor

Notes on Function Overloading Can have as many functions with the same name in a class as you wish Compiler differentiates between the functions by their parameter lists A constructor that requires no arguments is the default constructor -- will be called under most circumstances

Calling a constructor Constructor is called when an object is declared: Nesper stopsign (“Stop! Stop, I say!”); // calls first ctor in class Nesper midnight (12:00); // calls ctor with clock parameter Nesper sign; // calls default constructor

Constructor implementation Nesper::Nesper (string msg) { message = msg; }

Another example: default ctor Nesper::Nesper( ) { message = “Your message here”; temperature = 32; time = 12:00; }

Default arguments Functions can be declared with default values listed for their parameters Provides flexibility for constructors: –can be called with or without arguments –can be called with some, but not all arguments specified –if no arguments are specified, default values are used

Nesper constructor with default arguments class Nesper { public: Nesper (string msg = “Your message here”, int temp = 32, clock tm = 12:00);... Default arguments are specified in the function prototype, not the implementation When function is called, can omit some or all arguments -- may be omitted starting from right

Examples of function calls Nesper mysign (“Taurus”, 65, 4:30); // all defaults replaced by actual arguments Nesper yoursign (“Pisces”); // msg replaced by argument; use defaults for // remaining values Nesper sign; // most common -- defaults used for all values

Implementation of function with default arguments Nesper::Nesper (string msg, int temp, clock tm) { message = msg; temperature = temp; time = tm; } Implementation is identical to version that called for the same parameters but didn’t use default arguments Default arguments appear only in prototype

One more variation: inline functions Inline functions are defined (implemented) within the class definition –Saves a little execution time (no function call, no return) –Can be inefficient in terms of memory (can end up with many copies of same compiled code Best for extremely simple, “one-liner” functions

Inline constructor example class Nesper { public: Nesper( ) { message = “Your message”; temperature = 32; time = 12:00; }... Inline functions aren’t usually used for constructors, unless the object is very small This is still just one function declaration within class definition

Value semantics Operations that determine how values are copied from one object to another object of the same class type Assignment operator Copy constructor: constructor that instantiates an object which is an exact copy of its argument

Automatic assignment example Nesper sign1(“Hi”, 32, 1:00), sign2; // sign2 has default values sign2 = sign1; // sign2 now has same // values as sign1 Can use automatic assignment when the object doesn’t use dynamic memory Later we’ll see how to define the assignment operation for classes that require it

Automatic copy constructor Nesper sign1; … Nesper sign2(sign1);... Nesper sign3 = sign1; First object uses default constructor Both second and third objects use copy constructor, even though third example looks like assignment Like automatic assignment, must be explicitly defined for some classes

Assignment vs. copy constructor Assignment copies information from one existing object into another existing object Copy constructor declares and initializes a new object, which is a copy of an existing object

One more look at Nesper.h #ifndef NESPER_H// macro guard -- use to ensure that class declaration #define NESPER_H// only appears once in a program -- safeguard // to prevent linking errors in large programs class Nesper { public: Nesper (string msg=“Your message”, int temp=32, clock tm=12:00); void display_message( ) const; void display_tnt( ) const; void change_message (string); void change_tnt (int temp, clock tm); private: string message; int temperature; clock time; }; #endif