Classes?

Hi!

I got a task where I must write the header file to the main.cpp (see below). I have found out that I must create classes and somehow make them remember other class-objects to get the output screen that is given, as well. However, I can't figure out how to achieve it... I have begun writing the code, below it is visible, as well.

Any help would be appreciated!!

The main.cpp (it is given, can't be edited):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "solution.hpp"

int main(){
  NewsSource ns1("News Source 1");
  NewsSource ns2("News Source 2");

  NewConsumer dirk("Dirk");
  NewConsumer blaise("Blaise");
  blaise.registerToSource(&ns1);
  dirk.registerToSource(&ns2);

  SportsNews nn1("sports for blaise");
  SportsNews nn2("sports for dirk");
  CulturalNews nn3("cultural news for blaise");
  CulturalNews nn4("cultura news for dirk");

  News* news1 = &nn1;
  news1 = &nn3;

  ns1.publishNews(&nn1);
  ns1.publishNews(news1);
  ns2.publishNews(&nn2);
  ns2.publishNews(&nn4);

  blaise.printInbox();
  blaise.printCultural();
  blaise.printSports();

  dirk.printInbox();
  dirk.printCultural();
  dirk.printSports();

}


My .hpp file so far (solution.hpp, I have to work out this one):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<string>
#include<vector>

class News {
  private:
    std::string name;

  public:
    News(std::string name) : name(name) {}
    ~News(){};
};

class NewsSource {
  private:
    std::string name;
    std::vector<News*> sports;
    std::vector<News*> cultural;
    std::vector<News*> all;

  public:
    NewsSource(std::string name) : name(name) {}

    ~NewsSource(){};
};

class NewsConsumer{
  private:
    std::string consumer;

  public:
    NewsConsumer(std::string consumer) : consumer(consumer) {}

    std::string getConsumer() const { return consumer; }

    void registerToSource(NewsSource& source){
      

    }
    
    void printInbox(){
      std::cout << "Inbox for consumer " << getConsumer() << ":\n";



    }
    void printCultural(){
      std::cout << "Cultural news for consumer " << getConsumer() << ":\n";


    }
    void printSports(){
      std::cout << "Sports news for consumer " << getConsumer() << ":\n";

    }


    ~NewsConsumer(){};

};

class SportsNews : public News {
  private:
    std::string name;

  public:
    SportsNews(std::string name) : News(name) {}

    ~SportsNews(){};
};

class CulturalNews : public News {
  private:
    std::string name;

  public:
    CulturalNews(std::string name) : News(name) {}


    ~CulturalNews(){};
};


The output, which must look like this:

Inbox for consumer Blaise:
sports for blaise
cultural news for blaise

Cultural news for consumer Blaise:
cultural news for blaise

Sports news for consumer Blaise:
sports for blaise

Inbox for consumer Dirk:
sports for dirk
cultural news for dirk

Cultural news for consumer Dirk:
cultural news for dirk

Sports news for consumer Dirk:
sports for dirk

Last edited on
If you make some changes along the following lines and write some code for adding to the <vector> lists, then write the publishNews() function, you'll be well on your way to doing your homework.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
#include<string>
#include<vector>

class News
{
private:
    std::string name;
    
public:
    News(std::string name) : name(name) {}
    ~News(){};
};

class NewsSource
{
private:
    std::string name;
    std::vector<News*> sports;
    std::vector<News*> cultural;
    std::vector<News*> all;
    
public:
    NewsSource(std::string name) : name(name) {}
    
    ~NewsSource(){};
};

class NewsConsumer
{
private:
    std::string consumer;
        
public:
    NewsConsumer(std::string consumer) : consumer(consumer) {}
    
    std::string getConsumer() const { return consumer; }
    
    void registerToSource(const NewsSource& source)
    {
    }
    
    void printInbox()
    {
        std::cout << "Inbox for consumer " << getConsumer() << ":\n";
    }
    
    void printCultural()
    {
        std::cout << "Cultural news for consumer " << getConsumer() << ":\n";
    }
    
    void printSports()
    {
        std::cout << "Sports news for consumer " << getConsumer() << ":\n";
    }
};

class SportsNews : public News
{
private:
    std::string name;
    
public:
    SportsNews(std::string name) : News(name) {}
    
    ~SportsNews(){};
};


class CulturalNews : public News {
private:
    std::string name;
    
public:
    CulturalNews(std::string name) : News(name) {}
    ~CulturalNews(){};
};

int main(){
    NewsSource ns1("News Source 1");
    NewsSource ns2("News Source 2");
    
    NewsConsumer dirk("Dirk");
    NewsConsumer blaise("Blaise");
    
    blaise.registerToSource(ns1);
    dirk.registerToSource(ns2);
    
    SportsNews nn1("sports for blaise");
    SportsNews nn2("sports for dirk");
    CulturalNews nn3("cultural news for blaise");
    CulturalNews nn4("cultura news for dirk");
    
    News* news1 = &nn1;
    news1 = &nn3;
    
//    ns1.publishNews(&nn1);
//    ns1.publishNews(news1);
//    ns2.publishNews(&nn2);
//    ns2.publishNews(&nn4);
//
    blaise.printInbox();
    blaise.printCultural();
    blaise.printSports();
    
    dirk.printInbox();
    dirk.printCultural();
    dirk.printSports();
}



Inbox for consumer Blaise:
Cultural news for consumer Blaise:
Sports news for consumer Blaise:
Inbox for consumer Dirk:
Cultural news for consumer Dirk:
Sports news for consumer Dirk:
Program ended with exit code: 0
The problem is that I can't change the main.cpp, I can only work in solution.hpp ^^' But yeah, my idea is to somehow make difference between SportsNews and CulturalNews class (like deciding the object uses which class) and then publish it to the right vector, so it'd make printing it out easier
Well main L17-18 don't make sense. news1 is set to the address of nn1 and is then immediately set to the address of nn3 ??? Which do you want?

IMO giving an output and main() and asked to design and code the classes is not a good way. You either get the class design and the output and code the classes and main() or you get the program description and design your own classes, main etc.

seeplus wrote:
Well main L17-18 don't make sense. news1 is set to the address of nn1 and is then immediately set to the address of nn3 ??? Which do you want?

They reveal a class hierarchy:
1
2
News* news1 = &nn1; // SportNews IS-A News
news1 = &nn3; // CulturalNews IS-A News 

As you said, this is a peculiar approach to inject skill/knowledge into student.


1
2
3
dirk.registerToSource( &ns2 );

ns1.publishNews( &nn1 );

These two functions take address of object as parameter. Address is stored in a pointer, not as reference.


1
2
3
  dirk.printInbox();
  dirk.printCultural();
  dirk.printSports();

These three functions all print a header and a list of News from a source.
The printInbox() prints all News.
The printCultural() prints only the CulturalNews.
The printSports() prints only the SportsNews.
I bet that the class has discussed dynamic_cast ...

... and yes, class hierarchy is peculiar implementation for a table that has two columns: newstext and news category. Wait till you get a news that is about both culture and sports ...
Last edited on
I don't know what exactly you want to do but I made it look like your output:

solution.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<string>
#include<vector>

class News {
private:
    std::string name;
    friend std::ostream& operator<<(std::ostream& os, News& n) {
        os << n.name << std::endl;
        return os;
    }
public:
    News() = default;
    News(std::string name) : name(name) {}
    ~News() {};
};

class NewsSource {
private:
    std::string name;
    std::vector<News*> sports;
    std::vector<News*> cultural;
    std::vector<News*> all;
    friend std::ostream& operator<<(std::ostream& os, NewsSource& n) {
        os << n.name << std::endl;
        return os;
    }
public:
    NewsSource() = default;
    NewsSource(std::string name) : name(name) {}
    void publishNews(News* n) { }

    ~NewsSource() {};
};

class NewsConsumer {
private:
    std::string consumer;
    friend std::ostream& operator<<(std::ostream& os, NewsConsumer& n) {
        os << n.consumer << std::endl;
        return os;
    }
public:
    NewsConsumer() = default;
    NewsConsumer(std::string consumer) : consumer(consumer) {}

    std::string getConsumer() const { return consumer; }

    void registerToSource(NewsSource* source) {


    }

    void printInbox() {
        std::cout << "Inbox for consumer " << getConsumer() << ":\n";



    }
    void printCultural() {
        std::cout << "Cultural news for consumer " << getConsumer() << ":\n";


    }
    void printSports() {
        std::cout << "Sports news for consumer " << getConsumer() << ":\n";

    }


    ~NewsConsumer() {};

};

class SportsNews : public News {
private:
    std::string name;
    friend std::ostream& operator<<(std::ostream& os, SportsNews& sn) {
        os << sn.name << std::endl;
        return os;
    }
public:
    SportsNews() = default;
    SportsNews(std::string n) : name{ n } {}

    ~SportsNews() {};
};

class CulturalNews : public News {
private:
    std::string name;

public:
    CulturalNews(std::string name) : News(name) {}


    ~CulturalNews() {};
};


MAIN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include "solution.hpp"

using namespace std;

int main() {
	NewsSource ns1("News Source 1");
	NewsSource ns2("News Source 2");
	
	NewsConsumer dirk("Dirk");
	NewsConsumer blaise("Blaise");
	blaise.registerToSource(&ns1);
	dirk.registerToSource(&ns2);
	
	SportsNews nn1("sports for blaise");
	SportsNews nn2("sports for dirk");
	CulturalNews nn3("cultural news for blaise");
	CulturalNews nn4("cultural news for dirk");
	
	News* news1 = &nn1;
	news1 = &nn3;
	
	ns1.publishNews(&nn1);
	ns1.publishNews(news1);
	ns2.publishNews(&nn2);
	ns2.publishNews(&nn4);
	
	blaise.printInbox();
	cout << nn1;
	cout << nn3 << endl;
	blaise.printCultural();
	cout << nn3 << endl;
	blaise.printSports();
	cout << nn1 << endl;
	
	dirk.printInbox();
	cout << nn2;
	cout << nn4 << endl;
	dirk.printCultural();
	cout << nn4 << endl;
	dirk.printSports();
	cout << nn2 << endl;

	return 0;
}

The output, which must look like this:

Inbox for consumer Blaise:
sports for blaise
cultural news for blaise

Cultural news for consumer Blaise:
cultural news for blaise

Sports news for consumer Blaise:
sports for blaise

Inbox for consumer Dirk:
sports for dirk
cultural news for dirk

Cultural news for consumer Dirk:
cultural news for dirk

Sports news for consumer Dirk:
sports for dirk


The really hard part in this is that the shown output has an empty line both before and after everything, but otherwise only a single empty line per function.
Topic archived. No new replies allowed.