Question

In: Computer Science

C++ Assignment 1: Make two classes practice For each of the two classes you will create...

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().

Solutions

Expert Solution

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!!!!!!!!----------


Related Solutions

LAUNGEG: C# Objective: For this assignment you will be creating two classes, an interface, and a...
LAUNGEG: C# Objective: For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods for add( ), subtract( ), multiply( ) and divide( ). Both classes will implement the CalcOps interface. o Class Calculator’s add, subtract, multiply and divide will take...
Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with...
Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with a few classes. Requirements: Automated Teller Machine (ATM) simulationGiven 3 trials, the user is able to see his balance by entering a four-digit pin that must NEVER be displayed on screen but masked by the Asterix (*) character. A list of pins stored on the file system must be loaded to verify the pin. The user should be able to withdrawfundsbelow a set limit...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
For this assignment you will create a set of simple classes that model a cookbook. The...
For this assignment you will create a set of simple classes that model a cookbook. The cookbook will consist of set or recipes. Each recipe will consist of a set of ingredients and instructions. Your submission will consist of the below three classes and a test driver (the only class with a main()) that exercises the functionality of your system. You will need to implement the following classes based on the description of their attributes and operations: Ingredient name -...
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1....
C++ ONLY -- PRACTICE ASSIGNMENT We are given a code (Driver.cpp) and asked to create a...
C++ ONLY -- PRACTICE ASSIGNMENT We are given a code (Driver.cpp) and asked to create a file (StringStack.h) and turn it in. What should the code for StringStack.h look like based on the instructions below? Download Driver.cpp Create a class named StringStack in a file named StringStack.h. Create a ListNode structure as a private member of the class. The node should be able to hold a string called value.   Create a top pointer as private attributes of the class. (this...
For this assignment, you will create a hierarchy of five classes to describe various elements of...
For this assignment, you will create a hierarchy of five classes to describe various elements of a school setting. The classes you will write are: Person, Student,Teacher, HighSchoolStudent, and School. Detailed below are the requirements for the variables and methods of each class. You may need to add a few additional variables and/or methods; figuring out what is needed is part of your task with this assignment. Person Variables: String firstName - Holds the person's first name String lastName -...
Using C++. For this assignment you will design a set of classes that work together to...
Using C++. For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. The classes you should design are : The ParkedCar class: This class should simulate a parked car. The class's responsibilities are: -to know the car's make, model,color ,license number,and the number of minutes that the car has been parked The ParkingMeter Class: This class should simulate a parking meter. the class's only responsibility is: -To know...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Assignment Examine the Main and Address classes. You are going to add two classes derived from...
Assignment Examine the Main and Address classes. You are going to add two classes derived from Address: BusinessAddress and PersonAddress. Create BusinessAddress class Select package home and create a new Java class called BusinessAddress Make the class extend the Address class Add two private String fields businessName and address2 Generate constructor and all getters and setters Add a printLabel() method The printLabel method should print (using System.out.println()) First line – the businessName field Second line – the address2 field if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT