Anatomy of a class Part II

Slides:



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

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
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.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
TCP1201 OOPDS 1 Class & Template Lecture 2. TCP1201 OOPDS 2 Learning Objectives: To understand object behaviors To understand constructors To understand.
ECE122 Feb. 22, Any question on Vehicle sample code?
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
1 CSC241: Object Oriented Programming Lecture No 03.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Learners Support Publications Constructors and Destructors.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Visit for more Learning Resources
Review What is an object? What is a class?
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
CONSTRUCTORS & DESTRUCTORS
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Anatomy of a class Part II
Anatomy of a class Part I
Operator Overloading CSCE 121 J. Michael Moore
Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before.
Const in Classes CSCE 121 J. Michael Moore.
An Introduction to Java – Part II
Code Organization Classes
Learning Objectives Classes Constructors Principles of OOP
Anatomy of a Function Part 1
Constructors and Other Tools
Constructors and Destructors
JAVA Constructors.
Function Overloading CSCE 121 J. Michael Moore
Anatomy of Inheritance
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
C++ Constructor Insanity CSE 333 Summer 2018
Method of Classes Chapter 7, page 155 Lecture /4/6.
C++ Constructor Insanity CSE 333 Autumn 2018
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
C++ Constructor Insanity CSE 333 Winter 2019
Anatomy of a Function Part 1
Object oriented programming (OOP) Lecture No. 6
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Code Organization Classes
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Constructors & Destructors
Anatomy of a class Part I
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Anatomy of a class Part II CSCE 121 Based on Slides created by Carlos Soto.

Constructor Special method / member function Initializes an object Recall passing filename to file streams when they were declared / defined Recall RAII Primary way to support passing needed info into object to set it up correctly

Constructors class Student { string name; int id; public: Student (string name, int id); }; No return type. Not even void.

Preferred!!! Constructors Member Initialization List. class Student { string name; int id; public: Student (string name, int id): name (name), id (id) {} }; No confusion on which is which. Student (string name, int id): name (name), id (id) {}  string name(name) int id(id) Preferred!!!

Needed for anything prior to C++ 11. Constructors class Student { string name; int id; public: Student (string name, int id) { this->name = name; this->id = id; } }; Needed for anything prior to C++ 11. Not preferred.

Constructors class Student { // ... public: Student (string name, int id); }; int main() { Student joe("Joe Smith", 123456); }

Can move constructor definition outside of class definition. Constructors Can move constructor definition outside of class definition. class Student { string name; int id; public: Student (string name, int id); }; Student::Student (string name, int id): name(name), id(id) {}

Overloading constructors class Student { string name; int id; public: Student (string name, int id) : name(name), id(id) {} Student (string name) : name(name), id(0) {} };

No arguments / parameters. Default constructor class Student { string name; int id; public: Student (string name, int id) : name(name), id(id) {} Student (string name) : name(name), id(0) {} Student () : name(""), id(0) {} }; No arguments / parameters.

Using a default constructor class Student { // ... public: Student (string name, int id); Student (); }; int main () { Student joe("Joe Smith", 123456); Student amy; Student undergraduates[1000]; }

Notes on the Default constructor If there are no constructors defined, compiler automatically creates a default constructor. This is what happened when we declared a class before we learned about constructors. Calls default constructor on all class data types. If you define other constructors, but no default constructor, and the compiler needs one, then compiler error. You should go ahead and create your own default constructor. You know better than the compiler what default values each data member needs.

Using a class class Student { // ... public: Student (string name, int id); string getName(); }; int main() { Student joe("Joe Smith", 123456); string name = joe.getName(); }

class Student { // ... int id; void generateID(); public: Student (string name) : name(name) { generateID(); } string getID() { return id; }; int main() { Student joe("Joe Smith"); string id = joe.getID(); }

These constructors can be defined in two ways. class Student { // ... public: Student (); Student (string name, int id); }; int main() { Student joe("Joe Smith", 123456); } These constructors can be defined in two ways.

Initialization via default constructor: // 1st Pre C++11 Student::Student(string name, int id) { this->name = name; // assignment this->id = id; // assignment } Values set twice. Initialization via default constructor: name initialized to empty string with string’s default constructor Assignment in constructor with parameters: name is set to value passed as parameter id is set to value passed as a parameter this->name and this->id Default constructor for string called before body of this constructor is executed.

Initialization in constructor with parameters // 2nd C++ 11 and later Student::Student(string name, int id) : name(name), id(id) {} Values set once. Initialization in constructor with parameters name is initialized with parameter that is passed into string’s constructor. id is initialized with parameter