Challenge: Attributes of the Class
syntax.h
1234class Name { public: Type AttributeName; };
Public attributes and methods in a class can be directly accessed using the dot operator (.) on an object of the class.
main.cpp
12345678910111213class Student { public: int age; }; int main() { Student bob; Student ann; bob.age = 25; ann.age = 33; }
Swipe to start coding
Imagine you are building a simple registration system. You need to create a User class with fields for the user's name, age, and a boolean indicating whether the user is an adult. Your task is to implement a function that creates a User object, checks if the user is at least 18 years old, and sets the boolean field accordingly.
-
Define a class
Userwith three fields:nameof typestd::stringageof typeintisAdultof typebool
-
Implement a function
registerUserthat takes anameandageas parameters:- Create a
Userobject inside the function. - Assign the passed
nameandageto the object's corresponding fields. - Initialize
isAdulttofalse. - Check the age: if
ageis 18 or older, setisAdulttotrue. - Return the created
Userobject.
- Create a
Solution
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: Attributes of the Class
Swipe to show menu
syntax.h
1234class Name { public: Type AttributeName; };
Public attributes and methods in a class can be directly accessed using the dot operator (.) on an object of the class.
main.cpp
12345678910111213class Student { public: int age; }; int main() { Student bob; Student ann; bob.age = 25; ann.age = 33; }
Swipe to start coding
Imagine you are building a simple registration system. You need to create a User class with fields for the user's name, age, and a boolean indicating whether the user is an adult. Your task is to implement a function that creates a User object, checks if the user is at least 18 years old, and sets the boolean field accordingly.
-
Define a class
Userwith three fields:nameof typestd::stringageof typeintisAdultof typebool
-
Implement a function
registerUserthat takes anameandageas parameters:- Create a
Userobject inside the function. - Assign the passed
nameandageto the object's corresponding fields. - Initialize
isAdulttofalse. - Check the age: if
ageis 18 or older, setisAdulttotrue. - Return the created
Userobject.
- Create a
Solution
Thanks for your feedback!
single