Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Constructor and Attributes | Constructors and Destructors
C++ OOP

Swipe to show menu

book
Constructor and Attributes

One of the primary usage of the constructors is to initialize attributes of the class. A default constructor, for example, can be used to set initial values. For example:

cpp

main

copy
1234567891011121314
#include <iostream> class Person { public: Person() { name = "undefined"; } std::string name; }; int main() { Person person; std::cout << person.name; }

If you don't specify value for name attribute of the object, it will be set to underfined as a default. You can try removing this constructor to see what changes occur.

Initializing Attributes with Constructor

Like functions, constructors can take parameters, allowing different arguments during object creation. You can also overload them to handle varying numbers of arguments.

cpp

main

copy
123456789101112131415161718
class Person { public: Person(std::string _name, std::string _surname, int _age) { name = _name; surname = _surname; age = _age; } std::string name; std::string surname; int age; }; int main() { Person person("Bob", "Song", 23); }
Task

Swipe to start coding

You are working with a Location class and need to initialize its attributes using a constructor.

  • Create a constructor that takes three parameters and assigns them to the instance variables.
  • Create an object of the Location class using the constructor.
  • Output the initialized attributes of the object to the console.

Solution

cpp

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Constructor and Attributes

One of the primary usage of the constructors is to initialize attributes of the class. A default constructor, for example, can be used to set initial values. For example:

cpp

main

copy
1234567891011121314
#include <iostream> class Person { public: Person() { name = "undefined"; } std::string name; }; int main() { Person person; std::cout << person.name; }

If you don't specify value for name attribute of the object, it will be set to underfined as a default. You can try removing this constructor to see what changes occur.

Initializing Attributes with Constructor

Like functions, constructors can take parameters, allowing different arguments during object creation. You can also overload them to handle varying numbers of arguments.

cpp

main

copy
123456789101112131415161718
class Person { public: Person(std::string _name, std::string _surname, int _age) { name = _name; surname = _surname; age = _age; } std::string name; std::string surname; int age; }; int main() { Person person("Bob", "Song", 23); }
Task

Swipe to start coding

You are working with a Location class and need to initialize its attributes using a constructor.

  • Create a constructor that takes three parameters and assigns them to the instance variables.
  • Create an object of the Location class using the constructor.
  • Output the initialized attributes of the object to the console.

Solution

cpp

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt