How to creating function with given code in C++? -
giving function creatcustomer() create customer. prototype:
customer*creatcustomer(const string&name, const string&id, const string&pin)
and given code below.the structure done myself.
the question how create function using given code , prototype.
#include <iostream> #include <iomanip> #include <string> using namespace std; struct customer { string customername; string userid; string pin; }; int main() { customer* mary = createcustomer("mary jones", "235718", "5074"); customer* john = createcustomer("john smith", "375864", "3251"); }
first of in case don't need function, can do:
customer mary { "mary jones", "235718", "5074" }; customer john { "john smith", "375864", "3251" };
but if need to, should use constructor:
struct customer { std::string customername; std::string userid; std::string pin; customer(std::string a, std::string b, std::string c) : customername(a) , userid(b) , pin(c) {} };
the reason i'm not telling how directly because pointer in return type suggests me told use dynamic memory allocation inside function , return pointer it. that's awfully bad idea.
but since foot yours , i'm not here babysit shotgun, here's how shoot:
customer* createcustomer(const string&name, const string&id, const string&pin) { return new customer { name, id, pin }; }
Comments
Post a Comment