CMSC 341 C++ and OOP.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
1 CMSC 202 More C++ Classes. 2 Announcements Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Dynamic Memory for Class #include #ifndef STRCLASS_H_ #define STRCLASS_H_ class strclass { private: char *str; int len; static int num_strings; public:
GETTING STARTED WITH C++ ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and Communication.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Classes: Part 2 .
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
Intoduction to C++ CENG 213 Data Structures1. 2 Programming in C++ C++ –Improves on many of C's features –Has object-oriented capabilities Increases software.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Introduction to C++ CENG 213 Data Structures 1.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
1 Ugly Realities The Dark Side of C++ Chapter 12.
Intoduction to C++ CENG 213 Data Structures1. 2 Programming in C++ C++ –Improves on many of C's features –Has object-oriented capabilities Increases software.
1 Chapter (1) – Introduction Objectives Consistency and importance of the performance of a program for inputs of Different sizes. Basic mathematical background.
CENG 707 Data Structures and Algorithms
Chapter 2 Objects and Classes
Pointer to an Object Can define a pointer to an object:
METU Computer Engineering Dept.
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?
Intoduction to C++ CENG 213 Data Structures.
Chapter 1 C++ Basics Review
Introduction to Data Structures and Object-Oriented Programming with C++ 1.
Introduction to C++ and Algorithm Analysis
Object-Oriented Programming (OOP) Lecture No. 17
14.4 Copy Constructors.
Introduction to C++ CENG 213 Data Structures.
Chapter 2 Objects and Classes
جامعة البحر الاحمر كلية العلوم التطبيقية قسم الفيزياء التطبيقية الفصل الداسي الثاني IIالمقرر: حاسوب د. خالد عثمان العالم.
Introduction to Classes
CMSC 202 Templates.
جامعة البحر الاحمر كلية العلوم التطبيقية قسمي الحاسوب وتقنية المعلومات الفصل الداسي الثاني المقرر: اساليب برمجة 1 محاضرة رقم 1 د. خالد عثمان العالم.
CENG 213 Data Structures and Algorithms
Classes.
Passing Arguments and The Big 5
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CMSC 341 C++ and OOP.
Review: C++ class represents an ADT
CMSC 341 C++ and OOP.
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
CMSC 341 C++ and OOP.
Templates CMSC 202, Version 4/02.
CMSC 341 C++ and OOP.
Presentation transcript:

CMSC 341 C++ and OOP

Intcell.H #ifndef _IntCell_H_ #define _IntCell_H_ // A class for simulating an integer memory cell. class IntCell { public: explicit IntCell( int initialValue = 0 ); Intcell(const Intcell & ic) ~Intcell(); const Intcell & operator =(const Intcell & rhs);  int read( ) const; void write( int x );   private: int _storedValue; }; #endif

IntCell.C (part 1) #include "IntCell.h"   // Construct the IntCell with initialValue IntCell::IntCell( int initialValue ) : storedValue(initialValue) { // no code } //copy constructor Intcell::Intcell(const Intcell & ic) write (ic.read()); // destructor Intcell::~Intcell() {

IntCell.C (part 2) //assignment operator const Intcell & Intcell::operator=(const IntCell & rhs) { if (this != &rhs) write(rhs.read()); return *this; } // Return the stored value (accessor) int IntCell::read( ) const { return _storedValue; }   // Store x (mutator) void IntCell::write( int x ) { _storedValue = x; }

TestIntCell.C   #include <iostream.h> #include "IntCell.h" int main( ) { IntCell m; // Or, IntCell m( 0 ); but not IntCell m( ); Intcell n; n = m; m.write( 5 ); cout << "Cell m contents: " << m.read( ) << endl; cout << "Cell n contents: " << n.read( ) << endl; return 0; }

MemCell.H #ifndef _MEMCELL_H #define _MEMCELL_H  // A class for simulating a memory cell. template <class Object> class MemCell { public: explicit MemCell(const Object &initialValue =Object( ) ); MemCell(const MemCell & mc); const MemCell & operator=(const MemCell & rhs); ~MemCell(); const Object & read( ) const; void write( const Object & x ); private: Object _storedValue; };   //Because separate compilation doesn't always work #include "MemCell.C" #endif

MemCell.C(part 1) #include "MemCell.h“ // Construct the MemCell with initialValue template <class Object> MemCell<Object>::MemCell( const Object & initialValue ) : _storedValue( initialValue ) { // no code }   //copy constructor MemCell<Object>::MemCell(const MemCell<Object> & mc) write(mc.read( )); //assignment operator const MemCell<Object> & MemCell<Object>::operator=(const MemCell<Object> & rhs) write(rhs.read( ));

MemCell.C (part 2) // destructor template <class Object> MemCell<Objec!>::~MemCell( ) { // no code }   // Return the stored value. template <class Object> const Object & MemCell<Objec!>::read( ) const { return _storedValue; // Store x. void MemCell<Objec!>::write( const Object & x ) { storedValue = x;

TestMemCell.C #include <iostream.h> #include "MemCell.h" #include "string.h" #include <stdlib.h>   int main( ) { MemCell<int> m1; MemCell<string> m2( "hello" ); m1.write( 37 ); string str = m2.read(); str += " world"; m2.write(str); cout << m1.read( ) << endl << m2.read( ) << endl; return (EXIT_SUCCESS); }