0

I am just trying to get set up with some simple classes in C++. I am trying to create an Order type that takes in a price (double), quantity (int) and a style (std::string)

Here is my order.h

#ifndef ORDER_H
#define ORDER_H

#include <string>
#include <iostream>


class Order {
    private:
        double limit_price;
        int quantity;
        std::string style;
    public:
        Order();
        Order(double price, int quantity, std::string style);

        void print_price();
        void print();
};
#endif

My implementation in order.cpp.

#include "order.h"
#include <iostream>


Order::Order(){
    limit_price = 0;
    quantity = 0;
    style = "bid";
}


Order::Order(double price, int quantity, std::string style){
    limit_price = price;
    quantity = quantity;
    style = style;
}

void Order::print_price(){
    std::cout << "limit_price = " << limit_price << std::endl;
}

void Order::print(){
    std::cout << style << " " << quantity << "@" << limit_price << std::endl;
}

And here is my simple test code.

#include "order.cpp"
#include <iostream>
#include <string>

int main(){

    Order null_order = Order();
    Order order = Order(12.3, 2, "bid");

    null_order.print();
    order.print();

    return 0;
}

However, for a reason I don't understand, when I run I run my test file, instead of getting

bid 0@0
bid 2@12.3

As I would have expected, I get something like the following.

bid 0@0
 -1722935952@12.3

Where the large negative number changes on each run.

  • To compile I am just using the command `g++ tester.cpp -o tester` where tester.cpp is the name of my test file – James Delaney Oct 22 '20 at 20:19
  • 5
    `quantity = quantity;` What do you think that will do? Why do you think it will do that instead of something else? – Sneftel Oct 22 '20 at 20:19
  • `#include "order.cpp"` is a no-no. Include header files (the .h) and compile and link the cpp files. – user4581301 Oct 22 '20 at 20:20
  • 2
    Also read https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings – Sneftel Oct 22 '20 at 20:21
  • 4
    You will find the unfortunately rarely taught [Member Initializer List](https://en.cppreference.com/w/cpp/language/constructor) extremely helpful in untangling this mess. – user4581301 Oct 22 '20 at 20:21
  • thank you all. Setting the quantity as this->quantity = quantity fixed it. Same for style. – James Delaney Oct 22 '20 at 20:22

1 Answers1

2

In your constructor the name of the parameters shadow the members:

Order::Order(double price, int quantity, std::string style){
    limit_price = price;
    quantity = quantity;
    style = style;
}

What do you think quantity is in the second line? Why should it something different on the left hand side than on the right hand side?

quantity refers to the parameter.

You can prepend this-> to members to disambiguate, though thats rather uncommon. In such cases, better rename either of them.

Anyhow you should initialize members rather than assign in the body of the constructor. Members are initialized before the body of the constructor is executed. One way to initialize members is the member initializer list:

Order::Order(double p, int q, std::string s) : limit_price(p),quantity(q),style(s)
{ /* empty body */ }

And because in the initializer list there is no danger of ambiguity we can use the same name for parameters as for the members:

Order::Order(double limit_price, int quantity, std::string style) : limit_price(limit_price),quantity(quantity),style(style)
{}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I wonder if there have been any proposals for a shortcut for the initializer list where the parameters have the same names as the members. – Barmar Oct 22 '20 at 21:02
  • @Barmar: I don't think there have in C++. In C# there was/is a proposal for "record types", which would automatically store all constructor parameters. They were added to a technology preview version of the compiler, removed again before release (5.0 I think?), and look slated to be part of the next release (9.0) – Ben Voigt Oct 22 '20 at 21:05
  • @Barmar hm not sure if it would be worth the added complexity. New syntax for one special case of constructor. On the other hand, its a rather common line with triple repetition. – 463035818_is_not_an_ai Oct 22 '20 at 21:10
  • It's a special case, but also a common case. I can imagine a syntax like `: ()`. – Barmar Oct 22 '20 at 21:12
  • @Barmar actually it an intersting idea. Order of initialization could be always the correct one, one source of mistakes eliminated – 463035818_is_not_an_ai Oct 22 '20 at 21:21