У меня проблемы с моим конструктором перегрузки в классе NewsArticle. На самом деле код работает нормально, но я не знаю, почему он показывает ошибку, когда я удаляю значение заголовка аргумента в этой строке NewsArticle(Category c1, const char *title="untitled")
и оставляю это так NewsArticle(Category c1, const char *title)
?
#include<iostream>
#include<cstring>
using namespace std;
class Category {
private:
char name[20];
public:
Category() {
//cout<<"Category default constructor"<<endl;
strcpy(name,"unnamed");
}
Category(char *name) {
//cout<<"Category constructor"<<endl;
strncpy(this->name,name,20);
this->name[19]=0;
}
Category(const Category &n) {
strcpy(name,n.name);
//cout<<"Category copy-constructor"<<endl;
}
void print() {
cout<<"Category: "<<name<<endl;
}
};
class NewsArticle {
private:
Category c1;
char title[30];
public:
NewsArticle() {
strcpy(title,"untitled");
//cout<<"NewsArticle default constructor"<<endl;
}
NewsArticle(Category c1, const char *title="untitled") {
//cout<<"NewsArticle constructor"<<endl;
this->c1=c1;
strncpy(this->title,title,30);
this->title[29]=0;
}
NewsArticle(const NewsArticle &n) {
//cout<<"NewsArticle copy-constructor"<<endl;
strcpy(title,n.title);
c1=n.c1;
}
void print() {
cout<<"Article title: "<<title<<endl;
c1.print();
}
};
class FrontPage {
private:
NewsArticle n1;
float price;
int editionNumber;
public:
FrontPage() {
//cout<<"Front Page default constructor"<<endl;
price=0;
editionNumber=0;
}
FrontPage(NewsArticle n1, float price=0, int editionNumber=0) {
//cout<<"Front Page constructor"<<endl;
this->n1=n1;
this->price=price;
this->editionNumber=editionNumber;
}
void print() {
cout<<"Price: "<<price<<", Edition number: "<<editionNumber<<endl;
n1.print();
}
};
int main() {
char categoryName[20];
char articleTitle[30];
float price;
int editionNumber;
int testCase;
cin >> testCase;
if (testCase == 1) {
int iter;
cin >> iter;
while (iter > 0) {
cin >> categoryName;
cin >> articleTitle;
cin >> price;
cin >> editionNumber;
Category category(categoryName);
NewsArticle article(category, articleTitle);
FrontPage frontPage(article, price, editionNumber);
frontPage.print();
iter--;
}
} else if (testCase == 2) {
cin >> categoryName;
cin >> price;
cin >> editionNumber;
Category category(categoryName);
NewsArticle article(category);
FrontPage frontPage(article, price, editionNumber);
frontPage.print();
}// test case 3
else if (testCase == 3) {
cin >> categoryName;
cin >> articleTitle;
cin >> price;
Category category(categoryName);
NewsArticle article(category, articleTitle);
FrontPage frontPage(article, price);
frontPage.print();
} else {
FrontPage frontPage = FrontPage();
frontPage.print();
}
return 0;
}