Another question on classes

This is a discussion on Another question on classes within the C++ Programming forums, part of the General Programming Boards category; I had already picked up a couple other books prior to the suggested ones... one of them is an Ivor ...

  1. #16
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    I had already picked up a couple other books prior to the suggested ones... one of them is an Ivor Horton book, but it's the "Beginning Visual C++ 6".. I've consulted it a little bit so far...

  2. #17
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That default constructor seems odd to me...

    Maybe something like:
    Code:
    public:
    matrix();
    then
    Code:
    matrix::matrix()
    {
    r=10;
    c=10;
    v=0;
    }
    then you can define other constructors if the user specifies values
    im not exactly sure how your method would work...if it works
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #18
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Code:
    matrix::matrix()
    {
    r=10;
    c=10;
    v=0;
    }
    That thought had crossed my mind, but I was under the impression that I needed to mirror the way that the default constructor looked in the .h file (which was provided by the book for this problem)?

  4. #19
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    try changing the .h file so that the matrix constructor has no arguments ( matrix() )

    edit:: oh i also mean change the cpp file to what i said before as well
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    JaWib didn't follow the thread very carefully, and he doesn't appear to understand your assignment, and anyways the constuctor is fine. Your constructor has default parameter values, so it plays a dual role. It serves as a constructor that takes three int parameters, and it also serves as a default constructor: if you call the constructor with no arguments, the default values will be used.

    However, the whole issue with your constructor that you've failed to address is that a constructor is used to initialize the data members of your class. Your constructor was declared to accept three parameters of type int, yet your class doesn't have three data members of type int. So, there appears to be some kind of discrepancy.

    "I had already picked up a couple other books prior to the suggested ones... one of them is an Ivor Horton book, but it's the "Beginning Visual C++ 6"

    I don't know if your class is going to be covering Visual C++, and if it's not, I would exchange Beginng Visual C++ for Beginning C++, which is a more comprehensive text on C++. However, they are both good books, and I have them both. The first 12 chapters of Beginning Visual C++ is a condensed version of Beginning C++, so it's more cursory.
    Last edited by 7stud; 05-23-2003 at 01:41 AM.

  6. #21
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Visual C++ is what the course that I am taking requires us to use, so that's why I leaned towards getting the book with Visual C++ 6.0 focus. I read all of chapter 8 in that book last night (where classes are first introduced) and about 1/2 of Chapter 9. I can definitely tell the difference in quality of writing versus the required text for my course. The concepts are starting to crystalize a bit now..

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Visual C++ is both a compiler and a method for Windows programming. You can study C++ and use Visual C++ as your compiler, but never get into Windows Programming with Visual C++, which involves using the Microsoft Foundation Classes(MFC) to create Windows as the means of interacting with the user of your program. For instance, a lot of people don't like MFC, and don't use it for Windows Programming--they use the Windows API directly. I think "Beginning C++" is a much better C++ reference than "Beginning Visual C++", but either one is undoubtedly much better than your current text.
    Last edited by 7stud; 05-23-2003 at 04:37 PM.

  8. #23
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    I had a question regarding this statement:

    "However, the whole issue with your constructor that you've failed to address is that a constructor is used to initialize the data members of your class. Your constructor was declared to accept three parameters of type int, yet your class doesn't have three data members of type int. So, there appears to be some kind of discrepancy"

    The default constructor is the following:

    Code:
    Matrix(int r=10, int c=10, int v=0);
    This is the default constructor provided by the textbook for this problem, but I do see what you're saying (I think). It has three integers in it, but there are not 3 data members in the class to correlate the integers with, right? I am not sure how I am supposed to handle this if that is the case, because we are supposed to use the interface provided in the book (which is basically the matrix.h stuff that I posted before).

    On a good note, I did get the code to compile and display a 1 when I initialize a variable with the class Matrix.. such as:

    Code:
    Matrix A;
    cout << A.NumberRows;
    That code displays a "1" in my console window and I am not sure why. I imagine it has something to do with my difficulties in setting up the default constructor, but I am not sure what I need to do to fix this..

    Thanks!

  9. #24
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The default constructor is the following:
    Matrix(int r=10, int c=10, int v=0);
    Not really. That is a constructor that takes 0, 1, 2, or 3 arguments, but no matter how many arguments you send to the function it ALWAYS has three parameters--or values--that presumably you will do something with in the body of the constructor.

    If you define your constructor like this:

    Matrix::Matrix(int r, int c, int v){

    }

    then why would you declare it with any parameters? Since you aren't doing anything with the values you would declare the constructor like this:

    Matrix();

    and define it like this:

    Matrix::Matrix()
    {
    }

    I am supposed to handle this if that is the case, because we are supposed to use the interface provided in the book
    Exactly. So, that's a big hint your definition of the constructor is totally wrong, and until you get that straightened out, there's no use proceeding. The parameters of your constructor probably have something to do with the data member:

    IntList *Values;

    What's an IntList?
    Last edited by 7stud; 05-24-2003 at 12:27 AM.

  10. #25
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    The IntList is another class that was defined earlier in the book that (I am presuming) was designed with this assignment in mind. It of course has its own .h and .cpp file for it as well, but I don't know how well it would assist matters if I were to paste that here?

  11. #26
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Ok, here's the deal: Your constructor has three int parameters you have to do something with. And your class has these data members:


    int NumRows; // Row size of matrix
    IntList *Values; // pointer to rows

    If you assign one of the parameters to NumRows, that leaves you with two other int parameters that need to be put to use. Values is a pointer to an IntList. So, if you create an IntList object in your constructor and assign the address to Values, then your constructor has taken care of initializing both of your data members. What I'm guessing is that to create an IntList object, you need two int parameters--which would correspond to the two remaining int parameters of your constructor. Make sense?

  12. #27
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    That does make sense... the IntList does take two ints into it so that would make perfect sense... I think that helps..

    Thanks!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 02:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 03:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 03:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 08:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21