Operator overloading. Instructor: Bakhyt Bakiyev
|
|
- Lynne Alexander
- 5 years ago
- Views:
Transcription
1 Operator overloading Instructor: Bakhyt Bakiyev
2 content Operator overloading
3 Operator Overloading CONCEPT: C++ allows you to redefine how standard operators work when used with class objects.
4 Approaches to Operator Overloading There are two approaches you can take to overload an operator: 1. Make the overloaded operator a member function of the class. This allows the operator function access to private members of the class. It also allows the function to use the implicit this pointer parameter to access the calling object. 2. Make the overloaded member function a separate, stand-alone function. When overloaded in this manner, the operator function must be declared a friend of the class to have access to the private members of the class. Some operators, such as the stream input and output operators >> and <<, must be overloaded as stand-alone functions. Other operators may be overloaded either as member functions or stand-alone functions.
5 Overloading the = Operator class NumberArray private: double *aptr; int arraysize; public: void operator=(const NumberArray &right); // Overloaded operator. NumberArray(const NumberArray &); NumberArray(int size, double value); ~NumberArray() if (arraysize > 0) delete [ ] aptr; void print(); void setvalue(double value); ;
6
7 To change the meaning of = operator class Weird private: int value; public: Weird(int v) value = v; void operator=(const Weird &right) cout << right.value << endl; ;
8 Although the operator= function overloads the assignment operator, the function doesn t perform an assignment. All the overloaded operator does is display the contents of right.value. Consider the following program segment: Weird a(5), b(10); a = b; Although the statement a = b looks like an assignment statement, it actually causes the contents of b s value member to be displayed on the screen: 10
9 Example 1 class complex float x,y; public: complex() x=0;y=0; ; complex(float real, float imag) x=real; y=imag; friend complex operator+(complex a, complex b) return complex(a.x+b.x, a.y+b.y); friend complex operator-(complex a, complex b) return complex(a.x-b.x, a.y-b.y); void display(); ; void complex::display(void) cout<<x<<" + i"<<y<<endl;
10 Example 2 class complex float x; float y; public: complex() x=y=0; ; complex(float real, float imag) x=real; y=imag; complex operator+(complex c) complex temp; temp.x=x+c.x; temp.y=y+c.y; return(temp); complex operator-(complex c) complex temp; temp.x=x-c.x; temp.y=y-c.y; return(temp); void display(); ; void complex::display(void) cout<<x<<" + i"<<y<<endl;
11 main() int n; complex c1,c2,c3; c1=complex(2.5,3.5); c2=complex(1.6,2.7); cout<<"1-add/2-subtract :"; cin>>n; if(n==1) c3=c1+c2; if(n==2)c3=c1-c2; cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); cout<<"c3="; c3.display(); return 0;
12 Overloading the Arithmetic and Relational Operators friend Length operator+(length a, Length b); friend Length operator-(length a, Length b); friend bool operator<(length a, Length b); friend bool operator==(length a, Length b);
13 Length operator+(length a, Length b) Length result(a.len_inches + b.len_inches); return result; or more succinctly: Length operator+(length a, Length b) return Length(a.len_inches + b.len_inches);
14 Example class Length private: int len_inches; public: Length(int feet, int inches) setlength(feet, inches); Length(int inches) len_inches = inches; int getfeet() return len_inches / 12; int getinches() return len_inches % 12; void setlength(int feet, int inches) len_inches = 12 *feet + inches; friend Length operator+(length a, Length b); friend Length operator-(length a, Length b); friend bool operator< (Length a, Length b); friend bool operator== (Length a, Length b); ;
15 Length operator+(length a, Length b) return Length(a.len_inches + b.len_inches); Length operator-(length a, Length b) return Length(a.len_inches - b.len_inches); bool operator==(length a, Length b) return a.len_inches == b.len_inches; bool operator<(length a, Length b) return a.len_inches < b.len_inches;
16 int main() Length first(0), second(0), third(0); int f, i; cout << "Enter a distance in feet and inches: "; cin >> f >> i; first.setlength(f, i); cout << "Enter another distance in feet and inches: "; cin >> f >> i; second.setlength(f, i); // Test the + and - operators third = first + second; cout << "first + second = "; cout << third.getfeet() << " feet, "; cout << third.getinches() << " inches.\n"; third = first - second; cout << "first - second = "; cout << third.getfeet() << " feet, "; cout << third.getinches() << " inches.\n"; // Test the relational operators cout << "first == second = "; if (first == second) cout << "true"; else cout << "false"; cout << "\n"; cout << "first < second = "; if (first < second) cout << "true"; else cout << "false"; cout << "\n"; return 0; Program Output Enter a distance in feet and inches: 6 5 Enter another distance in feet and inches: 3 10 first + second = 10 feet, 3 inches. first - second = 2 feet, 7 inches. first == second = false first < second = false
17 Choosing Between Stand-Alone and Member-Function Operators To sum up, the addition operator (as well as other arithmetic and relational operators) can be overloaded equally well as member functions or as stand-alone functions. It is generally better to overload binary operators that take parameters of the same type as stand-alone functions. This is because, unlike stand-alone operator overloading, member-function overloading introduces an artificial distinction between the two parameters by making the left parameter implicit. This allows convert constructors to apply to the right parameter but not to the left, creating situations where changing the order of parameters causes a compiler error in an otherwise correct program: Length a(4, 2), c(0); c = a + 2; // Compiles, equivalent to c = a.operator+(2) c = 2 + a; // Does not compile: equivalent to c = 2.operator+(a);
18 Overloading the Prefix ++ Operator class Length private: int len_inches; public: // Declaration of prefix ++ Length operator++(); // Rest of class not shown ; Length Length::operator++() len_inches ++; return *this; Given this overload, the user-friendly notation ++b is equivalent to the call b.operator++(). Either notation may be used in your program.
19 Overloading the Postfix ++ Operator Length Length::operator++(int) Length temp = *this; len_inches ++; return temp; The first difference you will notice is that the function has a dummy parameter of type int that is never used in the body of the function. This is a convention that tells the compiler that the increment operator is being overloaded in postfix mode. The second difference is the use of a temporary local variable temp to capture the value of the object before it is incremented. This value is saved and is later returned by the function.
20 Overloading the Stream Insertion and Extraction Operators Length b(4, 8), c(2, 5); cout << b; cout << b + c; appear to the compiler as Length b(4, 8), c(2, 5); operator<<(cout, b); operator(cout, b + c);
21 insertion operator should be written as ostream &operator<<(ostream& out, Length a) out << a.getfeet() << " feet, " << a.getinches() << " inches"; return out; the header for the stream input operator looks like this: istream &operator>>(istream &in, Length &a);
22 Example class Length private: int len_inches; public: Length(int feet, int inches) setlength(feet, inches); Length(int inches) len_inches = inches; int getfeet() return len_inches / 12; int getinches() return len_inches % 12; void setlength(int feet, int inches) len_inches = 12 *feet + inches; friend Length operator+(length a, Length b); friend Length operator-(length a, Length b); friend bool operator<(length a, Length b); friend bool operator==(length a, Length b); Length operator++(); Length operator++(int); friend ostream &operator<<(ostream &out, Length a); friend istream &operator>>(istream &in, Length &a); ;
23 istream &operator>>(istream &in, Length &a) int feet, inches; cout << "Enter feet: "; in >> feet; cout << "Enter inches: "; in >> inches; // Modify the object a with the data and return a.setlength(feet, inches); return in; ostream &operator<<(ostream& out, Length a) out << a.getfeet() << " feet, " << a.getinches() << " inches"; return out;
24 Length Length::operator++() len_inches ++; return *this; Length Length::operator++(int) Length temp = *this; len_inches ++; return temp;
25 Length operator+(length a, Length b) return Length(a.len_inches + b.len_inches); Length operator-(length a, Length b) return Length(a.len_inches - b.len_inches); bool operator==(length a, Length b) return a.len_inches == b.len_inches; bool operator<(length a, Length b) return a.len_inches < b.len_inches;
26 int main() Length first(0), second(1, 9), c(0); cout << "Demonstrating prefix ++ operator and output operator.\n"; for (int count = 0; count < 4; count++) first = ++second; cout << "First: " << first << ". Second: " << second << ".\n"; cout << "\ndemonstrating postfix ++ operator and output operator.\n"; for (int count = 0; count < 4; count++) first = second++; cout << "First: " << first << ". Second: " << second << ".\n"; cout << "\ndemonstrating input and output operators.\n"; cin >> c; cout << "You entered " << c << "." << endl; return 0;
27 Program Output Demonstrating prefix ++ operator and output operator. First: 1 feet, 10 inches. Second: 1 feet, 10 inches. First: 1 feet, 11 inches. Second: 1 feet, 11 inches. First: 2 feet, 0 inches. Second: 2 feet, 0 inches. First: 2 feet, 1 inches. Second: 2 feet, 1 inches. Demonstrating postfix ++ operator and output operator. First: 2 feet, 1 inches. Second: 2 feet, 2 inches. First: 2 feet, 2 inches. Second: 2 feet, 3 inches. First: 2 feet, 3 inches. Second: 2 feet, 4 inches. First: 2 feet, 4 inches. Second: 2 feet, 5 inches. Demonstrating input and output operators. Enter feet: 3 Enter inches: 4 You entered 3 feet, 4 inches.
28 Overloading the [ ] Operator class IntArray private: int *aptr; int arraysize; void suberror(); // Handles subscripts out of range public: IntArray(int); // Constructor IntArray(const intarray &); // Copy constructor ~IntArray(); // Destructor int size() return arraysize; int &operator[](int); // Overloaded [] operator ;
29 //constructor IntArray::IntArray(int s) arraysize = s; aptr = new int [s]; for (int count = 0; count < size; count++) *(aptr + count) = 0; //copy constructor IntArray::IntArray(const IntArray &obj) arraysize = obj.arraysize; aptr = new int [arraysize]; for(int count = 0; count < arraysize; count++) *(aptr + count) = *(obj.aptr + count); //destructor IntArray::~IntArray() if (arraysize > 0) delete [] aptr;
30 The [] operator is overloaded similarly to other operators: int &IntArray::operator[](int sub) if (sub < 0 sub >= arraysize) suberror(); return aptr[sub];
31 Example 1 class IntArray private: int *aptr; int arraysize; void suberror(); // Handles subscripts out of range public: IntArray(int); // Constructor IntArray(const IntArray &); // Copy constructor ~IntArray(); // Destructor int size() return arraysize; int &operator[](int); // Overloaded [] operator ;
32 IntArray::IntArray(int s) arraysize = s; aptr = new int [s]; for (int count = 0; count < arraysize; count++) *(aptr + count) = 0; IntArray::IntArray(const IntArray &obj) arraysize = obj.arraysize; aptr = new int [arraysize]; for(int count = 0; count < arraysize; count++) *(aptr + count) = *(obj.aptr + count); IntArray::~IntArray() if (arraysize > 0) delete [] aptr;
33 void IntArray::subError() cout << "ERROR: Subscript out of range.\n"; exit(0); int &IntArray::operator[](int sub) if (sub < 0 sub >= arraysize) suberror(); return aptr[sub];
34 int main() IntArray table(10); // Store values in the array. for (int x = 0; x < table.size(); x++) table[x] = (x * 2); // Display the values in the array. for (int x = 0; x < table.size(); x++) cout << table[x] << " "; // Use the built-in + operator on array elements. for (int x = 0; x < table.size(); x++) table[x] = table[x] + 5; // Display the values in the array. for (int x = 0; x < table.size(); x++) cout << table[x] << " "; // Use the built-in ++ operator on array elements. for (int x = 0; x < table.size(); x++) table[x]++; // Display the values in the array. for (int x = 0; x < table.size(); x++) cout << table[x] << " "; return 0; Program Output
35 Example 2 int main() IntArray table(10); // Store values in the array. for (int x = 0; x < table.size(); x++) table[x] = x; // Display the values in the array. for (int x = 0; x < table.size(); x++) cout << table[x] << " "; cout << endl; Program Output Attempting to store outside the array bounds: ERROR: Subscript out of range. cout << "Attempting to store outside the array bounds:\n"; table[table.size()] = 0; return 0;
36 Type Conversion Operators CONCEPT: Special operator functions may be written to convert a class object to any other type. Suppose a program uses the following variables: int i; double d; The following statement automatically converts the value in i to a double and stores it in d: d = i; Likewise, the following statement converts the value in d to an integer (truncating the fractional part) and stores it in i: i = d; The same functionality can also be given to class objects.
37 Example class Length private: int len_inches; public: Length(int feet, int inches) setlength(feet, inches); Length(int inches) len_inches = inches; int getfeet() return len_inches / 12; int getinches() return len_inches % 12; void setlength(int feet, int inches) len_inches = 12 *feet + inches; // Type conversion operators operator double(); operator int() return len_inches; // Overloaded stream output operator friend ostream &operator<<(ostream &out, Length a); ;
38 Length::operator double() return len_inches /12 + (len_inches %12) / 12.0; ostream &operator<<(ostream& out, Length a) out << a.getfeet() << " feet, " << a.getinches() << " inches"; return out;
39 int main() Length distance(0); double feet; int inches; distance.setlength(4, 6); cout << "The Length object is " << distance << "." << endl; // Convert and print feet = distance; inches = distance; cout << "The Length object measures " << feet << " feet." << endl; cout << "The Length object measures " << inches << " inches." << endl; return 0; Program Output The Length object is 4 feet, 6 inches. The Length object measures 4.5 feet. The Length object measures 54 inches.
40 Literature Tony Gaddis, Judy Walters, Godfrey Muganda, Starting out with C++, 2011.
More Advanced Class Concepts
More Advanced Class Concepts Operator overloading Inheritance Templates PH (RH) (Roger.Henriksson@cs.lth.se) C++ Programming 2016/17 146 / 281 Operator Overloading In most programming languages some operators
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 informationChapter 11: More About Classes and Object-Oriented Programming
Chapter 11: More About Classes and Object-Oriented Programming Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Topics 11.1 The this Pointer and Constant
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 informationl Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:
Operator Overloading & Templates Week 6 Gaddis: 14.5, 16.2-16.4 CS 5301 Spring 2018 Jill Seaman Operator Overloading l Operators such as =, +,
More informationReview. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?
Review Describe pass-by-value and pass-by-reference Why do we use pass-by-reference? What does the term calling object refer to? What is a const member function? What is a const object? How do we initialize
More information! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:
Operator Overloading, Lists and Templates Week 6 Gaddis: 14.5, 16.2-16.4 CS 5301 Spring 2016 Jill Seaman Operator Overloading! Operators such as =, +,
More information14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members
Chapter 14: More About Classes 14.1 Instance and Static Members Instance and Static Members instance variable: a member variable in a class. Each object has its own copy. static variable: one variable
More informationOBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES
Polymorphism: It allows a single name/operator to be associated with different operations depending on the type of data passed to it. An operation may exhibit different behaviors in different instances.
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 informationOperator Overloading
C++ Programming: Operator Overloading 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Operator overloading Basics on operator overloading Overloading operators as
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 informationOperator overloading: extra examples
Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading
More informationModule Operator Overloading and Type Conversion. Table of Contents
1 Module - 33 Operator Overloading and Type Conversion Table of Contents 1. Introduction 2. Operator Overloading 3. this pointer 4. Overloading Unary Operators 5. Overloading Binary Operators 6. Overloading
More informationProgram construction in C++ for Scientific Computing
1 (26) School of Engineering Sciences Program construction in C++ for Scientific Computing 2 (26) Outline 1 2 3 4 5 6 3 (26) Our Point class is a model for the vector space R 2. In this space, operations
More informationOverloading & Polymorphism
Overloading & Polymorphism Overloading is considered ad-hoc polymorphism. 1 Can define new meanings (functions) of operators for specific types. Compiler recognizes which implementation to use by signature
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 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 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 informationIS0020 Program Design and Software Tools Midterm, Fall, 2004
IS0020 Program Design and Software Tools Midterm, Fall, 2004 Name: Instruction There are two parts in this test. The first part contains 22 questions worth 40 points you need to get 20 right to get the
More informationOverloaded Operators, Functions, and Students
, Functions, and Students Division of Mathematics and Computer Science Maryville College Outline Overloading Symbols 1 Overloading Symbols 2 3 Symbol Overloading Overloading Symbols A symbol is overloaded
More informationObject-Oriented Principles and Practice / C++
Object-Oriented Principles and Practice / C++ Alice E. Fischer June 3, 2013 OOPP / C++ Lecture 9... 1/40 Const Qualifiers Operator Extensions Polymorphism Abstract Classes Linear Data Structure Demo Ordered
More information7.1 Optional Parameters
Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default
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 informationKOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management
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 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 informationStarting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key
Starting Out with C++ Early Objects 9th Edition Gaddis TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/starting-c-early-objects-9thedition-gaddis-test-bank/ Starting
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 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 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 informationCh. 12: Operator Overloading
Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42
More informationA First Program - Greeting.cpp
C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout
More informationDue 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 informationIntermediate Programming & Design (C++) Classes in C++
Classes in C++ A class is a data type similar to a C structure. It includes various local data (called data members) together with constructors, destructors and member functions. All of them are called
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved
Chapter 8 Operator Overloading, Friends, and References Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Basic Operator Overloading Unary operators As member functions Friends
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 informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationOperation Overloading.
Operation Overloading pm_jat@daiict.ac.in Recap: Why do we need Operator Overloading? Operator based expressions are more readable: Compare a + b * c with plus(a, times(b, c)) Recap: What is Operator Overloading?
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 informationSearching Algorithms. Chapter 11
Data Structures Dr Ahmed Rafat Abas Computer Science Dept, Faculty of Computer and Information, Zagazig University arabas@zu.edu.eg http://www.arsaliem.faculty.zu.edu.eg/ Searching Algorithms Chapter 11
More informationObject-Oriented Design (OOD) and C++
Chapter 2 Object-Oriented Design (OOD) and C++ At a Glance Instructor s Manual Table of Contents Chapter Overview Chapter Objectives Instructor Notes Quick Quizzes Discussion Questions Projects to Assign
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 informationThis test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.
University of Toronto Faculty of Applied Science and Engineering ECE 244F PROGRAMMING FUNDAMENTALS Fall 2013 Midterm Test Examiners: T.S. Abdelrahman, V. Betz, M. Stumm and H. Timorabadi Duration: 110
More informationInstance and Static Members Each instance of a class has its own copies of the class s instance (member) variables
More About Classes Instance and Static Members Each instance of a class has its own copies of the class s instance (member) variables Objects box1 and box2 of class Rectangle each have their own values
More informationMore class design with C++ Starting Savitch Chap. 11
More class design with C++ Starting Savitch Chap. 11 Member or non-member function? l Class operations are typically implemented as member functions Declared inside class definition Can directly access
More informationData type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:
Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before
More informationCHAPTER 3 Expressions, Functions, Output
CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number
More informationUnit 3. Operators. School of Science and Technology INTRODUCTION
INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.
More informationCHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object
CHAPTER 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Computer Systems: Hardware and Software 2 1.3 Programs and Programming Languages 8 1.4 What is a Program Made of? 14 1.5 Input,
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationName: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015
CMSC 202 Section 06 Fall 2015 Computer Science II Midterm Exam II Name: Username: Score Max Section: (check one) 07 - Sushant Athley, Tuesday 11:30am 08 - Aishwarya Bhide, Thursday 11:30am 09 - Phanindra
More informationCh 8. Operator Overloading, Friends, and References
2014-1 Ch 8. Operator Overloading, Friends, and References May 28, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel
More informationObject oriented programming
Exercises 6 Version 1.0, 21 March, 2017 Table of Contents 1. Operators overloading....................................................... 1 1.1. Example 1..............................................................
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 informationCISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:
CISC 2200 Data Structure Fall, 2016 C++ Review:3/3 1 From last lecture: pointer type and pointer variable (stores memory addresses of a variable (of any type, local or global, automatic/static/dynamic)
More informationC++ 8. Constructors and Destructors
8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of
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 informationSFU CMPT Topic: Has-a Relationship
SFU CMPT-212 2008-1 1 Topic: Has-a Relationship SFU CMPT-212 2008-1 Topic: Has-a Relationship Ján Maňuch E-mail: jmanuch@sfu.ca Friday 22 nd February, 2008 SFU CMPT-212 2008-1 2 Topic: Has-a Relationship
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 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 informationProgramming. C++ Basics
Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++
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 informationCSCI 123 Introduction to Programming Concepts in C++
CSCI 123 Introduction to Programming Concepts in C++ Brad Rippe Brad Rippe More Classes and Dynamic Arrays Overview 11.4 Classes and Dynamic Arrays Constructors, Destructors, Copy Constructors Separation
More informationThe University Of Michigan. EECS402 Lecture 15. Andrew M. Morgan. Savitch Ch. 8 Operator Overloading Returning By Constant Value
The University Of Michigan Lecture 15 Andrew M. Morgan Savitch Ch. 8 Operator Overloading Returning By Constant Value Consider This Program class ChangePocketClass public: ChangePocketClass():quarters(0),dimes(0)
More informationOperator Overloading and Templates. Linear Search. Linear Search in C++ second attempt. Linear Search in C++ first attempt
Operator Overloading and Templates Week 6 Gaddis: 8.1, 14.5, 16.2-16.4 CS 5301 Fall 2015 Jill Seaman Linear Search! Search: find a given target item in an array, return the index of the item, or -1 if
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationG52CPP C++ Programming Lecture 17
G52CPP C++ Programming Lecture 17 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Exceptions How to throw (return) different error values as exceptions And catch the exceptions
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 informationDistributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes
Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented
More informationFunction Overloading
Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures
More informationPlan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz
Plan of the day Where are we at? session 1: basic language constructs session 2: pointers and functions session 3: basic class and operator overloading Today design of two types of container classes templates
More informationInstantiation of Template class
Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double
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 informationCAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE
CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What
More informationLecture 3 ADT and C++ Classes (II)
CSC212 Data Structure - Section FG Lecture 3 ADT and C++ Classes (II) Instructor: Feng HU Department of Computer Science City College of New York @ Feng HU, 2016 1 Outline A Review of C++ Classes (Lecture
More informationGeneric Programming: Overloading and Templates
H.O. #9 Fall 2015 Gary Chan Generic Programming: Overloading and Templates N:9; D:6,11,14 Outline Function overloading Operator overloading and copy constructor An example on string Function templates
More information3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.
3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.
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/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/
/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/ #1: Give the output for the following program. #include
More informationDo not write in this area TOTAL. Maximum possible points: 75
Name: Student ID: Instructor: Borja Sotomayor Do not write in this area 1 2 3 4 5 6 7 TOTAL Maximum possible points: 75 This homework assignment is divided into two parts: one related to the fundamental
More informationLesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)
Lesson Plan Name of the Faculty Discipline Semester :Mrs. Reena Rani : Computer Engineering : IV Subject: OBJECT ORIENTED PROGRAMMING USING C++ Lesson Plan Duration :15 weeks (From January, 2018 to April,2018)
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 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 informationPage. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team
Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Question No: 1 ( Marks: 2 ) Write a declaration statement for an array of 10
More informationConcepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.
CSC 143 Classes with Dynamically Allocated Data Concepts (this lecture) Constructors and destructors for dynamic data The this pointer Deep versus shallow copy Defining assignment (operator=) Copy constructor
More informationChapter 3. Numeric Types, Expressions, and Output
Chapter 3 Numeric Types, Expressions, and Output 1 Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and Explicit Type Conversion Calling a Value-Returning
More informationClass 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)
Class 2: Variables and Memory Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs
More informationSFU CMPT Topic: Class Templates
SFU CMPT-212 2008-1 1 Topic: Class Templates SFU CMPT-212 2008-1 Topic: Class Templates Ján Maňuch E-mail: jmanuch@sfu.ca Monday 3 rd March, 2008 SFU CMPT-212 2008-1 2 Topic: Class Templates Class templates
More informationObject Oriented Programming 2012
1. Write a program to display the following output using single cout statement. Maths = 90 Physics =77 Chemestry =69 2. Write a program to read two numbers from the keyboard and display the larger value
More informationCS304 Object Oriented Programming Final Term
1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationObjectives. In this chapter, you will:
Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about
More informationExam 3 Chapters 7 & 9
Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False
More informationNumerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2
Numerical Computing in C and C++ Jamie Griffin Semester A 2017 Lecture 2 Visual Studio in QM PC rooms Microsoft Visual Studio Community 2015. Bancroft Building 1.15a; Queen s W207, EB7; Engineering W128.D.
More informationOBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe
OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++
More informationCSci 1113 Final. Name: Student ID:
CSci 1113 Final Name: Student ID: Instructions: Please pick and answer any 10 of the 12 problems for a total of 100 points. If you answer more than 10 problems, only the first 10 will be graded. The time
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
More information