Help with C++ on Strings

  • Thread starter yungman
  • Start date
  • #101
4,951
110
In the Debug menu, there is Step Backward, or Alt [.
It's the same idea for a struct. The struct definition is the template for how a struct instance is laid out in memory.
That's the wrong way to think about things. Instead of thinking about the files where the functions are defined, the member functions of a struct are internal to the class. Whether the function definitions appear in the header file or in another .cpp file is really irrelevant.
The header file isn't the class blueprint -- the class definition, class InvItem, in this case -- is the blueprint, or template.
It's different because this struct doesn't have any member functions. It's also different in that the three data members are public.
In C++, the only difference between a struct and a class is that members of a struct are public, by default. That's the only difference. A struct can have member functions, just like a class.
Thanks for the reply.

I don't see Alt[ in debug menu. I see F11 step in, F10 step over and shift-F11 step out.

Yes, I think toward the last part, I realize the .h file is not the blue print, only the invItem is the Class, that is the blue print to create item1, item2 and item3.

I did not realize struct is a blueprint also. So the difference is struct are all PUBLIC . Class is more complicate with private and public. I have not learn struct can have member function yet.

In struct st, you access the member by st.cost, I have not seen in the invItem item1 that I can access item1.cost like in struct. Is this because double cost is PRIVATE. that if I declare double cost in PUBLIC, I can access from program using item1.cost = 6.95? If that is true, this clears up more question for me.

Thanks for explaining this.
 
  • #102
jtbell
Mentor
15,636
3,683
the external structure is not a blue print,
No, it is a blueprint just like a class is. See page 592 of your book, where Gaddis introduces structures by defining a struct data type named PayRoll.
C++:
struct PayRoll
{
    int empNumber; // Employee number
    char name[SIZE]; // Employee's name
    double hours; // Hours worked
    double payRate; // Hourly pay rate
    double grossPay; // Gross pay
};
He then writes:

Gaddis said:
It’s important to note that the structure declaration in our example does not define a variable. It simply tells the compiler what a PayRoll structure is made of. In essence, it creates a new data type named PayRoll. You can define variables of this type with simple definition statements, just as you would with any other data type. For example, the following statement defines a variable named deptHead:

PayRoll deptHead;
After that that statement, there now exist "sub-variables" named deptHead.empNumber, deptHead.name, etc.

If you then have the following statement:

PayRoll teamLead;

you now also have "sub-variables" names teamLead.empNumber, teamLead.name, etc.

If PayRoll were a class instead of a struct, you could do the same two statements, and you would have the same "sub-variables". The only difference here is that you cannot access the "sub-variables" (member data) directly from your program. In a class object, only the member functions can access private member data directly.
 
  • Like
Likes yungman
  • #103
34,158
5,777
I don't see Alt[ in debug menu.
The debugger has to be running for this option to appear.
In struct st, you access the member by st.cost, I have not seen in the invItem item1 that I can access item1.cost like in struct. Is this because double cost is PRIVATE. that if I declare double cost in PUBLIC, I can access from program using item1.cost = 6.95? If that is true, this clears up more question for me.
Yes, if cost were listed among the public members of the class, you could access it directly from an object using the dot operator. This is not a good idea, as it defeats one of the major attributes of OO design, encapsulation -- a class should provide a set of accessors and mutators to limit access to the data members of a class.
 
  • #104
4,951
110
The debugger has to be running for this option to appear.
Yes, if cost were listed among the public members of the class, you could access it directly from an object using the dot operator. This is not a good idea, as it defeats one of the major attributes of OO design, encapsulation -- a class should provide a set of accessors and mutators to limit access to the data members of a class.
Yes, I was running debug by hitting F5 first and stop at the break point before I check. This is the picture. I don't see it.
Step back.jpg


Thanks for clearing the private public variable up. I understand not to put that in public, just want to confirm I can do that if I put it in public and can use dot to access it.

Seems like after the first semester C++, things are getting much more complicate, it really takes time to really understand it. It's not like I can fly over the chapters.

Thanks for your time.
 
  • #105
4,951
110
No, it is a blueprint just like a class is. See page 592 of your book, where Gaddis introduces structures by defining a struct data type named PayRoll.
C++:
struct PayRoll
{
    int empNumber; // Employee number
    char name[SIZE]; // Employee's name
    double hours; // Hours worked
    double payRate; // Hourly pay rate
    double grossPay; // Gross pay
};
He then writes:


After that that statement, there now exist "sub-variables" named deptHead.empNumber, deptHead.name, etc.

If you then have the following statement:

PayRoll teamLead;

you now also have "sub-variables" names teamLead.empNumber, teamLead.name, etc.

If PayRoll were a class instead of a struct, you could do the same two statements, and you would have the same "sub-variables". The only difference here is that you cannot access the "sub-variables" (member data) directly from your program. In a class object, only the member functions can access private member data directly.
Thank you for the explanation. I am getting this.

Thanks
 
  • #106
34,158
5,777
Yes, I was running debug by hitting F5 first and stop at the break point before I check. This is the picture. I don't see it.
Possibly Step Backward is no longer a feature in VS 2019. I'm running VS 2017, and it is there, but only if the debugger is running.
Here's what I see when the debugger is not running.
DbgNotRun.png

Here are the options when the debugger is running.
DbgRunning.png
 
  • #107
4,951
110
Possibly Step Backward is no longer a feature in VS 2019. I'm running VS 2017, and it is there, but only if the debugger is running.
Here's what I see when the debugger is not running.
View attachment 271067
Here are the options when the debugger is running.
View attachment 271068
Thanks

Too bad, ha ha, maybe I should try to get the 2017!!! Believe me, I tried the alt-[ where it shows or not. It just gave me a "bing" sound.

I don't know how your 2017 is on this, my VS does not allow strcpy() and even strncpy(). I have to change the code in the book to strncpy_s() on all of them and I have look to get the length to put in the extra two parameters.

Thanks
 
  • #108
4,951
110
I might have an English problem here. I read this CAREFULLY like 10 times, I cannot make heads and tails out of this. The question is written in the copy. I have no idea what the book refer all the 1st, 2nd, 3rd constructors etc. I copy the two pages in the book. They are continuous from one page to the next.
Constructor.jpg


Constructor1.jpg


I labeled question (1) as 1st, 2nd and 3rd. I labeled questions(2) and (3) as 0, 1 and 2. Please help clarify (1) to (4) for me.

Thanks
 

Attachments

Last edited:
  • #109
jtbell
Mentor
15,636
3,683
For question (1), see Contents of InventoryItem.h (Version 3) on page 759. The "third constructor" is labeled "Constructor #3". It receives three arguments.

The statement that you asked about, is on page 761. It calls that constructor three times, once for each of the three elements of the array named inventory, supplying three arguments each time.

For questions (2) and (3):

The compiler chooses which constructor to use, by comparing the number and types of arguments that you give when calling the constructor, with the number and types of arguments in the constructors in the class definition. inventory[0] and inventory[2] are each constructed with one argument: a C-string. Therefore "Constructor #2" is used (because it receives one argument, a C-string). inventory[1] is constructed with three arguments: a C-string, a double and an int. Therefore "Constructor #3" is used.

For question (4), the "default constructor" is the one that has no arguments, in this case "Constructor #1".
 
Last edited:
  • Like
Likes yungman
  • #110
34,158
5,777
Too bad, ha ha, maybe I should try to get the 2017!!!
Besides the different versions (2015, 2017, 2019, and earlier), there are different editions, such as Community Edition, Enterprise Edition, and one or two more that I don't remember. If you have the (free) Community Edition, maybe its features are limited. I have version 2017, but it's the Enterprise Edition.
Otherwise, I don't know why your version doesn't support Step Backward.
I have no idea what the book refer all the 1st, 2nd, 3rd constructors etc.
@jtbell explained this pretty well, but just in case, the constructors are numbered in the code you showed.
// Constructor #1
InventoryItem() - no parameters

// Constructor #2
InventoryItem(char *description) - a single C-string parameter

// Constructor #3
InventoryItem(char *description, double cost, int units) - three parameters
#2 and #3 use the parameter names that I suggested.
 
  • Like
Likes yungman
  • #111
4,951
110
Thanks guys, I did NOT know it's referring back to the old stuffs. This is a new section Array of Objects, the book should say very specifically it refers back to P759.

Thanks jtbell, it really helps you have the book and gave me the page number. I have to go back and read over with the new understanding. I was pulling hair since last night on this. I was thinking my English cannot be that bad!!!

Hi Mark, my middle name is "CHEAP", I ain't paying no money for the version of VS that cost money!!! No walking back it is!!! 😊

Thanks both jtbell and Mark.
 
  • #112
34,158
5,777
This is a new section Array of Objects, the book should say very specifically it refers back to P759.
No, this isn't reasonable. The topics in this book and most other programming books build on each other, and aren't standalone. What you're asking for is like saying that every use of int should refer back to where this keyword was defined. Eventually the chapters would be mostly references back to previous material, with very few parts devoted to new topics.
 
  • Like
Likes Vanadium 50
  • #113
4,951
110
I am running into problem with VS again. If you look at the invItem.h, if I use line11 to line27. It will FAIL and the error file is shown below. BUT if you block out line 11 to line27, the USE line 29 to line 44. It will work. Look at the two blocks of codes. THEY ARE EXACTLY THE SAME. I compare the two blocks a few times. They are exactly the same.

This is the invItem.h.

C++:
#ifndef invItem_H
#define invItem_H
#include <cstring>   // Needed for strlen and strcpy
#include <iostream>

class invItem
{
  private:
    char* description; const int L = 21;  double cost; int units;
  public:
//This part of the code will give error
    invItem item1()
    { char cA1[] = "Alan";
      description = new char[L];//0 
      strncpy_s(description, L, cA1, L);
      cost = 0.0;   units = 0;}//2

   invItem item2(char *desc)//12
    { description = new char[strlen(desc)+1];//13
      strncpy_s(description, strlen(desc)+1, desc, strlen(desc)+1);//14
      cost = 0.0;   units = 0; }//15

    invItem item3(char* desc, double c, int u)//18
    { description = new char[strlen(desc) + 1];//19
      strncpy_s(description, strlen(desc)+1, desc, strlen(desc)+1);//20
      cost = c; units = u;}//21


/*This is the exact code as above. If you use this part, it will work.
    invItem()   // Constructor #1
    {   char cA1[] = "Alan";
        description = new char[L];
        strncpy_s(description, L, cA1, L);
        cost = 0.0; units = 0;}// 2--Default values

    invItem(char* desc)   //12   Constructor#2 
    {   description = new char[strlen(desc)+1];
        strncpy_s(description, strlen(desc)+1, desc, strlen(desc)+1);
        cost = 0.0; units = 0; }//15---Default value
    invItem(char* desc, double c, int u)//18-Constructor#3,No default value
    {   description = new char[strlen(desc) + 1];
        strncpy_s(description, strlen(desc) + 1, desc, strlen(desc) + 1);
        cost = c; units = u;}//21 --From main()   
This is the end of the working code*/
    ~invItem()  { delete [] description; }//42, 43, 44
//Mutator function only for Constructor#1
    void setDesc(char *d) {strncpy_s(description, L, d, L);};//5--d=cAr1
    void setCost(double c) { cost = c; }//7
    void setUnits(int u) { units = u; }//9
// Accessor functions
    const char *getDesc() const { return description; }    //25, 31, 37  
    double getCost() const { return cost; }//27, 33, 39
    int getUnits() const { return units; }//29, 35, 41
};
#endif


This is the source.cpp
C++:
#include <iostream>
#include <iomanip>
#include <cstring>
#include "invItem.h"
using namespace std;
int main()
{//Create invItem object item1, to Constructor#1, set default values
   invItem item1;
   char cAr1[] = "Hammer";//3
   item1.setDesc(cAr1);//4--to mutator
   item1.setCost(6.95);//6--to mutator
   item1.setUnits(12);//8---to mutator
//Create invItem object item2 with one parameter
   char cAr2[] = "Pliers";//10--input parameter to item2
   invItem item2(cAr2);//11--cost and units by default
//Create invItem object item3 with 3 parameters
   char cAr3[] = "Wrench";//16
   invItem item3(cAr3, 8.75, 20);//17 parameters for item3
   cout << "The following items are in inventory:\n";//22
   cout << setw(20) << setprecision(2) << fixed << showpoint;//23
//Display item1 use item1. to access member functions
   cout << "Description: " << item1.getDesc() << endl;//24
   cout << "Cost: $" << item1.getCost() << endl;//26
   cout << "Units on Hand: " << item1.getUnits() << "\n\n";//28
//Display item2
   cout << "Description: " << item2.getDesc() << endl;//30
   cout << "Cost: $" << item2.getCost() << endl;//32
   cout << "Units on Hand: " << item2.getUnits() << "\n\n";//34
//Display item3
   cout << "Description: " << item3.getDesc() << endl;//36
   cout << "Cost: $" << item3.getCost() << endl;//38
   cout << "Units on Hand: " << item3.getUnits() << "\n\n";//40
   return 0;
}

This is the error message for the block of line11 to line27.
Error 10_15.jpg


Anyone can explain this?
 
  • #114
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
2019 Award
25,402
8,598
  • #116
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
2019 Award
25,402
8,598
Or he could look line by line.

"invItem item1()" is not the same as "invItem()"
 
  • #117
34,158
5,777
Or he could look line by line.

"invItem item1()" is not the same as "invItem()"
And "invItem2(char *desc)" is not the same as "invItem(char *desc)"
And "invItem3(char* desc, double c, int u)" is not the same as "invItem(char* desc, double c, int u)"

@yungman, you have forgotten the most significant thing about overloaded functions.
 
  • #118
4,951
110
Thanks guys, I found it. I deleted the program accidentally and I had to retype the program from my notes and somehow I typed invItem item1(), invItem item2() and invItem item3() in the .h file. When I check, I check the lines after this declaration line and totally missed it.

I was actually working on the next problem, but with this one opened, I accidentally went to the wrong window and deleted the codes and closed the program and lost everything. So I just typed the whole thing back from reading the notes I posed in post #91 really fast without thinking and wrote the declaration of the object item1,2 and 3 in it.

Thanks
 
Last edited:
  • Sad
Likes Vanadium 50
  • #119
34,158
5,777
Since this is no longer about strings, I'm going to close it. If you have a question about classes, please start a new thread.
 
  • Love
Likes Tom.G

Related Threads on Help with C++ on Strings

Replies
5
Views
179
Replies
10
Views
1K
Replies
6
Views
980
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
1
Views
1K
Replies
1
Views
39K
Replies
6
Views
5K
  • Last Post
Replies
19
Views
3K
  • Last Post
Replies
2
Views
614
Top