I am a beginner with programming and I really need to resolve this problem.

So, I need to implement the following class that represents this type of crowd and operations with those :

class Crowd
{
int* element;
int number;
public:
Crowd(int m_number, int* m_element);
Crowd(Crowd&);
~Crowd();
Crowd& operator= (Crowd& m);
Crowd operator+ (Crowd& m);
Crowd operator* (Crowd& m);
Crowd operator- (Crowd& m);
Friend ostream& operator << (ostream& o, Crowd& m);
};.

Thanks you guys.

Recommended Answers

All 6 Replies

So, I need to implement the following class

Okay. And?

Thanks you guys.

For what? You completely failed to ask a question. I'll take the high ground and assume that you're not expecting us to do your homework for you.

I'll take the high ground and assume that you're not expecting us to do your homework for you.

I'm sorry, but i don't know how to work with classes and i really need to solve this in ~2 hours.
So, please give me some advice.

So, please give me some advice.

My advice is learn the material before you're so pressed for time. If you don't know anything about how to work with classes, then there's nothing I can do for you in ~2 hours that doesn't constitute doing the assignment for you. Sorry, but this assignment is a loss due to your procrastination.

If you have a C++ book I'd suggest cracking it open and reading it as well as following the exercises. If you don't have a book, you should probably get one, but you can also search Google for tutorials.

I know, you are right.

You have promised the compiler that you will create the following functions:

        Crowd(int m_number, int* m_element);
        Crowd(Crowd&);
        ~Crowd();
        Crowd& operator= (Crowd& m);
        Crowd operator+ (Crowd& m);
        Crowd operator* (Crowd& m);
        Crowd operator- (Crowd& m);
        Friend ostream& operator << (ostream& o, Crowd& m);

Start at the top. The first function, the constructor, will look something like this:

Crowd::Crowd (int input_number, int* p_input_element)
{

    // YOUR CODE HERE
}

Just start implementing them.

There are a number of problems with your class. One is a lack of initializers in your constructors. Two is that you are using non-const arguments for many functions (copy constructor, assignment operator, and other operators). Not delving into the guts of your method functions, this is a better approach:

class Crowd
{
    int* m_pElement;
    int m_number;
public:
    Crowd();
    Crowd(int number, int* pElement);
    Crowd(const Crowd&);
    ~Crowd();
    Crowd& operator=(const Crowd& m);
    Crowd operator+(const Crowd& m);
    Crowd operator*(const Crowd& m);
    Crowd operator-(const Crowd& m);
    Friend ostream& operator<<(ostream& o, const Crowd& m);
};

Crowd::Crowd()
: m_pElement(0),
  m_number(0)
{}

Crowd::Crowd(int number, int* pElement)
: m_pElement(pElement),
  m_number(number)
{}

Crowd::Crowd(const Crowd& cpy)
: m_pElement(cpy.m_pElement),
  m_number(cpy.m_number)
{}

Do bear in mind that the pointer assigned in the constructors is not a copy, and it isn't protected via a reference count, so if in your class distructor you delete the pointer, it will also no longer be valid for other members of the class that contain it - can you spell core dump? :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.