| | |
Overloading Program containing constructors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Solved Threads: 0
Purpose: Create a C++ class; use operators, overloading, member functions, friend functions, constructors and private data.
• Make the data members private. (This means that the non-member functions below will have to be friends of the Date class).
• Create a member function called ‘set’ with the same arguments, and return type void; and create a constructor with the same arguments.
Note that the member function accesses the data members of the object that called it, and a constructor sets the data members of the Date object being created.
A member function is called like ‘d.set(13,31,1999)’. A constructor is not called like a member function. It is used to create an object, which did not yet exist, either in a variable declaration, “Date d(12,31,1999);’
• Write operator++() and operator++(int) as member functions that do the same thing—change the value that is stored in the object of that called them, so that the object contains a date one day later.
In addition to doing this, they also return a value. The operator with no formal arguments is called as ‘++x’, and returns the value that is stored in the date after it is incremented. The one with a dummy int argument is called as ‘x++’, and returns the value that was stored in the date before it was incremented. Thus,
Date a(2,7,2001), b(1,1,2001), c, d;
c = a++; d = ++b;
cout << a << “,” << c << “,” << b << “,” << d;
will print “February 8, 2001, February 7, 2001, January 2, 2001, January 2, 1001”.
• Write operator==, so that ‘a==b= returns true if two dates are the same and false otherwise. It should not be a member function.
Finally, write a main function that tests all of the functions you wrote. If a function has a return value, you must demonstrate that the return value is correct. If a function is supposed to change the value stored in a variable, you must test that it did so. Be careful to test both of these properties for both operator++’s.
//Output:
Default constructor: January 1, 2000
3 argument constructor: February 27, 2000
D2++ returns February 27, 2000
Can anyone get me started with this??
• Make the data members private. (This means that the non-member functions below will have to be friends of the Date class).
• Create a member function called ‘set’ with the same arguments, and return type void; and create a constructor with the same arguments.
Note that the member function accesses the data members of the object that called it, and a constructor sets the data members of the Date object being created.
A member function is called like ‘d.set(13,31,1999)’. A constructor is not called like a member function. It is used to create an object, which did not yet exist, either in a variable declaration, “Date d(12,31,1999);’
• Write operator++() and operator++(int) as member functions that do the same thing—change the value that is stored in the object of that called them, so that the object contains a date one day later.
In addition to doing this, they also return a value. The operator with no formal arguments is called as ‘++x’, and returns the value that is stored in the date after it is incremented. The one with a dummy int argument is called as ‘x++’, and returns the value that was stored in the date before it was incremented. Thus,
Date a(2,7,2001), b(1,1,2001), c, d;
c = a++; d = ++b;
cout << a << “,” << c << “,” << b << “,” << d;
will print “February 8, 2001, February 7, 2001, January 2, 2001, January 2, 1001”.
• Write operator==, so that ‘a==b= returns true if two dates are the same and false otherwise. It should not be a member function.
Finally, write a main function that tests all of the functions you wrote. If a function has a return value, you must demonstrate that the return value is correct. If a function is supposed to change the value stored in a variable, you must test that it did so. Be careful to test both of these properties for both operator++’s.
//Output:
Default constructor: January 1, 2000
3 argument constructor: February 27, 2000
D2++ returns February 27, 2000
Can anyone get me started with this??
>>Can anyone get me started with this??
Yes
How much do you know about the assignment you were given ? Everything that's in your assignment should be covered somewhere in your textbook.
Yes
C++ Syntax (Toggle Plain Text)
int main() { // your code goes here }
How much do you know about the assignment you were given ? Everything that's in your assignment should be covered somewhere in your textbook.
Last edited by Ancient Dragon; Dec 3rd, 2007 at 2:07 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- This Pointer / Copy Constructor (C++)
- Program Involving Overloading (C++)
- Help with constructors and deonstructors (C++)
- Type casting (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ for loops
- Next Thread: MP3 Playback in Visual C++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






