create a structure called sales with:
salesid,
custmer name,
customer address
using constructor to initialize.

Recommended Answers

All 4 Replies

Ah, Could you also post your answer here? :D

Structures are like classes, but where all of the member variables (salesid, customer_name, etc) are public. As Sky said, post your answer here - we don't do your homework for you.

include <iostream>
include <string>

using namespace std;
struct salesheader

#include <iostream>
#include <string>
using namespace std;
struct salesheader
{
string salesId,customerName,customerAddress;
salesheader(): salesId(0),customerName(0),customerAddress(0){}
salesheader (string salesId,string customerName,string customerAddress): salesId(), customerName(),customerAddress(){}
};
class sales
{

   public:
   salesheader h;
   sales():h(nocky,msu,gweru}
   salesheader get_values()
   {
       return h;
   };
  int main()
{
    struct salesheader salesheader1;
    struct salesheader salesheader2;
    struct salesheader salesheader3;
    cout << "salesId: 1219" <<salesheader1.salesId<<endl;
    cout << "customerName: msu"  <<salesheader2.customerName<<endl;
    cout << "customerAddress: gweru"  <<salesheader3.customerAddress<<endl;
    return 0;
}

You're close to your answer.

Lets say I have a structure X. With members 'a' and 'b' And I need a constructor for it.

struct X {
    // Declaring members of the class. 
    string a,b;

    // Defining a constructor. 
    X(string a_val, string b_val): a(a_val), b(b_val){}
};

Now if I pass strings to the structure in a constructor, they will be assigned that value.

struct X object("Hello", "World");
// Now The above line creates a structure called object, 

cout << object.a << endl; // This line would return 'Hello'.
cout << object.b << endl; // This would return 'World'.

You should now look at the similarities in this code to the one's you've submitted. And possibly try coming up with a solution. Do post back your code and ask more if needed :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.