1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <string>
struct kontakt
{
// construct kontact from the given information
kontakt( std::string first_name, std::string last_name, std::string phone_number )
: fname(first_name), lname(last_name), phone(phone_number) // name( std::move(first_name) ) etc.
{ /* TO DO: validate. eg. both first name and last name can't be empty strings */ }
std::string first_name() const { return fname ; }
std::string last_name() const { return lname ; }
std::string phone_number() const { return phone ; }
std::string full_name() const { return last_name() + ", " + first_name() ; }
// return the full name and phone number as a single string
std::string to_string() const { return "{ name: '" + full_name() + "' phone: " + phone + " }"; }
private:
std::string fname ;
std::string lname ;
std::string phone ;
};
int main()
{
// get the first name from the user
std::string fname ;
std::cout << "first name: " ;
std::cin >> fname ;
// or if the first name may contain spaces: std::getline( std::cin, fname ) ;
// get the last name from the user
std::string lname ;
std::cout << "last name: " ;
std::cin >> lname ; // std::getline( std::cin, lname ) ;
// get the phone number name from the user
std::string phone ;
std::cout << "phone number: " ;
std::cin >> phone ;
// construct kontakt with the information given by the user
kontakt my_kontakt( fname, lname, phone ) ;
// or: kontakt my_kontakt { fname, lname, phone } ;
// print out information about the kontakt
std::cout << "kontakt: " << my_kontakt.to_string() << '\n' ;
// just as an example, to set values (the constructor subsumes kontakt::set_values)
my_kontakt = { "mickey", "mouse", "123-456789" } ;
std::cout << "updated kontakt: " << my_kontakt.to_string() << '\n' ;
}
|