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

Edited by nmunemo

2 Years
Discussion Span
Last Post by Sky Diploma

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

   salesheader h;
   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 :).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.