Aleksandar Angelov
Aleksandar Angelov

Reputation: 3

Problem with constructor overloading in C++

I have difficulty with my overloading constructor in class NewsArticle. Actually the code works fine but i don't know why it shows error when i delete the value of argument title in this line NewsArticle(Category c1, const char *title="untitled") and leave it like this 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;
}

Upvotes: 0

Views: 311

Answers (1)

srilakshmikanthanp
srilakshmikanthanp

Reputation: 2389

i don't know why it shows error when i delete the value of argument title in this line NewsArticle(Category c1, const char *title="untitled") and leave it like this NewsArticle(Category c1, const char *title)?

In you code

   NewsArticle article(category);

you are passing category But the constructor in NewsArticle have following types only

   NewsArticle();
   NewsArticle(Category c1, const char *title);
   NewsArticle(const NewsArticle &n);

So there is no matching prototype to call but if you have the default argument

   NewsArticle(Category c1, const char *title="untitled")

NewsArticle article(category); wiil call this or call like this

   NewsArticle article(category,"untitled");

will call NewsArticle(Category c1, const char *title);

Upvotes: 1

Related Questions