Execution Problem w/memory object

Discussion in 'C++' started by August1, Sep 14, 2004.

  1. August1

    August1 Guest

    This is a short program that I have written from a text that
    demonstrates a class object variable created on the stack memory and
    another class object variable created on the heap memory. By way of
    the text, the program is supposed to demonstrate the use of a copy
    constructor function for the object created on the heap (because there
    is a char* pointer variable in the instantiated class) and how you
    would use the delete keyword with the object and to also use a
    destructor for the char* pointer variable which is also placed on the
    heap memory when instantiated - this being becuase the heap memory
    will not be freed when the main function goes out of scope, as with
    the class object variable declared on the stack memory does by default
    constructor.

    The program is exactly as demonstrated in the text - WHY the problem?
    Thank you in advance.

    //interface file Stocks.h
    #if !defined(STOCKS_H)
    #define STOCKS_H

    class Stocks
    {
    public:
    Stocks();//default constructor
    Stocks(char* szName);//parameterized constructor
    Stocks(const Stocks&);//copy constructor declaration
    ~Stocks();//destructor
    //accessor function declarations
    void setStockName(char* szName);
    void setNumShares(int);
    void setPricePerShare(double);
    char* getStockName() const;
    int getNumShares() const;
    double getPricePerShare() const;
    double calcTotalValue();
    private:
    //declare private data members
    char* szStockName;//declares character array pointer
    int iNumShares;
    double dCurrentValue;
    double dPricePerShare;
    };

    #endif
    //end class Stocks.h

    ///////////////////////////////////////////
    //implementation file Stocks.cpp

    #include "Stocks.h"
    #include <string.h>
    #include <ostream.h>

    Stocks::Stocks()//defines default constructor
    {
    char* szStockName = new char[25];
    strcpy(szStockName,"");
    iNumShares = 0;
    dCurrentValue = 0;
    dPricePerShare = 0;
    };

    Stocks::Stocks(char* szName)//defines parameterized constructor
    {
    char* szStockName = new char[25];
    strcpy(szStockName,szName);
    iNumShares = 0;
    dCurrentValue = 0;
    dPricePerShare = 0;
    };

    Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
    {
    szStockName = new char[25];
    strcpy(szStockName, sourceStock.szStockName);
    };

    Stocks::~Stocks()//destructor definition
    {
    delete[] szStockName;
    cout << "Destructor called." << endl;

    };


    void Stocks::setStockName(char* szName)
    {
    strcpy(szStockName,szName);
    }

    void Stocks::setNumShares(int iShares)
    {
    iNumShares = iShares;
    }

    void Stocks::setPricePerShare(double dPrice)
    {
    dPricePerShare = dPrice;
    }

    char* Stocks::getStockName() const
    {
    return szStockName;
    }

    int Stocks::getNumShares(void)const
    {
    return iNumShares;
    }

    double Stocks::getPricePerShare()const
    {
    return dPricePerShare;
    }

    double Stocks::calcTotalValue()
    {
    dCurrentValue = iNumShares * dPricePerShare;
    return dCurrentValue;
    }
    //end Stocks.cpp
    //////////////////////////////////

    //client source file used to instantiate custom class objects
    //Shares.cpp
    #include "Stocks.h"
    #include <iostream>
    #include <string>
    #include <iomanip>//for use of setprecision() and setiosflags()
    using namespace std;

    void main()
    {
    cout << "\t\t\t\t\b\bStock Portfolio" << endl << endl
    << "This program displays the current values of stock you have
    purchased for" << endl
    << "both Cisco and Lucent Technologies." << endl << endl;

    //declares class object variable and passes value to parameterized
    constructor
    Stocks stockPick1("Cisco");

    //heap memory object that passes literal string to parameterized
    constructor
    Stocks* stockPick2 = new Stocks("Lucent");
    /*use indirection operator associated with heap memory object to
    access
    class method and pass literal numeric value*/
    stockPick2->setNumShares(200);
    stockPick2->setPricePerShare(59.5);

    //format numeric output in fixed notation with two decimals
    cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
    cout << "The current value of your stock in " <<
    stockPick1.getStockName()
    << " is $" << stockPick1.calcTotalValue() << "." << endl;

    cout << "The current value of your stock in " <<
    stockPick2->getStockName()
    << " is $" << stockPick2->calcTotalValue() << "." << endl;

    delete stockPick2;//manually delete the object from the heap memory

    }//end main
    August1, Sep 14, 2004
    #1
    1. Advertising

  2. August1

    Jack Klein Guest

    On 13 Sep 2004 21:17:08 -0700, (August1) wrote
    in comp.lang.c++:

    > This is a short program that I have written from a text that
    > demonstrates a class object variable created on the stack memory and
    > another class object variable created on the heap memory. By way of
    > the text, the program is supposed to demonstrate the use of a copy
    > constructor function for the object created on the heap (because there
    > is a char* pointer variable in the instantiated class) and how you
    > would use the delete keyword with the object and to also use a
    > destructor for the char* pointer variable which is also placed on the
    > heap memory when instantiated - this being becuase the heap memory
    > will not be freed when the main function goes out of scope, as with
    > the class object variable declared on the stack memory does by default
    > constructor.
    >
    > The program is exactly as demonstrated in the text - WHY the problem?
    > Thank you in advance.


    Either the text is wrong, or you copied it incorrectly.

    > //interface file Stocks.h
    > #if !defined(STOCKS_H)
    > #define STOCKS_H
    >
    > class Stocks
    > {
    > public:
    > Stocks();//default constructor
    > Stocks(char* szName);//parameterized constructor
    > Stocks(const Stocks&);//copy constructor declaration
    > ~Stocks();//destructor
    > //accessor function declarations
    > void setStockName(char* szName);
    > void setNumShares(int);
    > void setPricePerShare(double);
    > char* getStockName() const;
    > int getNumShares() const;
    > double getPricePerShare() const;
    > double calcTotalValue();
    > private:
    > //declare private data members
    > char* szStockName;//declares character array pointer
    > int iNumShares;
    > double dCurrentValue;
    > double dPricePerShare;
    > };
    >
    > #endif
    > //end class Stocks.h
    >
    > ///////////////////////////////////////////
    > //implementation file Stocks.cpp
    >
    > #include "Stocks.h"
    > #include <string.h>


    The recommended name for this C header in standard C++ is <cstring>.

    > #include <ostream.h>


    This header is not part of standard C++ at all. It should be
    <ostream>.

    > Stocks::Stocks()//defines default constructor
    > {
    > char* szStockName = new char[25];


    The pointer to char _defined_ above is a local automatic variable of
    the this function. It hides the class member of the same name, which
    could only be accessed by using this->szStockName. So you end up
    leaving the member uninitialized. The local variable goes out of
    scope at the end of the constructor without the memory ever being
    deleted, causing a memory leak.

    Remove the 'char *' from in front of szStockName and you will be
    allocating memory to the member variable with that name, instead of
    hiding it.

    > strcpy(szStockName,"");
    > iNumShares = 0;
    > dCurrentValue = 0;
    > dPricePerShare = 0;
    > };
    >
    > Stocks::Stocks(char* szName)//defines parameterized constructor
    > {
    > char* szStockName = new char[25];


    The same thing applies here. Eliminate the 'char *' in front of the
    pointer.

    > strcpy(szStockName,szName);
    > iNumShares = 0;
    > dCurrentValue = 0;
    > dPricePerShare = 0;
    > };
    >
    > Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
    > {
    > szStockName = new char[25];


    Note that in this constructor you got it right.

    > strcpy(szStockName, sourceStock.szStockName);
    > };
    >
    > Stocks::~Stocks()//destructor definition
    > {
    > delete[] szStockName;
    > cout << "Destructor called." << endl;
    >
    > };
    >
    >
    > void Stocks::setStockName(char* szName)
    > {
    > strcpy(szStockName,szName);
    > }
    >
    > void Stocks::setNumShares(int iShares)
    > {
    > iNumShares = iShares;
    > }
    >
    > void Stocks::setPricePerShare(double dPrice)
    > {
    > dPricePerShare = dPrice;
    > }
    >
    > char* Stocks::getStockName() const
    > {
    > return szStockName;
    > }
    >
    > int Stocks::getNumShares(void)const
    > {
    > return iNumShares;
    > }
    >
    > double Stocks::getPricePerShare()const
    > {
    > return dPricePerShare;
    > }
    >
    > double Stocks::calcTotalValue()
    > {
    > dCurrentValue = iNumShares * dPricePerShare;
    > return dCurrentValue;
    > }
    > //end Stocks.cpp
    > //////////////////////////////////
    >
    > //client source file used to instantiate custom class objects
    > //Shares.cpp
    > #include "Stocks.h"
    > #include <iostream>
    > #include <string>
    > #include <iomanip>//for use of setprecision() and setiosflags()
    > using namespace std;
    >
    > void main()


    You were doing pretty good up until now. The C++ standard requires
    that main() be defined with a return type of int. "void main()" is
    not, and never has been, legal C++. No matter what some illiterate
    compilers chose to promote.

    > {
    > cout << "\t\t\t\t\b\bStock Portfolio" << endl << endl
    > << "This program displays the current values of stock you have
    > purchased for" << endl
    > << "both Cisco and Lucent Technologies." << endl << endl;
    >
    > //declares class object variable and passes value to parameterized
    > constructor
    > Stocks stockPick1("Cisco");
    >
    > //heap memory object that passes literal string to parameterized
    > constructor
    > Stocks* stockPick2 = new Stocks("Lucent");
    > /*use indirection operator associated with heap memory object to
    > access
    > class method and pass literal numeric value*/
    > stockPick2->setNumShares(200);
    > stockPick2->setPricePerShare(59.5);
    >
    > //format numeric output in fixed notation with two decimals
    > cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
    > cout << "The current value of your stock in " <<
    > stockPick1.getStockName()
    > << " is $" << stockPick1.calcTotalValue() << "." << endl;
    >
    > cout << "The current value of your stock in " <<
    > stockPick2->getStockName()
    > << " is $" << stockPick2->calcTotalValue() << "." << endl;
    >
    > delete stockPick2;//manually delete the object from the heap memory
    >
    > }//end main


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 14, 2004
    #2
    1. Advertising

  3. August1

    August1 Guest

    >
    > Either the text is wrong, or you copied it incorrectly.
    >


    Hi Jack,
    Thanks for the expedient response. No, I did not copy anything
    errantly and believe me I tried a few changes before re-reading the
    text allocated to memory management. I was already well on my way to
    the changes you have highlighted and discussed.
    Yes, I am well informed that C++ uses the int main() function
    rather than void main(). Here is what I don't really appreciate, I
    suppose, Why are the majority of authors who are submitting texts that
    are approved for tutorials or educational purposes adhering to and
    promoting the void main() approach, without even mentioning the int
    main() standard???
    How should this normally be remedied, by adding a return statement
    with int main()? If this is the approach, what is the ususal return
    value?

    Thanks again?
    August1, Sep 14, 2004
    #3
  4. August1 wrote:
    >
    > >
    > > Either the text is wrong, or you copied it incorrectly.
    > >

    >
    > Hi Jack,
    > Thanks for the expedient response. No, I did not copy anything
    > errantly and believe me I tried a few changes before re-reading the
    > text allocated to memory management. I was already well on my way to
    > the changes you have highlighted and discussed.
    > Yes, I am well informed that C++ uses the int main() function
    > rather than void main(). Here is what I don't really appreciate, I
    > suppose, Why are the majority of authors who are submitting texts that
    > are approved for tutorials or educational purposes adhering to and
    > promoting the void main() approach, without even mentioning the int
    > main() standard???


    Because they don't know.
    Because a major company used void main() throught their whole
    documentation
    Because those instuctors think that this major company is god and defines
    the language
    ....

    > How should this normally be remedied, by adding a return statement
    > with int main()? If this is the approach, what is the ususal return
    > value?


    for main() there is an exception:
    You don't need to return anything. In this case the
    compiler has to insert a
    return 0;
    for you. But beware: main() is the only place where you can do
    this. In every other function with a return value it is prohibited
    to not actually return something.

    Other return values are

    return EXIT_SUCCESS
    return EXIT_FAILURE


    BTW: Online tutorial aren't a good way to learn the language. As you have
    seen, most of them are full of errors of the simplest type and work
    just by accident. Also: How do you know that the guy who wrote the
    tutorial knows what he is talking about?

    Get a good book. See the FAQ for recommendations.
    http://ma.rtij.nl/acllc-c .FAQ.html

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Sep 14, 2004
    #4
  5. August1

    August1 Guest

    Karl Heinz Buchegger <> wrote in message news:<>...
    > August1 wrote:
    > >
    > > >
    > > > Either the text is wrong, or you copied it incorrectly.
    > > >

    > >
    > > Hi Jack,
    > > Thanks for the expedient response. No, I did not copy anything
    > > errantly and believe me I tried a few changes before re-reading the
    > > text allocated to memory management. I was already well on my way to
    > > the changes you have highlighted and discussed.
    > > Yes, I am well informed that C++ uses the int main() function
    > > rather than void main(). Here is what I don't really appreciate, I
    > > suppose, Why are the majority of authors who are submitting texts that
    > > are approved for tutorials or educational purposes adhering to and
    > > promoting the void main() approach, without even mentioning the int
    > > main() standard???

    >
    > Because they don't know.
    > Because a major company used void main() throught their whole
    > documentation
    > Because those instuctors think that this major company is god and defines
    > the language
    > ...
    >
    > > How should this normally be remedied, by adding a return statement
    > > with int main()? If this is the approach, what is the ususal return
    > > value?

    >
    > for main() there is an exception:
    > You don't need to return anything. In this case the
    > compiler has to insert a
    > return 0;
    > for you. But beware: main() is the only place where you can do
    > this. In every other function with a return value it is prohibited
    > to not actually return something.
    >
    > Other return values are
    >
    > return EXIT_SUCCESS
    > return EXIT_FAILURE
    >
    >
    > BTW: Online tutorial aren't a good way to learn the language. As you have
    > seen, most of them are full of errors of the simplest type and work
    > just by accident. Also: How do you know that the guy who wrote the
    > tutorial knows what he is talking about?
    >
    > Get a good book. See the FAQ for recommendations.
    > http://ma.rtij.nl/acllc-c .FAQ.html


    Hi Karl,

    Thank you for the follow-up. I think that I understand who the
    company is that you are referencing and other matters associated with
    it. I'm certain there are a number of people who are not aware that
    int main() is a viable and more preferable option to use, but when
    associated with the company in some form or fashion, those who are
    aware of int main() may choose to promote the gospel of void main()
    instead.
    I think you are also saying the compiler by default inserts the
    return 0 value. I have manually inserted return 0 when declaring main
    of the int type to see if this was executable on previous occasions
    prior to posts, and wanted to see if a program would execute without
    inserting the return statement with the int main() function. The
    compiler raises a warning but does allow program execution.
    I also used the constants EXIT_SUCCESS and EXIT_FAILURE as return
    values when using the int main() function and was pleased to see the
    execution desired. I will probably go with either of the latter in
    subsequent programs and use the int main() function from this point
    on.
    Also, where are EXIT_SUCCESS and EXIT_FAILURE defined, and on what
    level does the benefit arise when using int main() as opposed to void
    main()? This seems to be a strong point many C - C++ programmers
    continuously raise when discussing the language.
    Lastly, I do not know that the individual who authored the text is
    one of the better concerning this language. I have not found his code
    to be errant until raising the matters stated within these posts
    (perhaps typo error regarding the char* array pointers defined in the
    constructor functions, exlcluding void main()), and he is also
    associated with the course.com Web site, which I think tries to do a
    legitimate service. This particular text, as many associated with this
    Web site, is published by Course Technology of Thomson Learning. It
    may be that there is a strong MS flavor associated with this site that
    I really am not qualified to comment on.

    Again, thanks for your input.
    Anthony
    August1, Sep 15, 2004
    #5
  6. August1

    Greg Comeau Guest

    In article <>,
    August1 <> wrote:
    > Also, where are EXIT_SUCCESS and EXIT_FAILURE defined, and on what
    >level does the benefit arise when using int main() as opposed to void
    >main()? This seems to be a strong point many C - C++ programmers
    >continuously raise when discussing the language.


    Check out http://www.comeaucomputing.com/techtalk/#voidmain
    http://www.comeaucomputing.com/techtalk/#mainexit
    http://www.comeaucomputing.com/techtalk/#maintermination
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Sep 15, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rubikzube*
    Replies:
    4
    Views:
    311
    rubikzube*
    May 23, 2006
  2. polas

    Passing execution to a memory address

    polas, Mar 1, 2007, in forum: C Programming
    Replies:
    15
    Views:
    501
    Mark McIntyre
    Mar 2, 2007
  3. jon wayne
    Replies:
    9
    Views:
    705
    Jim Langston
    Sep 22, 2005
  4. Griff
    Replies:
    3
    Views:
    94
    Kyle Schmitt
    Mar 21, 2007
  5. Aaron Smith
    Replies:
    3
    Views:
    157
    Aaron Smith
    Jul 5, 2007
Loading...

Share This Page