Set Implementation Version 1
|
|
- Christopher Floyd
- 5 years ago
- Views:
Transcription
1 Introduction to System Programming Set Implementation Version 1 Masha Nikolski, CS Department, Technion
2 1 // Version // Header file for set class. 3 // In this implementation set is a container of a limited size holding elements 4 // with no repetitions. Elements can be added to the set till it is full. 5 6 // Those two lines and the last one prevent the set header from being 7 // included more than once. 8 #ifndef _SET_H_ 9 #define _SET_H_ // We have to include iostream because we want to implement printing 12 // and scanning operators for the set class. 13 #include <iostream> // We are using those two classes and not the whole std namespace. 16 using std::ostream; 17 using std::istream; class Set { 20 // This part is accessible only by the Set class. 21 private: 22 // the size of the set 23 int dim; 24 // current number of elements in the set 25 int numelems; 26 // buffer holding set's data 27 double* buffer; // When a class has dynamically allocated memory in his fields, its usually 30 // useful to implement those two private functions. They later will be used 31 // in constructors, destructor, operator=. 32 void copy(const Set& s); 33 void clear(); // Returns the index of the element d in the buffer or -1 if not found in 36 // the set. 37 int find(const double& d) const; // This part is accessible by everyone. 40 public: // Constructor with a default parameter for set's size. 43 // Its accentual to define one if we want to allocate an array of vectors. 44 // For example: Set array[5]; 45 Set(int s = 10); // Constructor initializing the set from an array of elements. 48 Set(const double* arr, int n); // Copy constructor. 51 // It is accentual to define one since there are dynamically allocated fields 52 // in this class. 53 // Will be used for: 54 // 1. Creating set from another set: Set s1; Set s2 = s1; 55 // 2. For passing set by value to a function: func(s); 56 // 3. For returning a set by value from a function: 57 // Set f(){ return Set();} 58 Set(const Set& s); // Conversion constructors 61 // We want to be able to do: 62 // Set s; s += 7.4; 63 // Here first a conversion constructor will be called converting 7.4 to a set 64 // with one element, and then operator += will be called for s adding the 65 // element 7.4 to the set. 66 Set(const double& d); // Destructor. 69 // It is accentual to define one since there are dynamically allocated fields 70 // in this class and we need to free this memory. 71 ~Set(); // operator=
3 74 // It is accentual to define one since there are dynamically allocated fields 75 // in this class. The default operator= supplied by the compiler will simply 76 // copy the set field by field, which will be wrong in case of pointers. 77 Set& operator=(const Set& s); // Checks if an element is inside 80 bool is_in(const double& d) const; // Inserts an element 83 bool insert(const double& d); // Erases an element 86 bool erase(const double& d); // Checks if the set is full 89 bool full() const; // Checks if the set is empty 92 bool empty() const; // Erases all the elements from the set 95 void clean(); // Returns the current size of a set 98 int size() const; // Those operators return a reference to allow concatenation of 101 // operations on sets (s1 += s2) -= s3; 102 Set& operator+=(const Set& s); 103 Set& operator-=(const Set& s); // This function will be used in order to implement the comparison operators 106 // without making them friends. 107 bool compare(const Set& s) const; // This function will be used in order to implement operator<< 110 // without making it a friend. 111 void print(ostream& ost) const; // This function will be used in order to implement operator>> 114 // without making it a friend. 115 void scan(istream& ist); }; // All the following operators are global functions and there is no need 120 // to make them friends // Comparison operators 123 bool operator==(const Set& s1, const Set& s2); 124 bool operator!=(const Set& s1, const Set& s2); // I/O operators 127 ostream& operator<<(ostream& ost, const Set& s); 128 istream& operator>>(istream& ist, Set& s); // Union/Substruction 131 Set operator+(const Set& s1, const Set& s2); 132 Set operator-(const Set& s1, const Set& s2); #endif
4 1 // Version // Implementation of the set class. 3 4 // In this file NULL is defined, in C++ it is the constant 0. 5 #include <cstdlib> 6 7 #include "set.h" 8 9 void Set::copy(const Set& s) 10 { 11 dim = s.dim; 12 numelems = s.numelems; 13 buffer = new double[dim]; 14 for (int i = 0; i < numelems; i++) 15 buffer[i] = s.buffer[i]; 16 } void Set::clear() 19 { 20 if (buffer) { 21 delete[] buffer; 22 // make sure there is no garbage there, so we can always check 23 // the value of the fields later 24 buffer = NULL; 25 dim = 0; 26 numelems = 0; 27 } 28 } // Use initialization list to initialize all the fields. 31 Set::Set(int s) 32 : dim(s) 33, numelems(0) 34, buffer(new double[s]) 35 { 36 } // This is an example why the copy function is so useful. 39 Set::Set(const Set& s) 40 { 41 copy(s); 42 } Set::Set(const double& d) 45 { 46 dim = 1; 47 numelems = 1; 48 // Allocate a one double and call its constructor. 49 buffer = new double(d); 50 } Set::Set(const double* arr, int n) 53 { 54 dim = n; 55 numelems = 0; 56 buffer = new double[n]; for (int i = 0; i < n; i++) 59 insert(arr[i]); // numelems will be updated in this function 60 } // All the destructor needs to do is to call the clear function. 63 Set::~Set() 64 { 65 clear(); 66 } Set& Set::operator=(const Set& s) 69 { 70 // Check if we are not doing s = s. 71 // If we do, we can't erase ourselves, otherwise it will be a disaster! 72 if (this == &s) 73 return *this;
5 74 clear(); 75 copy(s); 76 return *this; 77 } Set& Set::operator+=(const Set& s) 80 { 81 // No need to do anything in this case, since the set holds only unique 82 // values. 83 if (this == &s) 84 return *this; Set new_set(dim + s.dim); new_set.numelems = numelems; 89 for (int i = 0; i < numelems; i++) 90 new_set.buffer[i] = buffer[i]; for (int i = 0; i < s.numelems; i++) 93 new_set.insert(s.buffer[i]); // Note that operator= will also call to clear(), 96 // hence preventing a memory leak. 97 *this = new_set; 98 return *this; 99 } // You can always implement global operator+ with method operator+= 102 Set operator+(const Set& s1, const Set& s2) 103 { 104 Set s = s1; 105 s += s2; 106 return s; 107 } Set& Set::operator-=(const Set& s) 110 { 111 // In this case just remove all the elements. 112 if (this == &s) 113 clean(); for (int i = 0; i < s.numelems; i++) 116 erase(s.buffer[i]); 117 return *this; 118 } // You can always implement global operator- with method operator-= 121 Set operator-(const Set& s1, const Set& s2) 122 { 123 Set s = s1; 124 s -= s2; 125 return s; 126 } int Set::size() const 129 { 130 return numelems; 131 } bool Set::compare(const Set& s) const 134 { 135 // for s.compare(s) case 136 if (this == &s) 137 return true; if (numelems!= s.numelems) 140 return false; for (int i = 0; i < numelems; i++) { 143 if (!is_in(s.buffer[i])) 144 return false; 145 } 146 return true;
6 147 } // You can always implement global operator== with method compare 150 bool operator==(const Set& s1, const Set& s2) 151 { 152 return s1.compare(s2); 153 } bool operator!=(const Set& s1, const Set& s2) 156 { 157 return!(s1 == s2); 158 } void Set::print(ostream& ost) const 161 { 162 ost << "( "; 163 for (int i = 0; i < numelems; i++) 164 ost << buffer[i] << " "; 165 ost << ")"; 166 } ostream& operator<<(ostream& ost, const Set& v) 169 { 170 // Here we can just use the set method print and no need to make this 171 // function a friend. 172 v.print(ost); 173 return ost; 174 } void Set::scan(istream& ist) 177 { 178 double d; while (!ist.eof() &&!full()) { 181 ist >> d; 182 insert(d); 183 } 184 } istream& operator>>(istream& ist, Set& v) 187 { 188 // Here we can just use the set method scan and no need to make this 189 // function a friend. 190 v.scan(ist); 191 return ist; 192 } bool Set::is_in(const double& d) const 196 { 197 for (int i = 0; i < numelems; i++) 198 if (buffer[i] == d) 199 return true; 200 return false; 201 } bool Set::full() const 204 { 205 return (numelems == dim); 206 } bool Set::empty() const 209 { 210 return (numelems == 0); 211 } bool Set::insert(const double& d) 214 { 215 if (!is_in(d) &&!full()) { 216 buffer[numelems++] = d; 217 return true; 218 } 219 return false;
7 220 } bool Set::erase(const double& d) 223 { 224 if (is_in(d)) { 225 int i = find(d); 226 for (int j = i; j < numelems; j++) 227 buffer[j] = buffer[j + 1]; 228 numelems--; 229 return true; 230 } 231 return false; 232 } void Set::clean() 235 { 236 numelems = 0; 237 } int Set::find(const double& d) const 240 { 241 for (int i = 0; i < numelems; i++) 242 if (buffer[i] == d) 243 return i; 244 return -1; 245 }
8 1 #include "set.h" 2 3 // Include <iostream> and not <iostream.h> (see the tutorial for explanation). 4 #include <iostream> 5 6 // Using for printing/scanning purposes. 7 using std::cout; 8 using std::endl; 9 using std::cin; int main() 12 { 13 double arr[6] = {4.8, 9.5, 8.3, 9.5, 1.2, 4.8}; Set s1; // Constructor with default size = Set s2(3); // Constructor with size = Set s3(arr, 6); cout << "s1 = " << s1 << endl; 20 cout << "s2 = " << s2 << endl; 21 cout << "s3 = " << s3 << endl; s2.insert(4.7); 24 s2.insert(8.2); 25 s2.insert(8.2); 26 s2.insert(9.3); 27 s2.insert(7.1); 28 cout << "s2 = " << s2 << endl; s1 = s2; // s1.operator=(s2) 31 cout << "s1 = " << s1 << endl; s1 = s2 + s3; // operator+(s2, s3), then s1.operator=. 34 cout << "s1 = " << s1 << endl; s3 += 6.8; // Conversion constructor Set(6.8), then s3.operator+=. 37 cout << "s3 = " << s3 << endl; Set s4 = s3; // Copy constructor. 40 cout << "s4 = " << s4 << endl; return 0; 43 // Destructors. 44 }
Due Date: See Blackboard
Source File: ~/2315/45/lab45.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 4 Integer data is usually represented in a single word on a computer.
More informationDue Date: See Blackboard
Source File: ~/2315/11/lab11.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 1 The purpose of this assignment is to become more familiar with
More informationReview: C++ Basic Concepts. Dr. Yingwu Zhu
Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to
More informationChapter 18 - C++ Operator Overloading
Chapter 18 - C++ Operator Overloading Outline 18.1 Introduction 18.2 Fundamentals of Operator Overloading 18.3 Restrictions on Operator Overloading 18.4 Operator Functions as Class Members vs. as friend
More informationLab 2: ADT Design & Implementation
Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted
More informationDue Date: See Blackboard
Source File: ~/2305/lab06.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 2 Extend the IntegerSet class from Lab 04 to provide the following
More informationDue Date: See Blackboard
Source File: ~/2315/06/lab06.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 2 Extend the IntegerSet class from Lab 04 to provide the following
More informationOperator Overloading in C++ Systems Programming
Operator Overloading in C++ Systems Programming Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading
More informationEECE.3220: Data Structures Spring 2017
EECE.3220: Data Structures Spring 2017 Lecture 14: Key Questions February 24, 2017 1. Describe the characteristics of an ADT to store a list. 2. What data members would be necessary for a static array-based
More informationEINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science
EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science Written examination Homologation C++ and Computer Organization (2DMW00) Part I: C++ - on Tuesday, November 1st 2016, 9:00h-12:00h.
More informationObject oriented programming
Exercises 12 Version 1.0, 9 May, 2017 Table of Contents 1. Virtual destructor and example problems...................................... 1 1.1. Virtual destructor.......................................................
More informationCircle all of the following which would make sense as the function prototype.
Student ID: Lab Section: This test is closed book, closed notes. Points for each question are shown inside [ ] brackets at the beginning of each question. You should assume that, for all quoted program
More informationการทดลองท 8_2 Editor Buffer Array Implementation
การทดลองท 8_2 Editor Buffer Array Implementation * File: buffer.h * -------------- * This file defines the interface for the EditorBuffer class. #ifndef _buffer_h #define _buffer_h * Class: EditorBuffer
More informationstruct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };
struct Buffer { Buffer(int s) { buf = new char[s]; ~Buffer() { delete [] buf; char *buf; ; struct FBuffer : public Buffer { FBuffer(int s) : Buffer(s) { f = fopen("file", "w"); ~FBuffer() { fclose(f);
More informationDue Date: See Blackboard
Source File: ~/2315/04/lab04.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 3 In this assignment create an IntegerSet class that will provide
More informationCSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 2 Yusuf Pisan Compiled helloworld yet? Overview C++ fundamentals Assignment-1 CSS Linux Cluster - submitting assignment Call by Value,
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table
More informationStreams. Ali Malik
Streams Ali Malik malikali@stanford.edu Game Plan Recap Purpose of Streams Output Streams Input Streams Stringstream (maybe) Announcements Recap Recap - Hello, world! #include int main() { std::cout
More informationW8.2 Operator Overloading
1 W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction
More informationClasses in C++98 and C++11
Classes in C++98 and C++11 January 10, 2018 Brian A. Malloy Slide 1 of 38 1. When we refer to C++98, we are referring to C++98 and C++03, since they differ only slightly. C++98 contained 3 types of constructors,
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationPIC 10B Lecture 1 Winter 2014 Homework Assignment #3
PIC 10B Lecture 1 Winter 2014 Homework Assignment #3 Due Tuesday, February 4, 2014 by 5:00pm. Objectives: 1. To redefine the Big 3 : Copy constructor, Assignment operator, and Destructor for a class whose
More informationOperator Overloading
Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is
More informationPIC10B/1 Winter 2014 Exam I Study Guide
PIC10B/1 Winter 2014 Exam I Study Guide Suggested Study Order: 1. Lecture Notes (Lectures 1-8 inclusive) 2. Examples/Homework 3. Textbook The midterm will test 1. Your ability to read a program and understand
More informationIntroduction. W8.2 Operator Overloading
W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction
More informationA <Basic> C++ Course
A C++ Course 5 Constructors / destructors operator overloading Julien Deantoni adapted from Jean-Paul Rigault courses This Week A little reminder Constructor / destructor Operator overloading Programmation
More informationpointers & references
pointers & references 1-22-2013 Inline Functions References & Pointers Arrays & Vectors HW#1 posted due: today Quiz Thursday, 1/24 // point.h #ifndef POINT_H_ #define POINT_H_ #include using
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College November 9, 2016 Outline Outline 1 Chapter 9: C++ Classes Outline Chapter 9: C++ Classes 1 Chapter 9: C++ Classes Class Syntax
More informationA <Basic> C++ Course
A C++ Course 5 Constructors / destructors operator overloading Julien DeAntoni adapted from Jean-Paul Rigault courses 1 2 This Week A little reminder Constructor / destructor Operator overloading
More informationIntroduction to C++ Systems Programming
Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions
More informationIS 0020 Program Design and Software Tools
1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Multiple Inheritance July 26, 2004 22.9 Multiple Inheritance 2 Multiple inheritance Derived class has several base classes Powerful,
More informationLinked List using a Sentinel
Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include
More informationCS
CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and
More informationEL2310 Scientific Programming
(pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object
More informationG52CPP C++ Programming Lecture 14. Dr Jason Atkin
G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used
More informationClasses, Constructors, etc., in C++
Classes, Constructors, etc., in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute
More informationShahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods
Overloading Operators friend functions static data and methods Shahram Rahatlou Computing Methods in Physics http://www.roma1.infn.it/people/rahatlou/cmp/ Anno Accademico 2018/19 Today s Lecture Overloading
More informationObject oriented programming
Exercises 6 Version 1.0, 21 March, 2017 Table of Contents 1. Operators overloading....................................................... 1 1.1. Example 1..............................................................
More informationInheritance and Polymorphism
Inheritance and Polymorphism 1 Inheritance extending a clock to an alarm clock deriving a class 2 Polymorphism virtual functions and polymorphism abstract classes MCS 360 Lecture 8 Introduction to Data
More informationUse the dot operator to access a member of a specific object.
Lab 16 Class A class is a data type that can contain several parts, which are called members. There are two types of members, data member and functions. An object is an instance of a class, and each object
More informationJordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A
Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Name: ID#: Section #: Day & Time: Instructor: Answer all questions as indicated. Closed book/closed
More informationCSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms
CSC 330 Object Oriented Programming Operator Overloading Friend Functions & Forms 1 Restrictions on Operator Overloading Most of C++ s operators can be overloaded. Operators that can be overloaded + -
More informationOperator Overloading
Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is
More informationFinancial computing with C++
Financial Computing with C++, Lecture 4 - p1/19 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 4 - p2/19 Outline General properties
More informationAbstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles
Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can
More informationCMPT 135: Midterm Answer Key
CMPT 135, Spring 2017, Surrey Midterm Answer Key Page 1 of 6 CMPT 135: Midterm Answer Key Last name exactly as it appears on your student card First name exactly as it appears on your student card Student
More informationExercise 1.1 Hello world
Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example
More informationIntroduction to C++ Introduction to C++ 1
1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference
More informationCSE 333 Lecture 9 - intro to C++
CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia & Agenda Main topic: Intro to C++ But first: Some hints on HW2 Labs: The
More informationCh 2 ADTs and C++ Classes
Ch 2 ADTs and C++ Classes Object Oriented Programming & Design Constructing Objects Hiding the Implementation Objects as Arguments and Return Values Operator Overloading 1 Object-Oriented Programming &
More informationArizona s First University. ECE 373. Operation Overloading. The Revolution Operation Will Be Televised (and, these slides will be online later today)
Arizona s First University. ECE 373 Operation Overloading The Revolution Operation Will Be Televised (and, these slides will be online later today) Previously, on ECE373 Families of operators can be overloaded
More informationCS 247: Software Engineering Principles. ADT Design
CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data
More informationUEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors
UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors What you will learn from Lab 5 In this laboratory, you will learn how to use constructor and copy constructor to create an object
More informationChapter 12 - Templates
Chapter 12 - Templates O utline 12.1 Introd uction 12.2 Function Te m plate s 12.3 Ove rload ing Te m plate Functions 12.4 Class Te m p late s 12.5 Class Te m plate s and Non-type Param e te rs 12.6 Te
More informationPointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.
Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference
More informationUnderstanding main() function Input/Output Streams
Understanding main() function Input/Output Streams Structure of a program // my first program in C++ #include int main () { cout
More informationComp151. Generic Programming: Container Classes
Comp151 Generic Programming: Container Classes Container Classes Container classes are a typical use for class templates, since we need container classes for objects of many different types, and the types
More information1 Short Answer (5 Points Each)
1 Short Answer ( Points Each) 1. Find and correct the errors in the following segment of code. int x, *ptr = nullptr; *ptr = &x; int x, *ptr = nullptr; ptr = &x; 2. Find and correct the errors in the following
More informationLecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology
Lecture 8: Object-Oriented Programming (OOP) 1 Introduction to C++ 2 Overview Additional features compared to C: Object-oriented programming (OOP) Generic programming (template) Many other small changes
More informationEX3 Code Review. Instructor: Tsung-Che Chiang
EX3 Code Review Instructor: Tsung-Che Chiang tcchiang@ieee.org Department of Computer Science and Information Engineering National Taiwan Normal University The Practice of Programming, CSIE@NTNU, Fall,
More informationBasic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable
Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is
More informationSuppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }
Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3;
More informationCSE 303: Concepts and Tools for Software Development
CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects
More informationIntroducing C++ to Java Programmers
Introducing C++ to Java Programmers by Kip Irvine updated 2/27/2003 1 Philosophy of C++ Bjarne Stroustrup invented C++ in the early 1980's at Bell Laboratories First called "C with classes" Design Goals:
More informationCSE 333 Midterm Exam Nov. 3, 2017 Sample Solution
Question 1. (8 points) Making things. Consider this (buggy) Makefile (the options -Wall, -g, and -std=c11 were omitted to save space and do not affect the answers): all: prog foo.c: foo.c foo.h bar.h gcc
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationCopying Data. Contents. Steven J. Zeil. November 13, Destructors 2
Steven J. Zeil November 13, 2013 Contents 1 Destructors 2 2 Copy Constructors 11 2.1 Where Do We Use a Copy Constructor? 12 2.2 Compiler-Generated Copy Constructors............................................
More informationOverloading Operators in C++
Overloading Operators in C++ C++ allows the programmer to redefine the function of most built-in operators on a class-by-class basis the operator keyword is used to declare a function that specifies what
More informationPart of the Picture: Simulation
Part of the Picture: Simulation Random Number Generators The RandomInt Class The Part of the Picture: Simulation section in Chapter 5 referred to a class RandomInt, which can be used to conveniently generate
More informationIntroduction to C++ (Extensions to C)
Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.
More informationPIC 10A Objects/Classes
PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous
More informationReference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that
Reference Parameters There are two ways to pass arguments to functions: pass-by-value and pass-by-reference. pass-by-value A copy of the argument s value is made and passed to the called function. Changes
More information04-19 Discussion Notes
04-19 Discussion Notes PIC 10B Spring 2018 1 Constructors and Destructors 1.1 Copy Constructor The copy constructor should copy data. However, it s not this simple, and we need to make a distinction here
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationMotivation for Templates
Motivation for You want both: a list of Location objects a list of MazeMonster objects 1 How can you accomplish this by writing one LinkedList class? state all the ways you can think of doing this state
More informationIII. Classes (Chap. 3)
III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,
More informationCSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington
CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia New exercise posted yesterday afternoon, due Monday morning - Read a directory
More informationOutline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor
Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened
More informationThe Memory Manager Project
The Memory Manager Project Objectives The goal of your next project is to simulate the C heap manager A runtime module used to allocate and de-allocate dynamic memory. The "heap" is a large "pool" of memory
More informationOutline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.
Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations
More informationLists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.
1 2 3 MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010 1 2 3 efficient updates with lists At http://www.sgi.com/tech/stl/ is the Standard Template Library Programmer
More informationLecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)
Lecture 10 Class string Namespaces Preprocessor directives Macros Conditional compilation Command line arguments Character handling library void* TNCG18(C++): Lec 10 1 Class string Template class
More informationCS242 COMPUTER PROGRAMMING
CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing
More informationLecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures
Lecture 10 Class string Exception Handling To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures Class template auto_ptr Lec 10 Programming in C++ 1 Class
More informationChapter 15 - C++ As A "Better C"
Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference
More informationLinked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4
Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 4 1 2 Coding for Linked Lists 8 2.1 Traversing a Linked List............................... 12 2.2 Searching a Linked List................................
More informationAssignment 4: SkipList
Assignment 4: SkipList Goals : Working with dynamic arrays, pointers, doubly linked lists For this assignment, you will write a Skip List data structure to store integers. When searching a Skip List, items
More informationIntroduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More informationVectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects
Vectors of Objects As we have mentioned earlier, you should almost always use vectors instead of arrays. If you need to keep track of persons (objects of class Person), you must decide what to store in
More informationOperator Overloading
Steven Zeil November 4, 2013 Contents 1 Operators 2 1.1 Operators are Functions.... 2 2 I/O Operators 4 3 Comparison Operators 6 3.1 Equality Operators....... 11 3.2 Less-Than Operators...... 13 1 1 Operators
More informationC++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis
C++ Programming Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis michail.lampis@dauphine.fr Classes These (and the previous) slides demonstrate a number of C++ features related
More informationCSE 333 Midterm Exam July 24, Name UW ID#
Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,
More informationCSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010
CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)
More informationUEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading
UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading What you will learn from Lab 7 In this laboratory, you will learn how to use operator overloading and function
More informationEL2310 Scientific Programming
Lecture 14: Object Oriented Programming in C++ (fpokorny@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Wrap Up Introduction to Object Oriented Paradigm Classes More on Classes
More informationPraktikum: Entwicklung interaktiver eingebetteter Systeme
Praktikum: Entwicklung interaktiver eingebetteter Systeme C++-Labs (falk@cs.fau.de) 1 Agenda Writing a Vector Class Constructor, References, Overloading Templates, Virtual Functions Standard Template Library
More informationHomework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine
Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013 Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars
More informationCS 201, Fall 2018 Homework Assignment 1
CS 201, Fall 2018 Homework Assignment 1 Due: 23:59, November 21 (Wednesday), 2018 In this homework, you will implement a music album collection system to store the song names of the music albums in a particular
More informationSmart Pointers, deleted functions, and 2-3 trees
Smart Pointers, deleted functions, and 2-3 trees But first Any questions about your current homework assignment? Due Thursday night by 11:59pm Make-up oral exam: I will have a sign-up sheet on Thursday
More informationCS 211 Winter 2004 Sample Final Exam (Solutions)
CS 211 Winter 2004 Sample Final Exam (Solutions) Instructor: Brian Dennis, Assistant Professor TAs: Tom Lechner, Rachel Goldsborough, Bin Lin March 16, 2004 Your Name: There are 6 problems on this exam.
More information