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

bookChallenge: 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:

main.cpp

main.cpp

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.

main.cpp

main.cpp

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

Imagine you are building a simple mapping system. You need to create a Location class that represents a point on a map with a name and coordinates. Your task is to implement a constructor and a method to calculate the distance to another location.

  1. Implement a constructor that initializes all three fields:
    • The constructor should take three parameters: a std::string for the name, and two double values for the x and y coordinates.
    • Inside the constructor, assign the parameter for the name to the name field of the object.
    • Assign the parameter for the x coordinate to the x field of the object.
    • Assign the parameter for the y coordinate to the y field of the object.
  2. Implement a method distanceTo** that takes another Location object as a parameter:
    • Access the x and y coordinates of both the current object and the other object.
    • Calculate the difference in the x coordinates: other.x - x.
    • Calculate the difference in the y coordinates: other.y - y.
    • Compute the distance using the formula: sqrt(dx * dx + dy * dy).
    • Return the calculated distance as a double.

Example

Location("Home", 0.0, 0.0) and Location("Office", 3.0, 4.0) -> 5.0
Location("Park", 1.0, 2.0) and Location("School", 4.0, 6.0) -> 5.0
Location("Cafe", -1.0, -1.0) and Location("Library", 2.0, 3.0) -> 5.0

Solution

solution.cpp

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

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

close

Awesome!

Completion rate improved to 3.13

bookChallenge: Constructor and Attributes

Swipe to show menu

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:

main.cpp

main.cpp

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.

main.cpp

main.cpp

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

Imagine you are building a simple mapping system. You need to create a Location class that represents a point on a map with a name and coordinates. Your task is to implement a constructor and a method to calculate the distance to another location.

  1. Implement a constructor that initializes all three fields:
    • The constructor should take three parameters: a std::string for the name, and two double values for the x and y coordinates.
    • Inside the constructor, assign the parameter for the name to the name field of the object.
    • Assign the parameter for the x coordinate to the x field of the object.
    • Assign the parameter for the y coordinate to the y field of the object.
  2. Implement a method distanceTo** that takes another Location object as a parameter:
    • Access the x and y coordinates of both the current object and the other object.
    • Calculate the difference in the x coordinates: other.x - x.
    • Calculate the difference in the y coordinates: other.y - y.
    • Compute the distance using the formula: sqrt(dx * dx + dy * dy).
    • Return the calculated distance as a double.

Example

Location("Home", 0.0, 0.0) and Location("Office", 3.0, 4.0) -> 5.0
Location("Park", 1.0, 2.0) and Location("School", 4.0, 6.0) -> 5.0
Location("Cafe", -1.0, -1.0) and Location("Library", 2.0, 3.0) -> 5.0

Solution

solution.cpp

solution.cpp

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
single

single

some-alt