Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
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.
Operator Overloading Fundamentals
C++ Classes & Data Abstraction
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Constructors & Destructors Review CS 308 – Data Structures.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CMSC 2021 Stream I/O Operators and Friend Functions.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Negation Operator. Code Trace // main.cpp... g = -f;... // fraction.h... class Fraction { friend Fraction operator-(const.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
Learners Support Publications Functions in C++
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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?
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Programming Principles II Lecture Notes 3.1 Void Functions Andreas Savva.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Learners Support Publications Constructors and Destructors.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Static Members.
Constructors and Destructors
Review What is an object? What is a class?
Andy Wang Object Oriented Programming in C++ COP 3330
Constructor & Destructor
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Andy Wang Object Oriented Programming in C++ COP 3330
Accessor and Mutator Functions
Friend Functions.
CS150 Introduction to Computer Science 1
Constructors and destructors
Constructors and Destructors
Bracket Operator.
Constructors and Destructors
COP 3330 Object-oriented Programming in C++
Function Overloading.
Dynamic Memory.
Anatomy of a class Part II
CMSC 202 Lesson 6 Functions II.
Object-Oriented Programming (OOP) Lecture No. 44
Class: Special Topics Overloading (methods) Copy Constructors
Defining Class Functions
Class rational part2.
Chapter 11 Classes.
Constructors & Destructors
Presentation transcript:

Constructors

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); // no constructor to accept two ints...

Constructors  Constructors are always named the name of the class.

Constructors  Constructors are always named the name of the class.  Constructors have no return type (this is not void) and hence have no return statement.

Constructors  Constructors are always named the name of the class.  Constructors have no return type (this is not void) and hence have no return statement.  Constructors can be overloaded like any other function, and you will most likely do so.

Constructors  Constructors are always named the name of the class.  Constructors have no return type (this is not void) and hence have no return statement.  Constructors can be overloaded like any other function, and you will most likely do so.  Constructors are called by the compiler automatically; they are rarely called explicitly by the programmer.

Constructors  Constructors are always named the name of the class.  Constructors have no return type (this is not void) and hence have no return statement.  Constructors can be overloaded like any other function, and you will most likely do so.  Constructors are called by the compiler automatically; they are rarely called explicitly by the programmer.  If you write no constructor, the compiler will provide your class with a default constructor. The mechanism is suppressed once you write any constructor.

First Constructor class Fraction { public: Fraction(const int num, const int den); void readin(); void print();... };

First Constructor class Fraction { public: Fraction(const int num, const int den); void readin(); void print();... }; Fraction::Fraction(const int num, const int den) { m_Numerator = num; m_Denominator = den; }

First Constructor class Fraction { public: int Fraction(const int num, const int den); void readin(); void print();... }; Fraction::Fraction(const int num, const int den) { m_Numerator = num; m_Denominator = den; } Take Note: No Return Statements or Types NO!

First Constructor class Fraction { public: Fraction(const int num, const int den); void readin(); void print();... }; void Fraction::Fraction(const int num, const int den) { m_Numerator = num; m_Denominator = den; } Take Note: No Return Statements or Types NO!

First Constructor class Fraction { public: Fraction(const int num, const int den); void readin(); void print();... }; Fraction::Fraction(const int num, const int den) { m_Numerator = num; m_Denominator = den; return; } Take Note: No Return Statements or Types NO!

What if… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0);... // fraction.cpp Fraction::Fraction(const int num, const int den) { m_Numerator = num; m_Denominator = den; }

What if… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f;DENOMINATOR ZERO !! Fraction g(4, 0);... // fraction.cpp Fraction::Fraction(const int num, const int den) { m_Numerator = num; m_Denominator = den; // allows ALL integer values, even zero! }

What if fix… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0);... // fraction.cpp Fraction::Fraction(const int num, const int den) { setNum(num); setDen(den); // let mutator function vet the argument passed }

An Initialization List // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0);... // fraction.cpp Fraction::Fraction(const int num, const int den) : m_Numerator(num),m_Denominator(den){} // note: you must have a function body in an initialization list // even if it is empty.

What if fix… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0);... // fraction.cpp Fraction::Fraction(const int num, const int den) : m_Numerator(num),m_Denominator(den) { if(m_Denominator == 0) { cout << “error: 0 passed in as denominator” << endl; exit(1); } }

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);... Because we have provided one constructor, the compiler no longer automatically provides a default constructor – one that takes no arguments.

Default Constructor // main.cpp... int main() { Fraction f;... // fraction.h... class Fraction { Fraction(); // default constructor... // fraction.cpp Fraction::Fraction() : m_Numerator(0), m_Denominator(1) {} // defaults to 0/1

Copy Constructor #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); Fraction h(g); // create a fraction that is a copy of another...

Copy Constructor // main.cpp... int main() { Fraction g(4, 5); Fraction h(g)... // fraction.h... class Fraction { Fraction(const Fraction & source);...

Copy Constructor // main.cpp... int main() { Fraction g(4, 5); Fraction h(g)... // fraction.h... class Fraction { Fraction(const Fraction & source);... // fraction.cpp Fraction::Fraction(const Fraction & source) : m_Numerator(source.m_Numerator), m_Denominator(source.m_Denominator) {}

Initializing New Objects A Warning! #include “fraction.h” int main() { fraction f();// NEVER do this. The compiler thinks you are...// declaring a function named “f” that has no // parameters but returns a fraction.

End of Session