Class: Special Topics Overloading (methods) Copy Constructors

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Starting Out with C++: Early Objects 5th Edition
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
ITEC 320 C++ Examples.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
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?
Chapter -6 Polymorphism
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
 2006 Pearson Education, Inc. All rights reserved Templates.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Overloading C++ supports the concept of overloading Two main types
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Learning Objectives Pointers as dada members
Classes C++ representation of an object
Submission Example May 14, 2018
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?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
C# - Inheritance and Polymorphism
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?
Class: Special Topics Copy Constructors Static members Friends this
Templates.
Templates in C++.
group work #hifiTeam
Java Programming Language
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS150 Introduction to Computer Science 1
Static in Classes CSCE 121 J. Michael Moore.
Object Oriented Programming Using C++
JAVA Constructors.
Assessment – Java Basics: Part 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
9-10 Classes: A Deeper Look.
Parasol Lab, Texas A&M University
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS 144 Advanced C++ Programming February 21 Class Meeting
TOPIC: FUNCTION OVERLOADING
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
Constructors & Destructors
9-10 Classes: A Deeper Look.
Presentation transcript:

Class: Special Topics Overloading (methods) Copy Constructors Static members

Overloading (methods) The same function (i.e. function name) can be defined multiple times in a class. but, each definition must have a different list of parameters The compiler decides which to use based on the argument list Considerations Number of arguments Types of arguments Inheritance Lower-level method will be chosen over a higher level

Method overloading (type based) class PrintData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; void print(string c) { cout << "Printing string: " << c << endl; }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print string pd.print("Hello C++"); return 0; }

Method overloading (number based) class PrintData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(int i, int j) { cout << "Printing 2 ints: " << i << “ “ << j << endl; }; int main(void) { PrintData pd; // Call print to print integer pd.print(5); // Call print to print two integers pd.print(5,10); return 0; } Examples: FuncOver and Person

Copy constructor Example: General/Date A copy constructor is a special constructor that takes a class instance of the same type as an argument It is called automatically when the compiler needs to make a copy of a class instance Ex: when passing a class instance to a function “by value” Input argument should always be a const reference MyClass(const MyClass&); const ensures that the argument will not be modified Reference ensures that the argument will not be copied Otherwise, the copy constructor would be called again to make the copy Infinite loop!!!! Example: General/Date

Static members A static member of a class is specified using the keyword static static const string seasons[]; // in header file ClassName::seasons[]= { “spring”, “summer”, “winter”, “fall” }; // in .cpp file Member functions can also be static! Static members do not belong to any instance of the class They are “global” Static members are accessed using the class name directly string now= ClassName::seasons[2]; An instance is not required (no “.” operator!) – and should not be used Example: General/Date