Navigate Editor Product Debug Source Control Window Help Ready | Today at 4:44 P
ID: 3752271 • Letter: N
Question
Navigate Editor Product Debug Source Control Window Help Ready | Today at 4:44 PM Lab2-3 Structures VoelkerFrank)9. Exercise-Type Properties and Methods Imagine you have an app that requires the user to log in. You may have a User struct similar to that shown below. However, in addition to keeping track of specific user information, you might want to have a way of knowing who the current logged in user is. Create a currentUser type property on the User struct below and assign it to a user object representing you. Now you can access the current user through the User struct. Print out the properties of currentUser. 6 struct User t var userName: String 8var email: String var age: Int There are other properties and actions associated with a User struct that might be good candidates for a type property or method. One might be a method for logging in. Go back and create a type method called LogIn(user:) where user is of type User. In the body of the method, assign the passed in user to the currentUser property, and print outa statement using the user's userName saying that the user has logged in. Below, call the LogIn(user:) method and pass in a different User instance than what you assigned to currentUser above. Observe the printout in the console.Explanation / Answer
Since you have not specified the language of your preference, I am providing the code in C++.
CODE
=================
#include <iostream>
using namespace std;
struct User {
string userName;
string email;
int age;
struct User *currentUser;
} user;
void login (struct User *new_user) {
user.currentUser = new_user;
cout << user.currentUser->userName << " has logged in!!";
}
void printCurrentUser() {
cout << "Details of current user : " << endl;
cout << "Username : " << user.currentUser->userName << endl;
cout << "Email : " << user.currentUser->email << endl;
cout << "Age : " << user.currentUser->age << endl;
}
int main() {
user = {"John_1995", "john.1995@email.com", 35};
struct User *new_user;
new_user->userName = "@max123";
new_user->email = "max@email.com";
new_user->age = 24;
login(new_user);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.