Challenge: 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
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
123456789101112131415161718class 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); }
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.
- Implement a constructor that initializes all three fields:
- The constructor should take three parameters: a
std::string
for the name, and twodouble
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.
- The constructor should take three parameters: a
- Implement a method
distanceTo
** that takes anotherLocation
object as a parameter:- Access the
x
andy
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
.
- Access the
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
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.13
Challenge: 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
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
123456789101112131415161718class 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); }
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.
- Implement a constructor that initializes all three fields:
- The constructor should take three parameters: a
std::string
for the name, and twodouble
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.
- The constructor should take three parameters: a
- Implement a method
distanceTo
** that takes anotherLocation
object as a parameter:- Access the
x
andy
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
.
- Access the
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
Thanks for your feedback!
single