120 likes | 138 Vues
Understand class initialization, constructors, and guidelines in C++. Learn how to initialize class instances and the importance of constructors with examples and best practices.
E N D
CSE 143 Class Constructors [Sections 3.3-3.4]
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";
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
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?
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...
A Better Bank Account • // in BankAccount.h • class BankAccount { • public: • BankAccount(); • void deposit(double amount); • . . . • }; • // in BankAccount.cpp • BankAccount::BankAccount() { • balance = 0.0; • }
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.
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); • }
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”)
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)
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.
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.