1 / 12

C++ Classes and Constructors: Initialization Demystified

Understand class initialization, constructors, and guidelines in C++. Learn how to initialize class instances and the importance of constructors with examples and best practices.

russelp
Télécharger la présentation

C++ Classes and Constructors: Initialization Demystified

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 143 Class Constructors [Sections 3.3-3.4]

  2. 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. 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. 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. 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. A Better Bank Account • // in BankAccount.h • class BankAccount { • public: • BankAccount(); • void deposit(double amount); • . . . • }; • // in BankAccount.cpp • BankAccount::BankAccount() { • balance = 0.0; • }

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

More Related