metalni

OOP Labs 3 Naslovna Stranica

May 30th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. //category class
  6. class Category {
  7.     private:
  8.         char name[20];
  9.     public:
  10.         Category();
  11.         Category(const char * ime);
  12.         void print();
  13. };
  14.  
  15. Category::Category(){
  16.     strcpy(this->name, "unnamed");
  17. }
  18. Category::Category(const char * ime){
  19.     strcpy(this->name, ime);
  20. }
  21. void Category::print(){
  22.     cout << "Category: " << this->name << endl;
  23. }
  24.  
  25. //NewsArticle class
  26. class NewsArticle{
  27.     private:
  28.         Category cat;
  29.         char title[30];
  30.     public:
  31.         NewsArticle();
  32.         NewsArticle(Category c);
  33.         NewsArticle(Category c, const char * title);
  34.         void print();
  35. };
  36.  
  37. NewsArticle::NewsArticle(){
  38.     strcpy(this->title, "untitled");
  39. }
  40. NewsArticle::NewsArticle(Category c){
  41.     this->cat=c;
  42.     strcpy(this->title, "untitled");
  43. }
  44.  
  45. NewsArticle::NewsArticle(Category c, const char * title){
  46.     this->cat=c;
  47.     strcpy(this->title, title);
  48. }
  49. void NewsArticle::print(){
  50.     cout << "Article title: " << this->title << endl;
  51.     cat.print();
  52. }
  53.  
  54. //FrontPage
  55. class FrontPage{
  56.     private:
  57.         NewsArticle news;
  58.         float price;
  59.         int editionNumber;
  60.     public:
  61.         FrontPage();
  62.         FrontPage(NewsArticle n, float cena);
  63.         FrontPage(NewsArticle n, float cena, int id);
  64.         void print();
  65. };
  66.  
  67. FrontPage::FrontPage(){
  68.     this->price=0.0;
  69.     this->editionNumber=0;
  70. }
  71. FrontPage::FrontPage(NewsArticle n, float cena){
  72.     this->news=n;
  73.     this->price=cena;
  74.     this->editionNumber=0;
  75. }
  76. FrontPage::FrontPage(NewsArticle n, float cena, int id){
  77.     this->news=n;
  78.     this->price=cena;
  79.     this->editionNumber=id;
  80. }
  81. void FrontPage::print(){
  82.     cout << "Price: " << this->price << ", Edition number: " << this->editionNumber << endl;
  83.     news.print();
  84. }
  85.  
  86.  
  87. // main
  88. int main() {
  89.     char categoryName[20];
  90.     char articleTitle[30];
  91.     float price;
  92.     int editionNumber;
  93.  
  94.     int testCase;
  95.     cin >> testCase;
  96.  
  97.  
  98.     if (testCase == 1) {
  99.         int iter;
  100.         cin >> iter;
  101.         while (iter > 0) {
  102.             cin >> categoryName;
  103.             cin >> articleTitle;
  104.             cin >> price;
  105.             cin >> editionNumber;
  106.             Category category(categoryName);
  107.             NewsArticle article(category, articleTitle);
  108.             FrontPage frontPage(article, price, editionNumber);
  109.             frontPage.print();
  110.             iter--;
  111.         }
  112.     }
  113.     else if (testCase == 2) {
  114.         cin >> categoryName;
  115.         cin >> price;
  116.         cin >> editionNumber;
  117.         Category category(categoryName);
  118.         NewsArticle article(category);
  119.         FrontPage frontPage(article, price, editionNumber);
  120.         frontPage.print();
  121.     }// test case 3
  122.     else if (testCase == 3) {
  123.         cin >> categoryName;
  124.         cin >> articleTitle;
  125.         cin >> price;
  126.         Category category(categoryName);
  127.         NewsArticle article(category, articleTitle);
  128.         FrontPage frontPage(article, price);
  129.         frontPage.print();
  130.     }
  131.     else {
  132.         FrontPage frontPage = FrontPage();
  133.         frontPage.print();
  134.     }
  135.     return 0;
  136. }
Add Comment
Please, Sign In to add comment