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.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Road Map Introduction to object oriented programming. Classes
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Function Part II: Some ‘advanced’ concepts on functions.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 10 Introduction to Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
ECE122 Feb. 22, Any question on Vehicle sample code?
CPS120: Introduction to Computer Science Lecture 14 Functions.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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?
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Chapter -6 Polymorphism
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
93 4/11/98 CSE 143 Class Constructors [Sections ]
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Overloading C++ supports the concept of overloading Two main types
Constructors and Destructors
Pointers and Dynamic Arrays
CSC 143 Introduction to C++ Classes and User-Defined Data Types
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?
Function Overloading.
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?
Constructor & Destructor
Classes and Data Abstraction
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
More ‘concepts’ on Function
Constructors and Destructors
Java Programming Language
Class: Special Topics Overloading (methods) Copy Constructors
Constructors & Destructors
More ‘concepts’ on Function
Corresponds with Chapter 5
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

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 is declared  Constructor’s name is class name  No explicit return type, not even void...

CSC 143F 3 Multiple Constructors  May be several reasonable ways to initialize a class instance  Multiple constructors  All have same name (name of class)  Distinguished by number and types of arguments  Example of “overloading” (more later)

CSC 143F 4 Multiple Constructors (review) //complex.h--simple complex number class class Complex { public: // constructors: Complex( );// = 0+0i Complex(double a, double b); // = a+bi // double -> Complex constructor Complex(double r);// = r+0i... };

CSC 143F 5 Default parameters //complex.h--simple complex number class class Complex { public: // combine all previous constructors // in one single constructor // Complex( );// = 0+0i // Complex(double r);// = r+0i Complex(double a=0, double b=0); // Complex() means Complex(0,0) // Complex(3.2) means Complex(3.2,0) };

CSC 143F 6 Default Constructor  If no explicit constructor is given, a default is supplied by compiler  Takes no arguments, does nothing  Not guaranteed to perform any initialization  Invisible  If a class has one or more “non-default” constructor:  then NO “default” constructor will be supplied; variable declaration without initialization fails.

CSC 143F 7 Guidelines for Constructors  A constructor cannot return a value  so it must be declared without a return type  A class may provide multiple constructors  Compiler will choose appropriate one, depending on context.  Syntax for invoking a constructor Complex a1; Complex a3 = Complex(123.45); Complex a2(10.0, -1); But not this: Complex a4();

CSC 143F 8 CSC 143 Overloading [Chapter 8, pp ]

CSC 143F 9 Name Scope and Visibility  Variable and function names may be repeated in different scopes int myVar; void Snork(int myVar) {... while ( a > b){ int myVar = 0;... } }  Style issue: using same name in multiple scopes can be confusing

CSC 143F 10 Overloading  Reusing names  Function names can be reused  Differentiate based on scope and context Global vs. class vs. local, etc.  Different functions in same scope can have same name if argument number and/or types different  Name is said to be overloaded  Operators (+, =, etc.) can also be overloaded (ex. int+int, double+double, Complex+Complex)

CSC 143F 11 Overloaded Function Names  Useful if one name has multiple meanings or multiple interfaces  Constructors are common example of overloading since all must have same name  Compiler determines which function to call at compile-time

CSC 143F 12 Resolving Overloaded Functions To "resolve" mean to decide which version of the overloaded function is being called  Determined by matching actual arguments against possible formal arguments  Compiler gives error if not exactly one matches  Complete matching algorithm rather complex  If match is not exact, automatic type conversions are used

CSC 143F 13 Matching Algorithm  Function declarations void Snark(int); void Snark(double); void Snipe(char []); void Snipe(double); void Sneep(char); void Sneep(double); Snark(1);// Integer Snark Snipe(1);// Double Snipe Sneep(1);// Ambiguous

CSC 143F 14 Example of Resolving  Function declarations void PrintData(int data) { cout << "int = " << data << endl; } void PrintData(char data) { cout << "char = '" << data << "'\n"; } void PrintData(double data) { cout << "double = " << data << endl; }  Which calls are valid? PrintData(3); PrintData("Hello"); PrintData( ); PrintData('m');

CSC 143F 15 Overloaded Operators  For convenience, can define functions named +, -, *, =, /, ==, etc. on classes  Gives natural expression to some operations  Very confusing if abused  Operator functions may be members or friends  Function “names” are operator+, operator-,...  Several ways of calling them