Constructors and Overloading...

Constructors and Overloading

Page 1 of 1

5 Replies - 1242 Views - Last Post: 12 December 2009 - 03:37 PM Rate Topic: -----

#1 jmlowrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-December 09

Constructors and Overloading...

Posted 12 December 2009 - 03:43 AM

So I have created a C++ program and its finished and functional, but there are a few things I have yet to add to it.

My questions deal with two things: overloading and constructors.

I am not familiar with either of them, but according to my homework assignment I have to implement them. Now, I have read through my book but it vaguely explains these things, so it must be noted that not only do I not know how to implement them in terms of code, but I am not even sure what they do or where to begin.

This program is our instructors way of helping us learn to define classes and use objects, use accessors, mutators, and constructors. Well I understand and have completed all of that aside from constructors.

An explanation of the program: It accepts as input a customers name, street address, zip code, cookie type ordered, and how many cookies where ordered, and on what date they were ordered. It then proves to my teacher that I know how to use accessors by displaying the year the cookies were ordered in main(), since the year was originally a private variable. Also, it invokes a manipulator by asking the user if they would like to change the customer name. Our instructor just wanted us to do these 2 random things to prove we understand how to code and use accessors/mutators. The program then outputs via an output method a report containing all of the information entered by the user. It does not need to loop; it only needs to run once.

Anyhow, to the problem. The instructions say to invoke a default constructor for each attribute (customer name, street address, etc) and also a constructor that accepts arguments for each of those attributes.

I have no idea what this means or what it is used for...

Additionally, he wants us to overload the input and output stream operators so that the objects may be used in standard input and output statements (which i assume he means "cin" and "cout")

I also have no idea what that means or what it is used for...

Any help would be appreciated, but I would also like an explanation of what exactly a constructor does and what the advantages are overloading are, for educational purposes.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class CookieOrder //This is the class i defined
{
public:
void input();
void output();

string get_name();//accessor

void set_name(string new_name);//mutator

string get_address();

void set_address(string new_address);

string get_city();

void set_city(string new_city);

string get_state();

void set_state(string new_state);

int get_zip();

void set_zip(int new_zip);

int get_month();

void set_month(int new_month);

int get_day();

void set_day(int new_day);

int get_year();

void set_year(int new_year);

int get_cookieType();

void set_cookieType(int new_cookieType);

int get_numCookies();

void set_numCookies(int new_numCookies);

private:
string name;
string address;
string city;
string state;
int zip;
int month;
int day;
int year;
int cookieType;
int numCookies;

};

int main()
{
int year;
char choice;
string newCustName;
CookieOrder entry;
cout << "Please enter the customers first and last name.\n";
entry.input();
cout << "The final report for this customer is as follows: \n\n";
entry.output();
cout << "These cookies were ordered in ";
year = entry.get_year();//demonstrating accessor usage
cout << year << endl;
cout << "Would you like to edit the customer name? Enter 'Y' or 'N'.\n";
cin >> choice;
if ((choice == 'Y') || (choice == 'y'))//demonstrating mutator usage
{
cout << "Please enter the new customer name: \n";
getline(cin, newCustName);
cin.ignore(1000, '\n');
entry.set_name(newCustName);
cout << "New customer name saved!\n";
}

cout << "Thank you for using this program! Good-Bye!\n";

return 0;
}

void CookieOrder::input()
{

cout << "Please put a space between them: \n";
getline(cin, name);
cout << "Please enter the customer's street address: \n";
getline(cin, address);
cout << "Please enter the city the customer lives in: \n";
getline(cin, city);
cout << "Please enter the state the customer lives in: \n";
getline(cin, state);
cout << "Please enter the customer's zip code: \n";
cin >> zip;
cout << "Enter the date the order was placed by first entering the numerical form of this month\n";
cout << "(January = 1, February = 2, ...)\n";
cin >> month;
cout << "Input the day of the month the order was placed: \n";
cin >> day;
cout << "Enter the year during which the order was placed: \n";
cin >> year;
cout << "Enter the number that corresponds to the cookie type ordered.\n"
<< "For a reminder, see the chart below.\n\n"
<< "Chocolate Chip" << setw(5) << "1" << endl//my bootleg form of right-aligning...lol
<< "Peanut Butter" << setw(6) << "2" << endl
<< "Sugar" << setw(14) << "3" << endl
<< "Pecan" << setw(14) << "4" << endl
<< "Candy" << setw(14) << "5" << endl;
cin >> cookieType;
cout << "Finally, enter the number of cookies that were ordered: \n";
cin >> numCookies;
cin.ignore(1000, '\n');
}

void CookieOrder::output()
{
cout << name << endl;
cout << address << endl;
cout << city << " " << state << ", " << zip << endl;
cout << "Order Date: " << month << "/" << day << "/" << year << endl;
switch (cookieType)
{
case 1:

cout << "Cookie type: Chocolate Chip" << endl;
break;

case 2:

cout << "Cookie Type: Peanut Butter" << endl;
break;

case 3:

cout << "Cookie Type: Sugar" << endl;
break;

case 4:

cout << "Cookie Type: Pecan" << endl;
break;

case 5:

cout << "Cookie Type: Candy" << endl;
break;
}
cout << "Quantity: " << numCookies << endl;

}

string CookieOrder::get_name()
{
return name;
}

void CookieOrder::set_name(string new_name)
{
name = new_name;
}

string CookieOrder::get_address()
{
return address;
}

void CookieOrder::set_address(string new_address)
{
address = new_address;
}

string CookieOrder::get_city()
{
return city;
}

void CookieOrder::set_city(string new_city)
{
city = new_city;
}

string CookieOrder::get_state()
{
return state;
}

void CookieOrder::set_state(string new_state)
{
state = new_state;
}

int CookieOrder::get_zip()
{
return zip;
}

void CookieOrder::set_zip(int new_zip)
{
zip = new_zip;
}

int CookieOrder::get_month()
{
return month;
}

void CookieOrder::set_month(int new_month)
{
month = new_month;
}

int CookieOrder::get_day()
{
return day;
}

void CookieOrder::set_day(int new_day)
{
day = new_day;
}

int CookieOrder::get_year()
{
return year;
}

void CookieOrder::set_year(int new_year)
{
year = new_year;
}

int CookieOrder::get_cookieType()
{
return cookieType;
}

void CookieOrder::set_cookieType(int new_cookieType)
{
cookieType = new_cookieType;
}

int CookieOrder::get_numCookies()
{
return numCookies;
}

void CookieOrder::set_numCookies(int new_numCookies)
{
numCookies = new_numCookies;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Constructors and Overloading...

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Constructors and Overloading...

Posted 12 December 2009 - 03:50 AM

Have a read here
http://www.cplusplus...torial/classes/
and here
http://www.cplusplus...orial/classes2/
Was This Post Helpful? 0
  • +
  • -

#3 fushar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 12-December 09

Re: Constructors and Overloading...

Posted 12 December 2009 - 08:07 AM

Tips: why not ask the teacher for more thorough details? :D

A constructor is called whenever an instance of a class is just created. A default constructor is a constructor with no arguments. When not defined by programmer, it does nothing. Your teacher ask for defining this, so let's define.

CookieOrder() // must have the same name as the class and have no return value.
{
	// initialize the default value of all (or some) attributes here.
}



A constructor can also accept arguments. Usually the arguments are the values of the desired attributes. Just add the parameters to the constructor definition. Initialize the attributes with the arguments.

We can overload stream operator, so that it can behave like input/output streams. The syntax is

friend ostream& operator<<(ostream& out, CookieOrder& o)
{
	// It's the output operator. Use it to print values to a stream. Maybe like this.
	
	out << o.name << '\n';
	return out;
}

friend istream& operator<<(istream& in, CookieOrder& o)
{
	// It's the input operator. Use it to get values from a stream. Maybe like this.
	
	in >> o.name;
	return in;
}



What are those for? With those overloadings, we can do input/output with this class more naturally, like this.
CookieOrder o;
cin >> o; // obtains the name for o.
cout << o; // prints the name of o.



Note: all functions above must be coded inside the CookieOrder class!
Hope these will help.

This post has been edited by fushar: 12 December 2009 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 jmlowrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-December 09

Re: Constructors and Overloading...

Posted 12 December 2009 - 09:10 AM

Wow... I pretty much have no more questions. You guys really cleared this up for me, thanks!
Was This Post Helpful? 0
  • +
  • -

#5 jmlowrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-December 09

Re: Constructors and Overloading...

Posted 12 December 2009 - 10:39 AM

Well actually, I am confused again. How exactly do I incorporate these bits of code...

Would i place the default constructor just before the }; at the end of my class as CookieOrder() (or should it be CookieOrder::CookieOrder

And how would it know when to use default values? like if the user presses enter to continue rather than enetering the address, how would i call the constructor from within my input() ,ethod so it would return something like "Undefined" for address?

Also, where would i place that bit of code you gave me for overloading?
Was This Post Helpful? 0
  • +
  • -

#6 fushar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 12-December 09

Re: Constructors and Overloading...

Posted 12 December 2009 - 03:37 PM

You can declare and define the constructor inside the class, like
class CookieOrder
{
public:
	CookieOrder()
	{
		// do something.
	}
};



or separate the definition outside.
class CookieOrder
{
public:
	CookieOrder();
};
CookieOrder::CookieOrder()
{
}



It's just like other member functions.

About the default value, maybe this link will help.

The placement of the overloading operators is the same as the constructors.

This post has been edited by fushar: 12 December 2009 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1