Learn
Classes and Objects
Constructors

Is there a way to give an object some data right when it gets created? We’re so glad you asked!

A constructor is a special kind of method that lets you decide how the objects of a class get created. It has the same name as the class and no return type. Constructors really shine when you want to instantiate an object with specific attributes.

If we want to make sure each City is created with a name and a population, we can use parameters and a member initializer list to initialize attributes to values passed in:

// city.hpp #include "city.hpp" class City { std::string name; int population; public: City(std::string new_name, int new_pop); }; // city.cpp City::City(std::string new_name, int new_pop) // members get initialized to values passed in : name(new_name), population(new_pop) {}

You could also write the definition like this:

City::City(std::string new_name, int new_pop) { name = new_name; population = new_pop; }

However, a member initialization list allows you to directly initialize each member when it gets created.

To instantiate an object with attributes, you can do:

// inside main() City ankara("Ankara", 5445000);

Now we have a City called ankara with the following attributes:

  • ankara.name set to "Ankara".
  • ankara.population set to 5445000.

Instructions

1.

Declare a public constructor for Song in song.hpp. Give it two std::string parameters:

  • new_title
  • new_artist Then define the constructor inside song.cpp. Initialize title to new_title and artist to new_artist.
2.

Hmm… a song probably only needs to be given a title and artist once. Now that you can set those attributes with a constructor, go ahead and remove .add_title() and .add_artist() from song.hpp and song.cpp.

3.

Let’s try out your new constructor!

In main(), instantiate a new Song called back_to_black. Give it:

  • a title of "Back to Black"
  • an artist of "Amy Winehouse"

(You can print out each attribute of back_to_black using .get_title() and .get_artist().)

Folder Icon

Take this course for free

Already have an account?