93 4/11/98 CSE 143 Class Constructors [Sections 3.3-3.4]

Slides:



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

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
1 Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Programming Languages and Paradigms Object-Oriented Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Writing Classes (Chapter 4)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
ECE122 Feb. 22, Any question on Vehicle sample code?
1 Warm-Up Problem Just like with primitive data types (int, double, etc.), we can create arrays of objects. ex: bankAccount employees[100]; Problem: It’s.
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Chapter 4 Introduction to Classes, Objects, Methods and strings
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
1 CSC241: Object Oriented Programming Lecture No 02.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
1 Introduction to Object Oriented Programming Chapter 10.
Classes - Intermediate
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Constructors and Destructors
Dynamic Storage Allocation
Classes C++ representation of an object
Lecture 3 John Woodward.
Classes Object-oriented programming: Example: Bank transactions
Implementing Classes Yonglei Tao.
Constructor & Destructor
Chapter Three - Implementing Classes
Classes and Data Abstraction
Contents Introduction to Constructor Characteristics of Constructor
Chapter 6 Class Definitions and Member Functions
Constructors and destructors
Constructors and Destructors
Java Programming Language
Classes C++ representation of an object
Chapter 9 Introduction To Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
Constructors & Destructors
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

93 4/11/98 CSE 143 Class Constructors [Sections ]

94 4/11/98 Classes and Initialization  Variables declared in a function have undefined initial value int x; // What’s its value?  May be lucky and have default value of 0  Don’t count on it!  Simple types can be initialized at declaration int x = 23; char InstructorName[] = "J. Boring";

95 4/11/98 Initialization of Instances  When declaring a class, its data members are all uninitialized BankAccount a1; // what is name? balance?  C allows initialization of structs: StudentRec john = {"John", "Smith", 1978,...};  C-style inadequate for classes  Data members may be private  Members may be too complex  No guarantee that client does it right

96 4/11/98 One Solution: init function class BankAccount { public: void init(char name[], double initBalance);... } BankAccount myAccount; myAccount.init(“Bob”, 200.0);  What happens if the client doesn’t call init ?

97 4/11/98 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...

98 4/11/98 A Better Bank Account // in BankAccount.h class BankAccount { public: BankAccount(); void deposit(double amount);... }; // in BankAccount.cpp BankAccount::BankAccount() { balance = 0.0; }

99 4/11/98 Constructors w/ Arguments Q: What’s wrong with the improved bank account class? A: There is no reasonable default value for the name of the bank account.  We can declare constructors that take arguments that allow us to pass in “interesting” values for initialization.

100 4/11/98 An Even Better Bank Account class BankAccount { public: BankAccount(); BankAccount(char name[]);... }; BankAccount::BankAccount() { balance = 0.0; strcpy(owner, “”); } BankAccount::BankAccount(char name[]) { balance = 0.0; strcpy(owner, name); }

101 4/11/98 Invoking a Constructor  A constructor is never invoked using the dot notation  A constructor is invoked whenever a class instance is created: // implicit invocation of BankAccount() BankAccount a1; // implicit invokation of BankAccount(char[]) BankAccount a2(“Bob”); // explicit invokation of BankAccount(char[]) BankAccount a3 = BankAccount(“Bob”)

102 4/11/98 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)

103 4/11/98 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  Unfortunately, we sometimes call a programmer-supplied, zero-argument constructor a default constructor, as well.

104 4/11/98 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.  If no constructors are supplied by the programmer:  the compiler provides a default, which does nothing (besides allocate uninitialized memory for the object)  If a class has one or more “non-default” constructors:  then NO “default” constructor will be supplied by the compiler.