CMSC 341 C++ and OOP.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

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.
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,
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
ITEC 320 C++ Examples.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
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++
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
11 Introduction to Object Oriented Programming (Continued) Cats.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
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.
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.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
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.
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.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 Compiler directive: #define, usage 1 #include using namespace std; #define TAX //double TAX=0.08; #define LAST_NAME "Li" #define FIRST_NAME "Dennis"
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
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.
Submission Example May 14, 2018
#define #include<iostream> using namespace std; #define GO
Chapter 1 C++ Basics Review
Command Line Arguments
Introduction to C++ and Algorithm Analysis
Pointers and Pointer-Based Strings
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Pointers Psst… over there.
Chapter 2 Objects and Classes
group work #hifiTeam
Pointers Psst… over there.
Learning Objectives What else in C++ Bitwise operator
Random Number Generation
Creation and Use of Namespaces
Screen output // Definition and use of variables
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Classes.
Namespaces How Shall I Name Thee?.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Pointers and Pointer-Based Strings
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CMSC 341 C++ and OOP.
Pointers and dynamic objects
Pointers & Functions.
Programming Strings.
CMSC 341 C++ and OOP.
Templates CMSC 202, Version 4/02.
CMSC 341 C++ and OOP.
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 m_storedValue; }; #endif

IntCell.C (part 1) #include "IntCell.h” using namespace std;   // Construct the IntCell with initialValue IntCell::IntCell( int initialValue ) : m_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 m_storedValue; }   // Store x (mutator) void IntCell::Write( int x ) { m_storedValue = x; }

TestIntCell.C   #include <iostream> #include "IntCell.h"  using namespace std; 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 m_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 ) :m_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) if (this != &rhs) Write( rhs.Read( ) ); return *this;

MemCell.C (part 2) // destructor template <class Object> MemCell<Object>::~MemCell( ) { // no code }   // Return the stored value. template <class Object> const Object & MemCell<Object>::Read( ) const { return m_storedValue; // Store x. void MemCell<Object>::Write( const Object & x ) { m_storedValue = x;

TestMemCell.C #include <iostream> #include <string> #include "MemCell.h" using namespace std;  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 ( 0 ); }