Advertisement
196040

OOP labs 3 Naslovna stranica

Apr 28th, 2020
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. class Category { // која ја претставува категоријата во која спаѓа веста
  5. private:
  6. // наслов од максимум 30 знаци (char title[30])
  7. char name[20]; //Класата Category треба да содржи име од максимум 20 знаци (char name[20])
  8. public:
  9.  
  10.     Category () {
  11.     strcpy(this->name, "unnamed"); // со предодредена вредност unnamed.
  12.     }
  13.     void print() { //За категоријата се печати само името: Category: [name].
  14.     cout<<"Category: "<<name;
  15.     } // За сите класи треба да напишете соодветен метод за печатење print().
  16. Category(char * name){
  17. strcpy(this->name, name);
  18. }
  19. };
  20. class NewsArticle { // Класата NewsArticle треба да содржи:
  21. private:
  22. Category c; // објект од класата Category
  23. char title[30];
  24. public:
  25. NewsArticle(Category c) { // За класата NewsArticle напишете предодреден конструктор
  26. strcpy(this->title, "untitled");  // со предодредена вредност untitled
  27. }
  28. NewsArticle(){}
  29. NewsArticle(Category c, char * title) { // и конструктор со параметри.
  30. this->c=c;
  31. strcpy(this->title, title);
  32. }
  33. NewsArticle(const NewsArticle &n) {
  34. this->c=n.c;
  35. strcpy(this->title, n.title);
  36. }
  37.     void print() { // За веста се печати насловот, па категоријата во нов ред:
  38.     cout<<"Article title: "<<title<<endl; //Title: [title]
  39.     c.print(); //category.print()
  40.     }
  41. };
  42. class FrontPage {// напишете класа FrontPage која ќе содржи:
  43.     private:
  44.     NewsArticle n; //објект од класата NewsArticle која ја претставува насловната вест на страницата
  45.     float price; // цена (float price) со предодредена вредност 0
  46.     int editionNumber; // број на издание на списанието (int editionNumber) со предодредена вредност 0
  47.     public:
  48.         FrontPage(NewsArticle n) {
  49.         this->n=n;
  50.         this->price=0;
  51.         this->editionNumber=0;
  52.     }
  53.     FrontPage(){} // За класата FrontPage напишете предодреден (default) конструктор
  54.     FrontPage(NewsArticle n, float price, int editionNumber) { // и конструктор со параметри.
  55.     this->n=n;
  56.         this->price=price;
  57.         this->editionNumber=editionNumber;
  58.     }
  59.     FrontPage(NewsArticle n, float price) {
  60.     this->n=n;
  61.         this->price=price;
  62.         this->editionNumber=0;
  63.         }
  64.     void print() { // За насловната страница се печати цената и изданието во прв ред, па веста во втор:
  65. cout<<"Price: "<<price<<" "<<"Edition number: "<<editionNumber<<endl;
  66. n.print(); //Price: [price], Edition number: [editionNumber]
  67. //article.print()
  68.     }
  69. }; //Внимајте на редоследот на параметрите во конструкторите.
  70. // Не го менувајте main методот.
  71. int main() {
  72.     char categoryName[20];
  73.     char articleTitle[30];
  74.     float price;
  75.     int editionNumber;
  76.     int testCase;
  77.     cin >> testCase;
  78.     if (testCase == 1) {
  79.         int iter;
  80.         cin >> iter;
  81.         while (iter > 0) {
  82.             cin >> categoryName;
  83.             cin >> articleTitle;
  84.             cin >> price;
  85.             cin >> editionNumber;
  86.             Category category(categoryName);
  87.             NewsArticle article(category, articleTitle);
  88.             FrontPage frontPage(article, price, editionNumber);
  89.             frontPage.print();
  90.             iter--;
  91.         }
  92.     }
  93.     else if (testCase == 2) {
  94.         cin >> categoryName;
  95.         cin >> price;
  96.         cin >> editionNumber;
  97.         Category category(categoryName);
  98.         NewsArticle article(category);
  99.         FrontPage frontPage(article, price, editionNumber);
  100.         frontPage.print();
  101.     }// test case 3
  102.     else if (testCase == 3) {
  103.         cin >> categoryName;
  104.         cin >> articleTitle;
  105.         cin >> price;
  106.         Category category(categoryName);
  107.         NewsArticle article(category, articleTitle);
  108.         FrontPage frontPage(article, price);
  109.         frontPage.print();
  110.     }
  111.     else {
  112.         FrontPage frontPage = FrontPage();
  113.         frontPage.print();
  114.     }
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement