Answer the question
In order to leave comments, you need to log in
C++ class templates how to complete the task?
1. Describe the ATD class template: "Publication print queue member: last name, phone number, number of pages "
2. Define and implement constructors, destructors, Input (keyboard input) and Print (screen output) functions in the class template, overload the operation assignments .
3. Provide for the generation and handling of two exceptions for possible errors. Situations that may lead to the generation of exceptions are simulated independently.
4. Show the use of the created class, including situations that lead to the generation of exceptions. Show template instantiation for int, double types.
It is not clear how to overload the assignment operation, and how to substitute int and double into the template if there should be a string.
And in general, I didn’t understand much about OOP, I ask for help in correcting this program. What is written:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
template <class t_name, class t_tel, class t_count>
class OCHERED {
private:
t_name name;
t_tel tel;
t_count count;
public:
OCHERED () {}
OCHERED (t_name t1, t_tel t2, t_count t3) {name = t1; tel = t2; count = t3}
void Input() {
cout << "Фамилия: "; cin >> name;
cout << "Номер телефона: "; cin >> tel;
cout << "Количество страниц: "; cin >> count;
try
{
if(count <= 0) throw "Количество страниц не может быть 0 или меньше";
}
catch(const char* s)
{
cout << endl << s << endl;
Input();
}
}
void Print(){
cout << "Фамилия: " << name << endl;
cout << "Номер телефона: " << tel << endl;
cout << "Количество страниц: " << count << endl;
}
};
int main()
{
setlocale(0,""); //поддержка кирилицы
OCHERED<string,string,int> obj;
obj.Input();
obj.Print();
_getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
2) To overload the assignment operator, you need to add a description of the OCHERED& operator=(const OCHERED& obj) method with component-by-component assignment of fields to the template class, an example is in https://habrahabr.ru/post/132014/
4) In this case, it will probably be enough use int and double only in the last template parameter (integer or fractional number of pages). You can make it the only template parameter.
3) For exceptional situations, it is better to describe separate classes for different situations, throw exceptions inside the called Input() function, and catch them at the place of its call from main().
Well, for greater meaningfulness of the example, it is worth creating a list / array of tasks, entering and printing them in a loop.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question