Pointer to an Object Can define a pointer to an object:

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
1 Introduction to Classes. C++: Classes & Objects - 12 Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions.
Starting Out with C++: Early Objects 5th Edition
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
C++ Review (3) Structs, Classes, Data Abstraction.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
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.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Review: Two Programming Paradigms
classes and objects review
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Chapter 14: More About Classes.
Introduction to Classes
Introduction to Classes and Objects
Review: C++ class represents an ADT
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Pointer to an Object Can define a pointer to an object: Rectangle *rPtr = nullptr; Can access public members via pointer: rPtr = &otherRectangle; rPtr->setLength(12.5); cout << rPtr->getLength() << endl;

Dynamically Allocating an Object We can also use a pointer to dynamically allocate an object.

Why Have Private Members? 13.4 Why Have Private Members?

Why Have Private Members? Making data members private provides data protection Data can be accessed only through public functions Public functions define the class’s public interface

Code outside the class must use the class's public member functions to interact with the object.

Separating Specification from Implementation 13.5 Separating Specification from Implementation

Separating Specification from Implementation Place class declaration in a header file that serves as the class specification file. Name the file ClassName.h, for example, Rectangle.h Place member function definitions in ClassName.cpp, for example, Rectangle.cpp File should #include the class specification file Programs that use the class must #include the class specification file, and be compiled and linked with the member function definitions

Inline Member Functions 13.6 Inline Member Functions

Inline Member Functions Member functions can be defined inline: in class declaration after the class declaration Inline appropriate for short function bodies: int getWidth() const { return width; }

Rectangle Class with Inline Member Functions 1 // Specification file for the Rectangle class 2 // This version uses some inline member functions. 3 #ifndef RECTANGLE_H 4 #define RECTANGLE_H 5 6 class Rectangle 7 { 8 private: 9 double width; 10 double length; 11 public: 12 void setWidth(double); 13 void setLength(double); 14 15 double getWidth() const 16 { return width; } 17 18 double getLength() const 19 { return length; } 20 21 double getArea() const 22 { return width * length; } 23 }; 24 #endif

Tradeoffs – Inline vs. Regular Member Functions Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Code for an inline function is copied into program in place of call – larger executable program, but no function call overhead, hence faster execution

13.7 Constructors

Constructors Member function that is automatically called when an object is created Purpose is to construct an object Constructor function name is class name Has no return type

Continues...

Contents of Rectangle.ccp Version3 (continued)

Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing. A simple instantiation of a class (with no arguments) calls the default constructor: Rectangle r;

Passing Arguments to Constructors 13.8 Passing Arguments to Constructors

Passing Arguments to Constructors To create a constructor that takes arguments: indicate parameters in prototype: Rectangle(double, double); Use parameters in the definition: Rectangle::Rectangle(double w, double len) { width = w; length = len; }

Passing Arguments to Constructors You can pass arguments to the constructor when you create an object: Rectangle r(10, 5);

More About Default Constructors If all of a constructor's parameters have default arguments, then it is a default constructor. For example: Rectangle(double = 0, double = 0); Creating an object and passing no arguments will cause this constructor to execute: Rectangle r;

Classes with No Default Constructor When all of a class's constructors require arguments, then the class has NO default constructor. When this is the case, you must pass the required arguments to the constructor when creating an object.

13.9 Destructors

Destructors Member function automatically called when an object is destroyed Destructor name is ~classname, e.g., ~Rectangle Has no return type; takes no arguments Only one destructor per class, i.e., it cannot be overloaded If constructor allocates dynamic memory, destructor should release it

Contents of InventoryItem.h Version1 (continued)

Constructors, Destructors, and Dynamically Allocated Objects When an object is dynamically allocated with the new operator, its constructor executes: Rectangle *r = new Rectangle(10, 20); When the object is destroyed, its destructor executes: delete r;

Overloading Constructors 13.10 Overloading Constructors

Overloading Constructors A class can have more than one constructor Overloaded constructors in a class must have different parameter lists: Rectangle(); Rectangle(double); Rectangle(double, double);

Continues...

Only One Default Constructor and One Destructor Do not provide more than one default constructor for a class: one that takes no arguments and one that has default arguments for all parameters Square(); Square(int = 0); // will not compile Since a destructor takes no arguments, there can only be one destructor for a class

Member Function Overloading Non-constructor member functions can also be overloaded: void setCost(double); void setCost(char *); Must have unique parameter lists as for constructors

Using Private Member Functions 3.11 Using Private Member Functions

Using Private Member Functions A private member function can only be called by another member function It is used for internal processing by the class, not for use outside of the class See the createDescription function in ContactInfo.h (Version 2)