CSC 143 Introduction to C++ Classes and User-Defined Data Types

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Introduction to Programming Lecture 31. Operator Overloading.
Class and Objects.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Monday, 10/14/02, Slide #1 CS 106 Intro to CS 1 Monday, 10/14/02  QUESTIONS??  Today: More on classes!  Reading: None new  Exercises: Implement and.
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 15: Operator Overloading
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
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.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 CSC241: Object Oriented Programming Lecture No 03.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Operator Overloading.
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading Introduction
Chapter 13: Overloading and Templates
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 9: Operator Overloading
A First C++ Class – a Circle
Introduction to Classes
More about OOP and ADTs Classes
Operator Overloading BCA Sem III K.I.R.A.S.
Classes with Dynamically Allocated Data
Introduction to Classes
Operator Overloading
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
More about OOP and ADTs Classes
Classes with Member Functions
Chapter 9 Objects and Classes
Overview of C++ Overloading
Dr. Bhargavi Dept of CS CHRIST
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Lists CMSC 202, Version 4/02.
Object Oriented Programming
Presentation transcript:

CSC 143 Introduction to C++ Classes and User-Defined Data Types [pp 125-141 (skim the example) chapter 3 and appendix A] Idea: Introduce the least amount of information needed to construct a working program containing a class specification and implementation, and do it in time for the first assignment to include defining a new class (type). This is a tiny new arithmetic class with a hidden representation part and operations that can access the representation. What’s not here: objects, member functions. Problem: most books include member functions when they introduce classes, so students wind up writing member functions without really understanding what they’re doing.

Complex Numbers in C To use Complex numbers in C, we’d might have... typedef struct { /* rectangular rep of complex */ double re, im; } Complex; ... Complex x,y,z; x = makeComplex(0,1); y = makeComplex(17,42); z = Cadd(x,y); Very clumsy compared to built-in numeric types. C++ solution: classes. A mechanism to define new types, their representations, and operations on them.

C++ Classes Class Declaration (in a .h file) class ClassName { public: information available to clients private: private information }; Private information is only accessible in functions that are given permission in the class declaration Limits region of program with access to implementation details Details may be changed without affecting clients as long as public interface remains the same (but may need to recompile)

Complex Numbers in C++ // complex.h:simple complex number class class Complex { public: // constructors: Complex( ); // = 0+0i Complex(double a, double b); // = a+bi Complex(double r); // = r+0i // component access friend double real(Complex); friend double imag(Complex);

Complex Numbers (cont) // operations friend Complex operator+(Complex,Complex); friend Complex operator-(Complex,Complex); friend Complex operator-(Complex); friend Complex operator*(Complex,Complex); friend bool operator==(Complex,Complex); friend bool operator!=(Complex,Complex); ... private: double re, im; // representation: // rectangular real and // imaginary parts };

Using Complex The Complex class is used like this: #include “complex.h” ... Complex a = 2.3; // i.e., 2.3+0i Complex b = 2*a; Complex c = Complex(0,1);// 0+1i Complex f; // 0+0i f = (2*a+b)*Complex(3,-1); cout << “the answer is “ << f << endl; The new definitions of operators +, -, *, ==, !=, and << overload (not replace) the existing definitions. Implicit conversions happen if appropriate constructors are available: 2*a means Complex((double)2)*a.

Friend Functions Ordinary functions; definitions placed in .cpp implementation files as usual But: have access to private class info because named as friends in the class declaration // complex.cpp -- Complex class impl. #include “complex.h” // = real part of c double real(Complex c) { return c.re;} // = sum of c and d Complex operator+(Complex c,Complex d) { return Complex(c.re+d.re,c.im+d.im); }

Constructors Special functions executed every time a variable with a class type is created Cannot be executed in any other situation Name is same as name of the class No explicit return type A place to assign sensible initial values to fields of new variable Direct access to fields of new variable via field names Also used to create anonymous values of the class type May have several constructors in a class - correct one picked based on argument types You can’t really have a new class without constructors, so we have to take a few things (:: operator) on faith for a while.

Constructor declaration Located in class declaration in header file //complex.h--simple complex number class class Complex { public: // constructors: Complex( ); // = 0+0i Complex(double a, double b);// = a+bi Complex(double r);// = r+0i ... };

Constructor Implementation In .cpp implementation file as usual, but… Unusual syntax, because not ordinary function. // in complex.cpp… // constructors: Complex::Complex( ) {re=0; im=0;} Complex::Complex(double r,double i){ re = r; im = i; } Complex::Complex(double r) { re = r; im = 0; Details about “::” notation to be revealed later. The :: member function notation can’t be avoided here. But treat it as the way a constructor is named without going into details of what it means.

Stream Output for Complex Operator << can also be overloaded for new types Arcane syntax - imitate blindly for now; to be explained later Declaration in file Complex.h: // write Complex number as <real,imag> friend ostream& operator<<(ostream &s,Complex z); More magic for now. But it’s real nice to be able to write a new type using cout<<value.

Stream Output for Complex Implementation in complex.cpp // write Complex number as <real,imag> ostream& operator<<(ostream &s,Complex z){ return s << ‘<‘ << z.re << ‘,’ << z.im << ‘>’; } operator<< has access to private parts of z because it is a friend of class Complex Must have #include <iostream.h> in.cpp file for this to work.