In C++ Extend this class to create a Class to handle the accepting of the first
ID: 3886350 • Letter: I
Question
In C++ Extend this class to create a Class to handle the accepting of the first and last name and subsequent display. Also be sure to create a Constructor and Get and Set member functions.
#include <iostream>
#include <string>
using namespace std;
class NameFunction {
public:
// function that displays a welcome message to the GradeBook user
void displayMessage(string first, string second) const {
cout << "Welcome " << first << "," << second
<< endl;
}
};
int main() {
string firstName;
string lastName;
NameFunction myName;
cout << "Please enter first name:" << endl;
getline(cin, firstName);
cout << "Please enter last name:" << endl;
getline(cin, lastName);
cout << endl;
myName.displayMessage(firstName, lastName);
}
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
class NameFunction
{
private:
string firstName;
string lastName;
public:
// Constructor
NameFunction(){
GetName();
DisplayName();
}
void GetName()
{
cout << "Enter firstName and lastName respectively: ";
cin >> firstName >> lastName;
}
void DisplayName()
{
cout << "Full Name is: " << firstName <<" "<<lastName;
}
};
int main()
{
NameFunction N1;
return 0;
}