Help me with class pls

I would instead of having to set name,lastname and phone in the code itself like to set it in the console and then print it, thanks.

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
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

class kontakt
{
public:
	void set_values(string, string, string);
	string returnname() { return name; }
	string returnlastname() { return lastname; }
	string returnphone() { return phone; }
private:
	string name, lastname, phone;

};

void kontakt::set_values(string x, string y, string z)
{
	name = x;
	lastname = y;
	phone = z;
}


int main()
{
	kontakt kontakt;
	kontakt.set_values("sven", "svensson", "070-0000000");
	cout << "Name: " << kontakt.returnname() << endl;
	cout << "Lastname: " << kontakt.returnlastname() << endl;
	cout << "Phonenumber: " << kontakt.returnphone() << endl;

	return 0;
}
Last edited on
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' ;
}
Topic archived. No new replies allowed.