Lesson 2
Constructors and Class Methods in C++
Topic Overview

Welcome back, C++ enthusiast! We're diving back into C++ classes, focusing on constructors and class methods. Picture building a robot: Constructors are the initial settings, and class methods are the commands that enable it to perform actions. Are you ready for a refresher? Let's get started!

Revisiting C++ Classes

A C++ class serves as a blueprint for creating objects. Here's a basic class, Robot:

C++
1class Robot { 2 // Class body goes here 3}; 4 5int main() { 6 Robot robot_instance; // Creating an instance of Robot 7 return 0; 8}

As of now, this Robot class is like an empty shell. It exists but doesn't know how to do anything. To make it functional, attributes and methods are needed.

Deep Dive into Constructors

A constructor is a special function that initializes an object when it is created. In C++, the constructor is a method with the same name as the class. It sets up — or constructs — our new objects with the necessary initial states.

Here, we upgrade the Robot class with a constructor:

C++
1#include<iostream> 2#include<string> 3 4class Robot { 5public: 6 // Constructor 7 Robot(const std::string &name, const std::string &color) 8 : name(name), color(color) { 9 std::cout << "Constructor is called" << std::endl; 10 } 11 12private: 13 std::string name; 14 std::string color; 15}; 16 17int main() { 18 Robot robot_instance("Robbie", "red"); // Output: Constructor is called 19 return 0; 20}

In this example, the constructor gets automatically called when we create a new Robot instance, set with name and color attributes. It’s always good practice to use constructors to ensure each instance starts with the correct initial values.

Notice the syntax : name(name), color(color) in the constructor. This is known as an initializer list. It is a more efficient way to initialize members because it directly initializes the member variables instead of first creating a default object and then assigning values to it. This can lead to better performance, especially with complex member variables or classes.

Multiple Constructors with Default Parameters

Having one constructor is great, but what if we want more flexibility in setting up our robots? In C++, you can use constructor overloading or default arguments to achieve similar behavior.

Consider the following code snippet:

C++
1#include<iostream> 2#include<string> 3 4class Robot { 5public: 6 // Constructor with default parameter for color 7 Robot(const std::string &name, const std::string &color="grey") 8 : name(name), color(color) {} 9 10 // Overloaded constructor with only name, sets default color to grey 11 Robot(const std::string &name) 12 : name(name), color("grey") {} 13 14private: 15 std::string name; 16 std::string color; 17}; 18 19int main() { 20 Robot robot_instance("Robbie", "red"); // Red Robbie 21 Robot robot_instance2("Bobby"); // Grey Bobby, no color provided (default) 22 return 0; 23}

The first constructor defines a default color using default parameters. In other words, with default parameters color becomes optional: when we don't specify it, the robot is grey by default.

Let's now discuss constructor overloading. Overloading allows us to define multiple constructors with different parameters. In the example above, we have two constructors: one that takes name and color, and another that takes only name and sets color to a default value. This provides flexibility in initializing our Robot instances, and now there are multiple ways to create a robot.

Class Methods

Class methods are akin to commands controlling the robot's actions. They provide additional behaviors for our objects.

This Robot class allows the robots to introduce themselves:

C++
1#include<iostream> 2#include<string> 3 4class Robot { 5public: 6 Robot(const std::string &name, const std::string &color="grey") 7 : name(name), color(color) {} 8 9 void say_hello() const { 10 std::cout << "Hello, I am " << name << " and I am " << color << "." << std::endl; 11 } 12 13private: 14 std::string name; 15 std::string color; 16}; 17 18int main() { 19 Robot robot_instance("Robbie", "red"); 20 robot_instance.say_hello(); // Output: "Hello, I am Robbie and I am red." 21 return 0; 22}

The say_hello method allows our robot instance to interact and even communicate.

Lesson Summary

Great job! You've refreshed and deepened your understanding of C++ classes, constructors, and class methods and learned how to mimic multiple constructors. Now, you can breathe more life into your C++ classes, making them more versatile and powerful. Next, we'll move on to the hands-on tasks, where you'll apply these concepts to more complex scenarios. Keep up the good work!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.