Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "93 4/11/98 CSE 143 Class Constructors [Sections 3.3-3.4]"— Presentation transcript:

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

2 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";

3 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

4 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 ?

5 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...

6 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; }

7 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.

8 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); }

9 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”)

10 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)

11 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.

12 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.


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

Similar presentations


Ads by Google