A First C++ Class – a Circle

Slides:



Advertisements
Similar presentations
Class and Objects.
Advertisements

Chapter 14: Overloading and Templates
Classes and Objects Systems Programming.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
C++ data types. Structs vs. Classes C++ Classes.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand the usage of classes and functions.  Understand static.
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.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and 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?
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
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.
Operator Overloading.
Chapter 12 Classes and Abstraction
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Classes C++ representation of an object
Chapter 13: 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?
Programming with ANSI C ++
Chapter 9 More on Objects and Classes
Object-Oriented Design (OOD) and C++
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
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
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Chapter 15: Overloading and Templates
More about OOP and ADTs Classes
CS212: Object Oriented Analysis and Design
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Introduction to Classes
More about OOP and ADTs Classes
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
CIS 199 Final Review.
Classes C++ representation of an object
CPS120: Introduction to Computer Science
C++ data types.
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Object Oriented Programming
Presentation transcript:

A First C++ Class – a Circle First examine the code that uses a struct to define a simple Circle. The C++ class syntax is built on the syntax of C struct. Important concepts: A class is an encapsulation of state (data members) and behavior (methods). Encapsulation supports information hiding, abstraction, modularity, and enhances security.

Class Declaration Syntax class ClassName { public: Declarations of public members (methods) private: Declarations of private members (data) };

Designing a Class Data members normally placed in private: section of a class Function members usually in public: section Typically public: section followed by private: although not required by compiler

Class Libraries Class declarations placed in header file Given .h extension Contains data items and prototypes Implementation file Same prefix name as header file Given .cpp extension Programs which use this class library called client programs

Example of User-Defined Circle Class See Circle.h and Circle.cpp Circle.h Constructor prototype – use name of class with no return type (builds the object) Note use of initializer list in default constructor Initializer list not used in parameterized constructor because we CHECK the values passed by the client to ensure precondition is satisfied

Constructors Note constructor definitions in Circle.cpp Syntax for default constructor ClassName::ClassName (parameter_list) : member_initializer_list { // body of constructor definition } Syntax for parameterized constructor ClassName::ClassName (parameter_list) { // body of constructor definition }

Constructors Results of default constructor? Circle c; Results of parameterized constructor? Circle bigC(0, 0, 500);

Overloading Functions Note existence of multiple functions with the same name 1. Circle(); 2.Circle(float x, float y, float r); Known as overloading Compiler compares numbers and types of arguments of overloaded functions Checks the "signature" of the functions 1. – default constructor 2. – parameterized constructor

Default Arguments Possible to specify default values for constructor arguments – allows any reasonable construction of an object Circle (float x = 2.0, float y = 2.0, float r = 10); Consider? Circle c1, c2(5), c3(5,30), c4(5,30,120); This allows the class designer to supply one constructor that gives the client reasonable options for constructing objects Syntax for parameterized constructor with default values ClassName::ClassName (type1 parm1 = val1, …, typen parmn = valn) { // body of constructor definition }

Copy Operations During initialization Circle c = c1; During Assignment c = c1; When passing by value or returning a value from a function/method We can do this because all the class data members are primitive types that the compiler knows how to copy. When the class data members are aggregate types a copy constructor and assignment operator must be added to the class definition.

Other Class Operations Accessors and Mutators See “get" and ”set" functions Overloading operators Same symbol can be used more than one way Note declaration for I/O operators<< and >> Note definition of overloaded I/O operators friend ostream& operator<<(ostream & out, const Circle &c); friend istream& operator>>(istream & in, Circle &c);

Friend Functions Possible to specify operator<<() as a "friend" function Thus given "permission" to access private data elements Declaration in .h file (a friend function is not a class member – it may be a free function or it may be a method belonging to another class. Operator<< is a method of the ostream class.)

Friend Functions Definition in .cpp file (remove friend reserved word here) ostream& operator<<(ostream & out, const Circle &c) { out<< " Circle radius = " << c.radius << endl; out << " Circle is centered at " << c.xCenter << "," << c. yCenter << endl; return out; } Note - a friend function is not a member function not qualified with class name and :: receives class object on which it operates as a parameter

Other Operations Relational Operators Circle object compares itself with another Determines if it is less than the other bool Circle::operator<(const Circle &circle1) const { return radius < circle1.radius; }

Redundant Declarations Note use of #include ”SimpleCircle.h" in SimpleCircle.cpp Client main program Causes "redeclaration" errors at compile time Solution is to use conditional compilation Use #ifndef and #define and #endif compiler directives

Pointers to Class Objects Possible to declare pointers to class objects Circle * cPtr = &c; Access with cPtr->getX (); or (*cPtr).getX();

The this Pointer Every class has a keyword, this a pointer whose value is the address of the object Value of *this would be the object itself void Circle::setX(float x) { this->xCenter = x; }