Builder Examples:

Java
Java
C#
C#
PHP
PHP
Python
Python
C++
C++
TypeScript
TypeScript
▸ Builder Quick Review

Contact Builder in C++

If you're out and about in the world, meeting new people, you'll want to hopefully have a way to keep in touch with them all. However, you think to yourself that the contacts app on your phone just isn't doing it for you, so you want to create your own contacts application. While this won't do any of the GUI work, we'll be able to get you set out in the right direction with the data that makes the magic work behind the screen!

The Contact Class

#include <iostream>
class Contact
{
   public:
       Contact(const char* name, const char* nickname, const char* address,
       const char* phoneNumber, const bool favorite)
       : name(name), nickname(nickname), address(address), 
           phoneNumber(phoneNumber), favorite(favorite)
       {
       };

       void PrintContact()
       {
           printf("Contact information for : %s\n", this->name);
           printf("\tNickname: %s\n", this->nickname);
           printf("\tAddress: %s\n", this->address);
           printf("\tPhone Number: %s\n", this->phoneNumber);
           printf("\tIs Favorite: %s\n", this->favorite? "Yes" : "No");
       }

   protected:
       const char* name;
       const char* nickname;
       const char* address;
       const char* phoneNumber;
       bool favorite;
};

The Contact Builder

class ContactBuilder
{
   public:
       ContactBuilder& SetName(const char* name) { 
           this->name = name; 
           return *this; 
       }
       ContactBuilder& SetNickname(const char* name) { 
           this->nickname = name; 
           return *this; 
       }
       ContactBuilder& SetAddress(const char* address) {
           this->address = address;
           return *this;
       }
       ContactBuilder& SetPhoneNumber(const char* phoneNumber) {
           this->phoneNumber = phoneNumber;
           return *this;
       }    
       ContactBuilder& SetAsFavorite(const bool favorite) {
           this->favorite = favorite;
           return *this;
       }

       Contact* BuildContact()
       {
           return new Contact(this->name, this->nickname, this->address,
           this->phoneNumber, this->favorite);
       }

   protected:
       const char* name;
       const char* nickname;
       const char* address;
       const char* phoneNumber;
       bool favorite;
   
};

Demo

int main()
{
   // Create the contact:
   ContactBuilder builder;
   builder.SetName("Harvey")
   .SetNickname("Harv")
   .SetAddress("445 N Gotham Pkwy")
   .SetPhoneNumber("555-454-1920")
   .SetAsFavorite(true);

   // Build:
   Contact* pContact = builder.BuildContact();

   // Get info:
   pContact->PrintContact();

   // Clean up:
   delete pContact;
}

Find any bugs in the code? let us know!