Hi.

(If you don't feel like explaining, please tell me what I need to read up on. Derived classes? Polymorphism?).

I have got this assignment from school, in which we are handed a main.cpp file and we are supposed to write the class for it.

But there's a few thing about classes that I don't understand. Here's a bit of code:

Product pineapple = Product( "Pineapple" );
Product milk = Product( "Milk", 7 );
Product mustard = Product( "Mustard", 35, "Very exclusive and good" );

cout <<  Product::getNumberOfProducts() << endl;

I understand the override constructor function part.

But why have my teacher defined the class like:

Product pineapple = Product( "Pineapple" );

and not like:

Product pineapple( "Pineapple" );

?

And the second question. How can he count the number of products from this function, it's not even a declared class.

Product::getNumberOfProducts()

I agree that your questions are valid, the little sample you provided is confusing. Could you post the whole of the main.cpp you were given, it might give us further clues as to what your prop expects.

The only usage of line 5 I could imagine is if the class keeps track of how many different instances of a product type have been instantiated - a static data member might do this, being incremented every time a constructor is executed. Otherwise, it doesn't make much sense in that use.

Okey, here's the whole main.cpp
(some text is in swedish, but all code is in eng so you should be able to understand it :)

#include <iostream>
#include <vector>
#include "Product.h"

using namespace std;

void displayProduct( Product& product );

void displayProducts( vector<Product*> products );

void advertiseCheap( Product product );

int main()
{
    Product pineapple = Product( "Ananas" );
    Product milk = Product( "Mjölk", 7 );
    Product mustard = Product( "Gotlandssenap", 35, "Mycket exklusiv och god" );

    displayProduct( pineapple );
    displayProduct( milk );
    displayProduct( mustard );

    cout << "Just nu finns de " << Product::getNumberOfProducts() << " produkter i sortimentet." << endl << endl;

    string teaName[] = {"Guteblandning", "Kalkstensdrömmar", "Munkte", "Gotländsk sommarblandning", "Sylves lilla gröna", "Den Raude"};
    vector<Product*> teas;
    for( int i = 0; i < sizeof(teaName)/sizeof(string); i++ )
    {
        Product* tea = new Product( teaName[i], 22, "Gotländskt te" );
        teas.push_back( tea );
    }

    displayProducts( teas );
    cout << "Just nu finns de " << Product::getNumberOfProducts() << " produkter i sortimentet." << endl << endl ;


    cout << "Grannbutiken sålde visst också te, vi tar bort det ur sortimentet igen" << endl;
    for( vector<Product*>::iterator tea = teas.begin(); tea != teas.end(); tea++ )
    {
        delete *tea;
    }

    cout << "Just nu finns de " << Product::getNumberOfProducts() << " produkter i sortimentet." << endl << endl;

    pineapple.setPrice( 15 );
    pineapple.setDescription( "Mycket god och taggig! Måste provas" );
    advertiseCheap( pineapple );

    cout << "Just nu finns de " << Product::getNumberOfProducts() << " produkter i sortimentet." << endl << endl;
}

void displayProduct( Product& product )
{
    cout << product.getName() << " kostar " << product.getPrice() << " kr: " << product.getDescription() << endl;
}

void displayProducts( vector<Product*> products )
{
    for( vector<Product*>::iterator product = products.begin(); product != products.end(); product++ )
    {
        displayProduct( **product );
    }
}

void advertiseCheap( Product product )
{
    cout << "EXTRA! EXTRA!" << endl;
    cout << product.getName() << ": " << product.getDescription() << endl
         << "Till det låga priset av " << product.getPrice() << " kr" << endl
         << "Köp idag!" << endl;
}

OK, I got this to work, see:

Ananas kostar 0 kr:
Mj÷lk kostar 7 kr:
Gotlandssenap kostar 35 kr: Mycket exklusiv och god
Just nu finns de 3 produkter i sortimentet.

Guteblandning kostar 22 kr: GotlΣndskt te
Kalkstensdr÷mmar kostar 22 kr: GotlΣndskt te
Munkte kostar 22 kr: GotlΣndskt te
GotlΣndsk sommarblandning kostar 22 kr: GotlΣndskt te
Sylves lilla gr÷na kostar 22 kr: GotlΣndskt te
Den Raude kostar 22 kr: GotlΣndskt te
Just nu finns de 9 produkter i sortimentet.

Grannbutiken sσlde visst ocksσ te, vi tar bort det ur sortimentet igen
Just nu finns de 9 produkter i sortimentet.

EXTRA! EXTRA!
Ananas: Mycket god och taggig! Mσste provas
Till det lσga priset av 15 kr
K÷p idag!
Just nu finns de 9 produkter i sortimentet.

Write your class as normal, see the displayProduct function near the bottom for some good hints on the accessor functions you'll need. Names for setter (mutator) functions are also in the code. The odd one is Product::getNumberOfProducts() . This needs to also be a public function that is made static, and the value it accesses will have to a static class member that gets incremented in the constructor (gets incremented every time an object is created.)
See here and here for some relevant material.

You can write just one constructor, using default parameters.

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.