The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and displays them and then creates a List of Dates and displays them. From this, you can see how a container class such as List can be made into a template that can store any data type.
Main Program:
#include <iostream>
#include <conio.h>
using namespace std;
class List //contains a series of integers using dynamic memory with flexible size,
known as a container class
{
private:
int size; //number of elements
int *ptr; //points to first element
public:
List(int size = 0, int* ptr = 0); //default size is 0 List(const List& l);
List(const List& l); //copy constructor - used to create and at once
initialize a List object
int get_size() const;
const List& operator=(const List& l); //overloaded assignment operator
int& operator[](int index); //returns a reference to int so it can be
assigned to(used as lvalue)
const int operator[](int index) const; //returns list member at specified
index(a rvalue)
~List(); //destructor frees allocated dynamic memory
};
class Date {
friend ostream& operator<<(ostream& out, const Date& date); //a non-member
function is used to display because
friend istream& operator>>(istream& in, Date& date); //a non-member function
is used to display because
private:
int day, month, year;
public:
Date(int = 1, int = 1, int = 1900);
void set(int = 1, int = 1, int = 1900);
void get();
void print() const;
bool operator==(const Date &) const;
};
Date::Date(int d, int m, int y)
{
set(d, m, y);
}
void Date::set(int d, int m, int y)
{
if (d >= 1 && d <= 31)
day = d;
else
{
std::cout << "\ninvalid day: " << d;
std::cout << "\nProgram ending. . .";
system("pause");
exit(1);
}
if (m >= 1 && m <= 12)
month = m;
else
{
std::cout << "\ninvalid month: " << m;
std::cout << "\nProgram ending. . .";
system("pause");
exit(1);
}
if (y >= 1900 && y <= 3000)
year = y;
else
{
std::cout << "\ninvalid year: " << y;
std::cout << "\nProgram ending. . .";
system("pause");
exit(1);
}
}
void Date::get()
{
char ch;
std::cout << "Enter date in month/day/year format: ";
cin >> month >> ch >> day >> ch >> year;
}
ostream& operator<<(ostream& out, const Date& date) //a non-member function is used
to display because
{
out << date.month << '/' << date.day << '/' << date.year;
return out;
}
istream& operator>>(istream& in, Date& date) //a non-member function is used to
display because
{
in >> date.month >> date.day >> date.year;
return in;
}
bool Date::operator==(const Date &d) const
{
return day == d.day && month == d.month && year == d.year;
}
List::List(int s, int* ptr) : size(s), ptr(new int[s])
{
} //initialization being done by the member initializer; alternatively we could
write them in the body like in set_size
List::List(const List& l) : size(l.size) //copy constructor used to initialize a
List object using an existing List object
{ //this creates the new object with l.size elements
ptr = new int[size];
for (int i = 0; i < size; i++) //now copy elements
ptr[i] = l.ptr[i];
}
int List::get_size() const
{
return size;
}
int& List::operator[](int index) //returns a reference to object so it can be
assigned to
{
return ptr[index];
}
const int List::operator[](int index) const //returns list member at specified
index
{
return ptr[index];
}
List::~List()
{
delete[] ptr;
}
int main()
{
List<int>numbers(10);
for (int i = 0; i < 10; i++)
{
numbers[i] = i + 1;
cout << numbers[i] << ' ';
}
List<Date>dates(3);
dates[0].set(1, 12, 2016);
dates[1].set(2, 12, 2016);
dates[2].set(3, 12, 2016);
for (int i = 0; i < 3; i++)
cout << endl << dates[i];
cout << "\nPress any key to continue.";
_getch();
}
If you have any doubts, please give me comment...
#include <iostream>
#include <cstdlib>
// #include <conio.h>
using namespace std;
template<class T>
class List //contains a series of integers using dynamic memory with flexible size, known as a container class
{
private:
int size; //number of elements
T *ptr; //points to first element
public:
List(int size = 0, T *ptr = 0); //default size is 0 List(const List& l);
List(const List &l); //copy constructor - used to create and at once initialize a List object
int get_size() const;
const List &operator=(const List<T> &l); //overloaded assignment operator
T &operator[](int index); //returns a reference to int so it can be assigned to(used as lvalue)
const T operator[](int index) const; //returns list member at specified index(a rvalue)
~List<T>(); //destructor frees allocated dynamic memory
};
class Date
{
friend ostream &operator<<(ostream &out, const Date &date); //a non-member function is used to display because
friend istream &operator>>(istream &in, Date &date); //a non-member function is used to display because
private :
int day,
month,
year;
public:
Date(int = 1, int = 1, int = 1900);
void set(int = 1, int = 1, int = 1900);
void get();
void print() const;
bool operator==(const Date &) const;
};
Date::Date(int d, int m, int y)
{
set(d, m, y);
}
void Date::set(int d, int m, int y)
{
if (d >= 1 && d <= 31)
day = d;
else
{
std::cout << "\ninvalid day: " << d;
std::cout << "\nProgram ending. . .";
system("pause");
exit(1);
}
if (m >= 1 && m <= 12)
month = m;
else
{
std::cout << "\ninvalid month: " << m;
std::cout << "\nProgram ending. . .";
system("pause");
exit(1);
}
if (y >= 1900 && y <= 3000)
year = y;
else
{
std::cout << "\ninvalid year: " << y;
std::cout << "\nProgram ending. . .";
system("pause");
exit(1);
}
}
void Date::get()
{
char ch;
std::cout << "Enter date in month/day/year format: ";
cin >> month >> ch >> day >> ch >> year;
}
ostream &operator<<(ostream &out, const Date &date) //a non-member function is used to display because
{
out << date.month << '/' << date.day << '/' << date.year;
return out;
}
istream &operator>>(istream &in, Date &date) //a non-member function is used to display because
{
in >> date.month >> date.day >> date.year;
return in;
}
bool Date::operator==(const Date &d) const
{
return day == d.day && month == d.month && year == d.year;
}
template<class T>
List<T>::List(int s, T *ptr) : size(s), ptr(new T[s])
{
} //initialization being done by the member initializer; alternatively we could write them in the body like in set_size
template<class T>
List<T>::List(const List<T> &l) : size(l.size) //copy constructor used to initialize a List object using an existing List object
{ //this creates the new object with l.size elements
ptr = new T[size];
for (int i = 0; i < size; i++) //now copy elements
ptr[i] = l.ptr[i];
}
template<class T>
int List<T>::get_size() const
{
return size;
}
template<class T>
T &List<T>::operator[](int index) //returns a reference to object so it can be assigned to
{
return ptr[index];
}
template<class T>
const T List<T>::operator[](int index) const //returns list member at specified index
{
return ptr[index];
}
template<class T>
List<T>::~List<T>()
{
delete[] ptr;
}
int main()
{
List<int> numbers(10);
for (int i = 0; i < 10; i++)
{
numbers[i] = i + 1;
cout << numbers[i] << ' ';
}
List<Date> dates(3);
dates[0].set(1, 12, 2016);
dates[1].set(2, 12, 2016);
dates[2].set(3, 12, 2016);
for (int i = 0; i < 3; i++)
cout << endl << dates[i];
cout << "\nPress any key to continue.";
// _getch();
}
The following program contains the definition of a class called List, a class called Date and...
This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...
A library maintains a collection of books. Books can be added to
and deleted from and checked out and checked in to this
collection.
Title and author name identify a book. Each book object
maintains a count of the number of copies available and the number
of copies checked out. The number of copies must always be greater
than or equal to zero. If the number of copies for a book goes to
zero, it must be deleted from the...
In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...
The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...
6. Suppose your program contains the following class definition. Please indicate whether the statement (a)-(e) are valid. If any one is invalid, please explain why include <iostream» using atd:icout: uning stdrtendli class Auto publier void setPrice (doubleli void setProfit (doublel double getPrice) private t double price, profiti double getProfitO // The definitions of member functions are omitted. int main() Auto honda, toyota: double a, b 0 honda. setPrice (19999.99): 17 (a) toyota.price 19999.99 b) a toyota. setProfit (10.0) // (c)...
Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int getDay() const; // add any member you need here }; You implementation should enable the usage like this: void f() { Date date = d; cout << "date = " << date << endl; cout << "date+1 = " << date+1 << endl; cout << "date-1 = "...
Please zoom in so the pictures become high resolution. I need
three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The
programming language is C++. I will provide the Sample Test Driver
Code as well as the codes given so far in text below.
Sample Testing Driver Code:
cs52::FlashDrive empty;
cs52::FlashDrive drive1(10, 0, false);
cs52::FlashDrive drive2(20, 0, false);
drive1.plugIn();
drive1.formatDrive();
drive1.writeData(5);
drive1.pullOut();
drive2.plugIn();
drive2.formatDrive();
drive2.writeData(2);
drive2.pullOut();
cs52::FlashDrive combined = drive1 + drive2;
// std::cout << "this drive's filled to " << combined.getUsed( )...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...