In: Computer Science
C++ Assignment 1:
Make two classes practice
For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them.
Class #1
Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc
Take your chosen class from the list such as Book. Add to your project new files called Book.h and Book.cpp and create the new class from scratch in those files. Come up with the 3 most important data members that describe the class/object you want to build and then write 2 constructors, 3 get member functions, 3 set member functions and a toString type method that will print out the value of the three variables.
Once you have created a new class, you need to adjust the main() function to allow the user to input the data members for your class (or preset with {} if you want) and create an object of that type.
Make a toString type method to then display the new object information on the screen. You can try the confusing overload function that’s in the video example, or just concatenate the values together into one string using to_string.
Please do not use the same 3 data types for any class's member variables. That is, do not make them all ' int ' or all ' string '.
Class #2 Save a date
The three variables will be day, year, month. User should be able to set and get those plus just ask for the date from a getDate function and get back something like:
"The Date object is: March 2, 2013"
Add a format parameter to method to getData so it will return a string in one of three or four formats, ie
format 0: Mar 12, 2013
format 1: 12 Mar 2013
format 2: 3-12-2013
format 3: 3/12/13
Also make sure to validate their entry to be legal, ie: days between 1 and 31, years between 1970 and 2070, etc.
Main Function
When your classes are complete, both classes/objects should be created and displayed with main().
SOLUTION -
I have solve the problem in c++ code with comments and screenshot for easy understanding :)
CODE-
SaveADate.h
#include <iostream>
#include <sstream>
using namespace std;
class SaveADate {
// Required member variables
private:
int day;
int year;
int month;
// All Required member functions
public:
// Constructor
SaveADate();
SaveADate(int d, int y, int
m);
// Setters and Getters
void setDay(int d);
void setYear(int y);
void setMonth(int m);
int getDay() const;
int getYear() const;
int getMonth() const;
// getData function
string getData(int format);
};
SaveADate.cpp
#include "SaveADate.h"
SaveADate::SaveADate() {
day = 1;
year = 1970;
month = 1;
}
SaveADate::SaveADate(int d, int y, int m) {
// Calling set functions which also make
validation
setDay(d);
setYear(y);
setMonth(m);
}
void SaveADate::setDay(int d) {
if(d >=1 && d <= 31)
day = d;
else
day = 1;
}
void SaveADate::setYear(int y) {
if(y >= 1970 && y <= 2070)
year = y;
else
year = 1970;
}
void SaveADate::setMonth(int m) {
if(m >= 1 && m <= 12)
month = m;
else
month = 1;
}
int SaveADate::getDay() const {
return day;
}
int SaveADate::getYear() const {
return year;
}
int SaveADate::getMonth() const {
return month;
}
string SaveADate::getData(int format) {
// Getting respective month
string mon = "";
if(month == 1)
mon = "Jan";
else if(month == 2)
mon = "Feb";
else if(month == 3)
mon = "Mar";
else if(month == 4)
mon = "April";
else if(month == 5)
mon = "May";
else if(month == 6)
mon = "June";
else if(month == 7)
mon = "July";
else if(month == 8)
mon = "Aug";
else if(month == 9)
mon = "Sep";
else if(month == 10)
mon = "Oct";
else if(month == 11)
mon = "Nov";
else if(month == 12)
mon = "Dec";
stringstream str;
if(format == 0)
str << mon << " "
<< day << ", " << year;
else if(format == 1)
str << day << " "
<< mon << " " << year;
else if(format == 2)
str << month << "-"
<< day << "-" << year;
else if(format == 3)
str << month << "/"
<< day << "/" << year % 1000;
return str.str();
}
main.cpp
#include "SaveADate.h"
#include <iostream>
using namespace std;
// main function
int main() {
// Creating SaveADate object
SaveADate d;
d.setDay(12);
d.setYear(2013);
d.setMonth(3);
cout << "Showing date according to all formats"
<< endl;
cout << "format 0: " << d.getData(0)
<< endl;
cout << "format 1: " << d.getData(1)
<< endl;
cout << "format 2: " << d.getData(2)
<< endl;
cout << "format 3: " << d.getData(3)
<< endl;
}
Sample Output:-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------