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

Conteúdo do Curso

C++ OOP

C++ OOP

1. Fundamentals of OOP
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

bookConstructor 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
12345678910111213141516
#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

Just as functions a constructor can accept parameters, allowing you to pass different arguments when instantiating an object. Also, constructor can be overrided, so you can increase flexibility, for instance varying number of arguments.

cpp

main

copy
123456789101112131415
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); }
Tarefa
test

Swipe to show code editor

  • Create a constructor for the Location class that takes three parameters and initializes the instance variables with these values.
  • Output initialized attributes of the object to the console.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
toggle bottom row

bookConstructor 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
12345678910111213141516
#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

Just as functions a constructor can accept parameters, allowing you to pass different arguments when instantiating an object. Also, constructor can be overrided, so you can increase flexibility, for instance varying number of arguments.

cpp

main

copy
123456789101112131415
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); }
Tarefa
test

Swipe to show code editor

  • Create a constructor for the Location class that takes three parameters and initializes the instance variables with these values.
  • Output initialized attributes of the object to the console.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
toggle bottom row

bookConstructor 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
12345678910111213141516
#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

Just as functions a constructor can accept parameters, allowing you to pass different arguments when instantiating an object. Also, constructor can be overrided, so you can increase flexibility, for instance varying number of arguments.

cpp

main

copy
123456789101112131415
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); }
Tarefa
test

Swipe to show code editor

  • Create a constructor for the Location class that takes three parameters and initializes the instance variables with these values.
  • Output initialized attributes of the object to the console.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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
12345678910111213141516
#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

Just as functions a constructor can accept parameters, allowing you to pass different arguments when instantiating an object. Also, constructor can be overrided, so you can increase flexibility, for instance varying number of arguments.

cpp

main

copy
123456789101112131415
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); }
Tarefa
test

Swipe to show code editor

  • Create a constructor for the Location class that takes three parameters and initializes the instance variables with these values.
  • Output initialized attributes of the object to the console.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt