Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Chained Account Constructors | Constructors and Overloading
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# OOP Class Construction Drills

bookChallenge: Chained Account Constructors

Opgave

Swipe to start coding

Practice constructor chaining by building an Account class that offers flexibility in how accounts are created. Use overloaded constructors to allow for different initialization scenarios, and ensure all constructors ultimately delegate initialization to a single constructor to avoid code duplication.

  • The Account class must have three constructors: a default constructor, a constructor that takes an accountNumber, and a constructor that takes both an accountNumber and a balance.
  • The default constructor must set both AccountNumber and Balance to zero.
  • The constructor with one parameter must set AccountNumber to the provided value and Balance to zero.
  • The constructor with two parameters must set AccountNumber and Balance to the provided values.
  • All constructors must use constructor chaining so that only one constructor contains the actual assignment logic.
  • Do not duplicate assignment code across constructors.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 6
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Chained Account Constructors

Stryg for at vise menuen

Opgave

Swipe to start coding

Practice constructor chaining by building an Account class that offers flexibility in how accounts are created. Use overloaded constructors to allow for different initialization scenarios, and ensure all constructors ultimately delegate initialization to a single constructor to avoid code duplication.

  • The Account class must have three constructors: a default constructor, a constructor that takes an accountNumber, and a constructor that takes both an accountNumber and a balance.
  • The default constructor must set both AccountNumber and Balance to zero.
  • The constructor with one parameter must set AccountNumber to the provided value and Balance to zero.
  • The constructor with two parameters must set AccountNumber and Balance to the provided values.
  • All constructors must use constructor chaining so that only one constructor contains the actual assignment logic.
  • Do not duplicate assignment code across constructors.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 6
single

single

some-alt