Solved

C++ difficulties again...

Posted on 1998-11-06
57
444 Views
Last Modified: 2011-09-20
Can someone please help me!!! I'm all confused about the "this" pointer, "new" and overloading functions.... it's a mess!!!  Sorry!!!
I am using MS Visual C++ 5.0 compiler.

Thank you...
appreciative

Here is the project:

class Shape
{
  public:
    void Set_Shape(int);//1 for rect., 2 for square, 3 for                          //triangle
    void Display_Shape();//print current shape on screen
    void Display_Shape_Type();//tells user what is current                                //shape
    void Set_Length(int);
    void Set_Width(int);
    Shape();//default constructor
    Shape(int,int,int);//constructor that sets all 3 param.
    ~Shape();//destructor
  private:
    int *shape,*length,*width;
}
A)  Add to existing class a copy constructor and the         overloaded assignment(=) operator prototypes to the         class, along with any accessor methods required
B)  Write all the method definitions in the Shape.cpp
    NOTE:  if the shape is a triangle or a square, only the     length dimension applies
C)  Write the main that uses the class to test the class
    (no menu)
    1. Create an object called Shape1 (not default                 constructor) with 3 param. (1,5,8)//1=shape,5=length,
                                         //8=width
    2. Create a second object called Shape2 with the copy          constructor
    3. Display Shape1 (with asterisks *)
    4. Display Shape2's shape type (rect.,sqr,tri.)
    5. Set Shape1 to a triangle with a length of 10
    6. Display Shape1 & display Shape2 (no longer the same)
    7. Assign Shape1 to Shape2 (Shape2=Shape1)
    8. Display Shape2's type & display Shape2 (has now             become the same)

Here is my source code up to now:

shape.h file
class Shape
{
  public:
    void Set_Shape(int);//1=rectangle, 2=square, 3=triangle
    void Display_Shape();//displays current shape on screen
    void Display_Shape_Type();//indicates to user what                               //current shape is
    void Set_Length(int);
    void Set_Width(int);

    Shape();//default constructor
    Shape(int,int,int);//constructor which sets all 3                        //parameters
    ~Shape();//destructor
    Shape(const Shape &);//copy constructor prototype
    Shape operator=(const Shape &);//overloaded assignment                                    //operator prototype
    int Get_Shape();
    int Get_Length();
    int Get_Width();
  private:
    int *shape, *length, *width,selection;
};

shape.cpp file
#include <iostream.h>
#include "Shape.h"

Shape::Shape()
{
  shape=new int;
  length=new int;
  width=new int;

  *shape=1;
  *length=5;
  *width=8;
  selection=0;
  userLength=0;
  userWidth=0;
}

Shape::~Shape()
{
  delete shape;
  delete length;
  delete width;
}

Shape::Shape(const Shape &Copy)
{
  shape=new int;
  length=new int;
  width=new int;

  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  *width=Copy.Get_Width();
}

Shape Shape::operator=(const Shape &Copy)
{
  if(this==&Copy)
  {
    return (*this);
  }

    delete shape;
    delete length;
    delete width;

    shape=new int;
    length=new int;
    width=new int;

    *shape=Copy.Get_Shape();
    *length=Copy.Get_Length();
    *width=Copy.Get_Width();

    return (*this);
}

void Set_Shape(int selection)
{
  *shape=selection;
}

void Display_Shape(int userValue)
{
  if (selection==2)
  {
    *shape=selection;
  }
  if (selection==3)
  {
    *shape=selection;
  }
  else
  {
    *shape=*shape;
  }      

  for (*Length=0,*Length<=userLength,*Length++)
  {
    for (*Width=0,*Width<=userWidth,*Width++)
    {
      cout<<"*";
    }
    cout<<endl;
}

void Display_Shape_Type()
{
  if (selection==2)
  {
    *shape=selection;
    cout<<"You have selected a square.";
  }
  if (selection==3)
  {
    *shape=selection;
    cout<<"You have selected a triangle.";
  }
  else
  {
    *shape=*shape;
    cout<<"You have selected a rectangle.";
  }
}

void Set_Length(int userLength)
{
  if (userLength!=0)
  {
    *length=userLength;
  }
}

void Set_Width(int userWidth)
{
  if (userWidth!=0)
  {
    *width=userWidth;
  }
}

shapemain.cpp file
#include <iostream.h>
#include "Shape.h"

void main (void)
{
  int selection,userLength,userWidth,pause;
      
  Shape *shape1;
  shape1=new Shape;
      
  Shape shape1;
  Shape shape2(shape1);

  cout<<"Please select desired shape: "<<endl
      << 1. Rectangle<<endl
      << 2. Square<<endl
      << 3. Triangle<<endl";
      cin>>selection;

  if (selection==1)
  {
    cout<<"Please enter length: ";
    cin>>userLength;
    cout<<"Please enter width: ";
    cin>>userWidth;
  }
  else
  {
    cout<<"Please enter length: ";
    cin>>userLength;
  }
      
  cout<<"Display shape1"<<endl;
  shape1->Display_Shape();

  cout<<"Display shape2's shape type"<<endl;
  shape2->Display_Shape_Type();
      
  cout<<"Set shape1 to a triangle, with a length of       <<10"<<endl;
  shape1->Set_Length(10);
      
  cout<<"Display shape 1 and shape2"<<endl;
  cout<<shape1->Display_Shape();
  cout<<shape2->Display_Shape();
      
  shape2=shape1;

  cout<<"Display shape2's type and display shape2"<<endl;
  cout<<shape2->Display_Shape_Type();
  cout<<shape2->Display_Shape();
}

Compiling errors: (discouraging enough...!!!)

Compiling...
shape.cpp
shape.cpp(14) : error C2065: 'userWidth' : undeclared identifier
shape.cpp(15) : error C2065: 'userLength' : undeclared identifier
shape.cpp(31) : error C2662: 'Get_Shape' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
Conversion loses qualifiers
shape.cpp(32) : error C2662: 'Get_Length' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
Conversion loses qualifiers
shape.cpp(33) : error C2662: 'Get_Width' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
Conversion loses qualifiers
shape.cpp(51) : error C2662: 'Get_Shape' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
Conversion loses qualifiers
shape.cpp(52) : error C2662: 'Get_Length' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
Conversion loses qualifiers
shape.cpp(53) : error C2662: 'Get_Width' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
Conversion loses qualifiers
shape.cpp(60) : error C2065: 'shape' : undeclared identifier
shape.cpp(60) : error C2100: illegal indirection
shape.cpp(65) : error C2065: 'selection' : undeclared identifier
shape.cpp(67) : error C2100: illegal indirection
shape.cpp(71) : error C2100: illegal indirection
shape.cpp(75) : error C2100: illegal indirection
shape.cpp(75) : error C2100: illegal indirection
shape.cpp(78) : error C2065: 'Length' : undeclared identifier
shape.cpp(78) : error C2100: illegal indirection
shape.cpp(78) : error C2100: illegal indirection
shape.cpp(78) : error C2100: illegal indirection
shape.cpp(78) : error C2143: syntax error : missing ';' before ')'
shape.cpp(78) : error C2143: syntax error : missing ';' before ')'
shape.cpp(80) : error C2065: 'Width' : undeclared identifier
shape.cpp(80) : error C2100: illegal indirection
shape.cpp(80) : error C2100: illegal indirection
shape.cpp(80) : error C2100: illegal indirection
shape.cpp(80) : error C2143: syntax error : missing ';' before ')'
shape.cpp(80) : error C2143: syntax error : missing ';' before ')'
shape.cpp(88) : error C2601: Display_Shape_Type' : local function definitions are illegal
shape.cpp(107) : error C2601: 'Set_Length' : local function definitions are illegal
shape.cpp(116) : error C2601: 'Set_Width' : local function definitions are illegal
shape.cpp(124) : fatal error C1004: unexpected end of file found
shapemain.cpp
shapemain.cpp(11) : error C2040: 'shape1' : 'class Shape' differs in levels of indirection from 'class Shape *'
shapemain.cpp(15) : error C2146: syntax error : missing ';' before identifier 'Rectangle'
shapemain.cpp(15) : error C2065: 'Rectangle' : undeclared identifier
shapemain.cpp(16) : error C2297: '<<' : bad right operand
shapemain.cpp(16) : error C2146: syntax error : missing ';' before identifier 'Square'
shapemain.cpp(16) : error C2065: 'Square' : undeclared identifier
shapemain.cpp(17) : error C2297: '<<' : bad right operand
shapemain.cpp(17) : error C2146: syntax error : missing ';' before identifier 'Triangle'
shapemain.cpp(17) : error C2065: 'Triangle' : undeclared identifier
shapemain.cpp(17) : error C2001: newline in constant
shapemain.cpp(17) : error C2297: '<<' : bad right operand
shapemain.cpp(17) : error C2143: syntax error : missing ';' before 'string'
shapemain.cpp(18) : error C2146: syntax error : missing ';' before identifier 'cin'
shapemain.cpp(38) : error C2819: type 'Shape' does not have an overloaded member 'operator ->'
shapemain.cpp(38) : error C2227: left of '->Display_Shape_Type' must point to class/struct/union
shapemain.cpp(46) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
shapemain.cpp(47) : error C2819: type 'Shape' does not have an overloaded member 'operator ->'
shapemain.cpp(47) : error C2227: left of '->Display_Shape' must point to class/struct/union
shapemain.cpp(53) : error C2819: type 'Shape' does not have an overloaded member 'operator ->'
shapemain.cpp(53) : error C2227: left of '->Display_Shape_Type' must point to class/struct/union
shapemain.cpp(54) : error C2819: type 'Shape' does not have an overloaded member 'operator ->'
shapemain.cpp(54) : error C2227: left of '->Display_Shape' must point to class/struct/union
Error executing cl.exe.

shapeAssign.exe - 53 error(s), 0 warning(s)
0
Comment
Question by:appreciative
  • 25
  • 15
  • 13
  • +2
57 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1177145
1.
shape.cpp(14) : error C2065: 'userWidth' : undeclared identifier
shape.cpp(15) : error C2065: 'userLength' : undeclared identifier

Declare these variables as members of the shape class


2.
shape.cpp(31) : error C2662: 'Get_Shape' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
     Conversion loses qualifiers
     shape.cpp(32) : error C2662: 'Get_Length' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
     Conversion loses qualifiers
     shape.cpp(33) : error C2662: 'Get_Width' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
     Conversion loses qualifiers
     shape.cpp(51) : error C2662: 'Get_Shape' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
     Conversion loses qualifiers
     shape.cpp(52) : error C2662: 'Get_Length' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
     Conversion loses qualifiers
     shape.cpp(53) : error C2662: 'Get_Width' : cannot convert 'this' pointer from 'const class Shape' to 'class Shape &'
     Conversion loses qualifiers

Each of these functions needs to be const - meaning they don't change the internal state of the shape, example :-
  int Get_Shape() const ;
-- put the const in both in the class (.h) and the function body (.cpp)

3.
shape.cpp(60) : error C2065: 'shape' : undeclared identifier
     shape.cpp(60) : error C2100: illegal indirection
     shape.cpp(65) : error C2065: 'selection' : undeclared identifier
     shape.cpp(67) : error C2100: illegal indirection
     shape.cpp(71) : error C2100: illegal indirection
     shape.cpp(75) : error C2100: illegal indirection
     shape.cpp(75) : error C2100: illegal indirection
     shape.cpp(78) : error C2065: 'Length' : undeclared identifier
     shape.cpp(78) : error C2100: illegal indirection
     shape.cpp(78) : error C2100: illegal indirection
     shape.cpp(78) : error C2100: illegal indirection
     shape.cpp(78) : error C2143: syntax error : missing ';' before ')'
     shape.cpp(78) : error C2143: syntax error : missing ';' before ')'
     shape.cpp(80) : error C2065: 'Width' : undeclared identifier
     shape.cpp(80) : error C2100: illegal indirection
     shape.cpp(80) : error C2100: illegal indirection
     shape.cpp(80) : error C2100: illegal indirection
     shape.cpp(80) : error C2143: syntax error : missing ';' before ')'
     shape.cpp(80) : error C2143: syntax error : missing ';' before ')'
     shape.cpp(88) : error C2601: Display_Shape_Type' : local function definitions are illegal

All these functions need to be members of the shape class in function bodys (.cpp).  You seem to have missed this out from a lot of function bodies, starting with Set_Shape

void Set_Shape(int selection)

should be

void Shape::Set_Shape(int selection)

You have also missed out a } in the DisplayShape function (look at your 'for' loops)

There may be other typos I haven't spotted.


4.

This is wrong
 Shape *shape1;
       shape1=new Shape;

       Shape shape1;
       Shape shape2(shape1);

because you define shap1 as a pointer to a shape, the create a shape.  Then you redefine shape1 as an actual shape - you can't do this.  Then you create shape2 from shape1.

You probably mean either
// pointers
Shape * shape1 = new shape ;
Shape * shape2 = new shape( *shape1 ) ;

or
// objects
Shape shape1 ;
Shape shape2( shape1  ) ;

When you access class members for thru a pointer use ->,  when you access them thru an object use a dot (.)

5. Finally

shape2=shape1;

This depends on how you solve #4

If pointers you are just copying the ptr (not the object, if you want to copy the object, write
*shape2 = *shape1 ;

If actual objects, you're okay.


6. I may have missed a couple of typos, but you should be able to correct em yourself.  When you get an error - go to the line given in the compiler output, then in VC goto help and type in the error number, e.g. C2001 and it will tell you why you get this error.  Start from the top of the error list and work done, as later compiling errors are sometimes spurious (caused by bad syntax earlier in your source)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177146
Answers2000's last ip (6) is probably the most important.  Ignore ALL the errors--it doesn't matter how many there are--and fix the top one.  If you can, then try to fix the next.  When you come to one you can't fix or that may have been fixed by changes you have already made, then recompile.

Another tyips is to make only small changes to the source.  Start out with an "empty" program that just has main() declared and does nothing.  Make it compile without errors.  Then add a skeleton of one procedure or one class definition  (by skeleton, I mean just the basic declaration without the gorry details).  Make it compile.  Then add some details.  Make it compile.  continue in this way with very small changes.  If you run into errors you know what few lines you have added or changed and what must be the source of the error.
0
 

Author Comment

by:appreciative
ID: 1177147
Hello Answers2000,

Thank you very much for your reply....I worked all night and managed to get down from 53 errors to 14.  I do go in help, as you suggested, and type in the error number.... sometimes it helps and other times I can't quite figure out what they are saying....I have done the corrections you sent and the program has improved immensely...Thanks again...
Can you help me with these compile errors which I do not understand (even by checking the help files)...Many thanks, I'm very grateful to you for sharing your knowledge!!!!
very, very
appreciative

For these first two lines, I get compile errors described below

for (*Length=0;*Length<=userLength;*Length++)
{
    for (*Width=0;*Width<=userWidth;*Width++)
    {
        cout<<"*";
    }
    cout<<endl;

shape.cpp
shape.cpp(84) : error C2065: 'Length' : undeclared identifier
shape.cpp(84) : error C2100: illegal indirection
shape.cpp(84) : error C2100: illegal indirection
shape.cpp(84) : error C2100: illegal indirection
shape.cpp(86) : error C2065: 'Width' : undeclared identifier
shape.cpp(86) : error C2100: illegal indirection
shape.cpp(86) : error C2100: illegal indirection
shape.cpp(86) : error C2100: illegal indirection

for this code, I get compile errors described below

cout<<shape1.Display_Shape();
cout<<shape2.Display_Shape();

shapemain.cpp
shapemain.cpp(40) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
shapemain.cpp(41) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)


for this code, I get compile error described below

*shape2=*shape1;

shapemain.cpp(43) : error C2100: illegal indirection

and for this code (not 1st line but 2nd and 3rd)  I get compile errors described below (same code and errors as above)

cout<<"Display shape2's type and display shape2"<<endl;
cout<<shape2.Display_Shape_Type();
cout<<shape2.Display_Shape();

shapemain.cpp(46) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
shapemain.cpp(47) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
0
 

Author Comment

by:appreciative
ID: 1177148
Hello Nietod,

Thanks for the tips, I just saw you message I was working with what Answers2000 sent.

What you mention in your first paragraph I do, but contrary to the tips of your second paragraph, I tend to throw everything in the program all at once, HOPING it will work well, but it never does..... so that is a good idea, from now on I will build the program, step by step...  Who knows, with the help of you guys....I might just become a "real" programmer, even though sometimes I think it will never happen.... my self worth takes a dive when I work on a program for hours, until daylight, ..... just to see that it doesn't work!!!!  I am, however, determined and persistent....
Thanks
appreciative
0
 

Author Comment

by:appreciative
ID: 1177149
I think I'll go for a quick nap, it's 9:26 am and I haven't slept yet....I'm exhausted...
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177150
C++ is "case sensitive"  that means that it treats lowercase and upperacase characters in a symbol name differently.  When you declare the class you wrote "length" and "width", all in lowercase, but in the "for loop" you capitalized the first letters.  Thus C++ is looking for differnet symbols, one that was declared with the first letters capitalized.  It can't find them, so it reports that "Length" and "Width" are undefined.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177151
Note that if it doesn't know what Length and Width are, it is unable to continue working.  So what your compiler did (and many do) is it assumes that Length and Width are integers (a reasonable guess) and tries to continue along treating them like integers.  This often works well, but not in this case.  Next it says you have an "illegal indiraction", meaning that in "*Length" you tried to derefrence something that is not a pointer.  that is because it is assuming that Length is an integer and you can't apply * to an integer., but once you make it the right case, the problem will dissapear.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177152
In

cout<<shape1.Display_Shape();

it is complaining because Display_Shape() returns a value of type "void" and you cannot pass that value (no value) to the << operator.  I'm not sure how you should fix this one, because I'm not sure what you want get done here.

In

*shape2=*shape1;

you declared shape1 as a pointer to a shape, so you need to use the "*" with that.  But shape2 is not a pointer, so it doesn't need the "*".    Here is another tip.  Make pointers stand out.  I make every pointer variable end with "Ptr", so I would have had "shape1Ptr" but not "shape2Ptr".  It would have made this clearer.

On a related note, why does the class store pointers to length, width, and shape?  that is not necessary and will cause problems with the = operator (unless you take additional steps.)

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1177153
For illegal indirection note the different between -> and . (my original point #4)

I think nietod's comments have covered the rest
0
 

Author Comment

by:appreciative
ID: 1177154
Thank you very much Nietod and Answer2000....
I appreciate your help....words cannot express how much....!!!!!

Nietod, in answer to

On a related note, why does the class store pointers to length, width, and shape?  that is not necessary and will cause problems with the = operator (unless you take additional steps.)

The project class Shape was already written by the teacher, I had to add a copy constructor and  the overloaded assignment operator prototypes along with accessor methods required; then write the main and test all the functions  (as indicated above in my first message).  To be frank, I don't even know if I'm supposed to ask for user input (as I did), since, by the way the project was written, it seems that the functions could be done without user input...!!!!  Verbally, the teacher told us to throw in a "cin>>pause" to make the program pause in between each function testing.... I did not include it because I really don't know what he wants with that....

Answer2000,
I'm supposed to work with objects (sorry, I forgot to mention it in my second message).
The project calls for 1) Create an ojbect called Shape1 (not with default constructor) with 3 parameters (1,5,8) and 2) Create a second ojbect called Shape2 with the copy constructor (as indicated in my first message).

Well I'm down to 2 errors .... great!!!! and am searching to find an answer on  how to correct them.  If you should have any clues, please inform me...  

for
shape2=*shape1;
I still get
shapemain.cpp(43) : error C2100: illegal indirection

for lines 2 and 3 here
cout<<"Display shape2's type and display shape2"<<endl;
cout<<shape2.Display_Shape_Type();
cout<<shape2.Display_Shape();
I still get
shapemain.cpp(46) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

very, very and greatly
appreciative


0
 
LVL 22

Expert Comment

by:nietod
ID: 1177155
For the problems with

cout<<shape2.Display_Shape_Type();
cout<<shape2.Display_Shape();

I would assume these two functions still return type void.  and void can't be outputted with <<.

There are two sort of approaches to this.  Approach one is to write an "access" function that returns information in a return value that information can be printed with <<.  For example

int Shape::GetShapeType()
{
   return SomeshapeTypeValue;
}

   *   *   *
cout << shape1.GetShapeType()


The other approach is to provide "output functions" that do the output.  (These can be overloaded << operators, but lets stick with something basic)  For example.

void Shape::OutputShapeType(ostream &Stream)
{
   Stream << SomeshapeTypeValue;
}

     *    *    *
Shape1.OutputShapeType(cout);
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177156
shape2=*shape1;

should work considering what we've seen in your question.  Is shape1 still a pointer? like it was in the question?  It might be a good idea to post everything again.

Note that the assignment (=) operation will cause problems (it will work, but you will get crashes later) until you write an overloaded assignment operator.
0
 

Author Comment

by:appreciative
ID: 1177157
Nietod,

As requested, here is my recent code....I just noticed that I'm not using my accessor codes, (int Get_Shape() const; int Get_Length () const and int Get_Width() const) probably that would help....  I did write an overloaded assignment operator (please see shape.h)....I hate to take advice and run, but I , unfortunately, must go out for approx. 2 hours.  I will continue working on it upon my return....Thanks again a million for your help!!!  I noticed your name is the first name in the list of experts.... congratulations!!!!  

shape.h
class Shape
{
  public:
    void Set_Shape(int);//1=rectangle, 2=square, 3=triangle
    void Display_Shape();//displays current shape on screen
    void Display_Shape_Type();//indicates to user what current shape is
    void Set_Length(int);
    void Set_Width(int);

    Shape();//default constructor
    Shape(int,int,int);//constructor which sets all 3 parameters
    ~Shape();//destructor
    Shape(const Shape &);//copy constructor prototype
    Shape operator=(const Shape &);//overloaded assignment operator prototype

    int Get_Shape() const;
    int Get_Length() const;
    int Get_Width() const;
  private:
    int *shape, *length, *width,selection,userLength,userWidth;
};

shape.cpp
#include <iostream.h>
#include "Shape.h"

Shape::Shape()
{
  shape=new int;
  length=new int;
  width=new int;

  *shape=1;
  *length=5;
  *width=8;
  selection=0;
  userLength=0;
  userWidth=0;
}

Shape::~Shape()
{
  delete shape;
  delete length;
  delete width;
}

Shape::Shape(const Shape &Copy)
{
  shape=new int;
  length=new int;
  width=new int;
  selection=0;
  userLength=0;
  userWidth=0;

  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  *width=Copy.Get_Width();
}

Shape Shape::operator=(const Shape &Copy)
{
  if(this==&Copy)
  {
    return (*this);
  }

  delete shape;
  delete length;
  delete width;

  shape=new int;
  length=new int;
  width=new int;
  selection=0;
  userLength=0;
  userWidth=0;

  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  *width=Copy.Get_Width();

  return (*this);
}

void Shape::Set_Shape(int selection)
{
  *shape=selection;
}

void Shape::Display_Shape()
{
  if (selection==2)
  {
    *shape=selection;
  }
  if (selection==3)
  {
    *shape=selection;
  }
  else
  {
    *shape=*shape;
  }      

  for (*length=0;*length<=userLength;*length++)
  {
    for (*width=0;*width<=userWidth;*width++)
    {
      cout<<"*";
    }
    cout<<endl;
  }
}

void Shape::Display_Shape_Type()
{
  if (selection==2)
  {
    *shape=selection;
    cout<<"You have selected a square.";
  }
    if (selection==3)
  {
    *shape=selection;
    cout<<"You have selected a triangle.";
  }
  else
  {
    *shape=*shape;
    cout<<"You have selected a rectangle.";
  }
}

void Shape::Set_Length(int userLength)
{
  if (userLength!=0)
  {
    *length=userLength;
  }
}

void Shape::Set_Width(int userWidth)
{
  if (userWidth!=0)
  {
    *width=userWidth;
  }
}

shapemain.cpp
#include <iostream.h>
#include "Shape.h"

void main (void)
{
  int selection,userLength,userWidth;
      
  Shape shape1(1,5,8);
  Shape shape2(shape1);

  cout<<endl<<"Please select desired shape: "<<endl;
  cout<<"1. Rectangle"<<endl;
  cout<<"2. Square"<<endl;
  cout<<"3. Triangle"<<endl;
  cin>>selection;

  if (selection==1)
  {
    cout<<endl<<"Please enter length: "<<endl;
    cin>>userLength;
    cout<<endl<<"Please enter width: "<<endl;
    cin>>userWidth;
  }
  else
  {
    cout<<endl<<"Please enter length: "<<endl;
    cin>>userLength;
  }
    cout<<endl<<"Display shape1"<<endl;
    shape1.Display_Shape();
            
    cout<<endl<<"Display shape2's shape type (rectangle, square, triangle)"<<endl;
    shape2.Display_Shape_Type();
      
    cout<<"Set shape 1 to a triangle, with a length of 10"<<endl;
    shape1.Set_Length(10);
      
    cout<<"Display shape 1 and shape2"<<endl;
    cout<<shape1.Display_Shape();
    cout<<shape2.Display_Shape();
      
    shape2=*shape1;

    cout<<"Display shape2's type and display shape2"<<endl;
    cout<<shape2.Display_Shape_Type();
    cout<<shape2.Display_Shape();
}


0
 

Author Comment

by:appreciative
ID: 1177158
still working on trying to figure out above errors.... no luck....yet !!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177159
I don't have much time, but some observations

1. operator = should return a const reference.  that is "const shape &".
2. shape1 is not a pointer this time.  So drop the * in the "shape2 = *shape1"
3. for the cout stuff see my recomendations above.

I have to go, but answers2000 is ussually around at night.  I'll be back tommorrow at 6ish, central time.
0
 

Author Comment

by:appreciative
ID: 1177160
Thanks Nietod,

I took out the * and it corrected the error.... I will try and fix the rest later tonight with your suggestions above, if I can keep awake long enough... if not I'll continue tomorrow.

Once again thanks a mil., very kind.... have a good night...

appreciative
0
 

Author Comment

by:appreciative
ID: 1177161
I have managed to correct the error

shapemain.cpp(46) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

by applying your suggestion, Nietod, but now those 3 function definitions give me another error

shape.cpp(73) : error C2511: 'Get_Shape' : overloaded member function not found in 'Shape'
shape.cpp(104) : error C2511: 'Get_Shape_Type' : overloaded member function not found in 'Shape'
shape.cpp(138) : error C2511: 'Get_Length' : overloaded member function not found in 'Shape'

I tried to go to help for this error, but it said to insert disc (which I don't have) ....


I've added a variable named 'type' to my shape.h and shape.cpp and modified
my shape.cpp, here is the latest version... would you know what the error means...???   Sorry to bother you again....thanks...
appreciative

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

Shape::Shape()
{
  shape=new int;
  length=new int;
  width=new int;

  *shape=1;
  *length=5;
  *width=8;
  selection=0;
  type=0;
  userLength=0;
  userWidth=0;
}

Shape::~Shape()
{
  delete shape;
  delete length;
  delete width;
}

Shape::Shape(const Shape &Copy)
{
  shape=new int;
  length=new int;
  width=new int;
  selection=0;
  type=0;
  userLength=0;
  userWidth=0;

  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  type=Copy.Get_Shape_Type();
}

Shape Shape::operator=(const Shape &Copy)
{
  if(this==&Copy)
  {
    return (*this);
  }

  delete shape;
  delete length;
  delete width;

  shape=new int;
  length=new int;
  width=new int;
  selection=0;
  type=0;
  userLength=0;
  userWidth=0;

  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  type=Copy.Get_Shape_Type();
      
  return (*this);
}

void Shape::Set_Shape(int selection)
{
  *shape=selection;
}

int Shape::Get_Shape()
{
  if (selection==2)
  {
    *shape=selection;
  }
  if (selection==3)
  {
    *shape=selection;
  }
  else
  {
    *shape=*shape;
  }      

  for (*length=0;*length<=userLength;*length++)
  {
    for (*width=0;*width<=userWidth;*width++)
    {
      cout<<"*";
    }
    cout<<endl;
  }
  return *shape;
}

void Shape::Display_Shape()
{
  *shape=Get_Shape();      
}

int Shape::Get_Shape_Type()
{
  if (selection==2)
  {
    *shape=selection;
    cout<<"You have selected a square.";
  }
  if (selection==3)
  {
    *shape=selection;
    cout<<"You have selected a triangle.";
  }
  else
  {
    *shape=*shape;
    cout<<"You have selected a rectangle.";
  }
  return *shape;
}

void Shape::Display_Shape_Type()
{
  type=Get_Shape_Type();
}

void Shape::Set_Length(int userLength)
{
  if (userLength!=0)
  {
    *length=userLength;
  }
}

int Shape::Get_Length()
{
  return *length;
}

void Shape::Set_Width(int userWidth)
{
  if (userWidth!=0)
  {
    *width=userWidth;
  }
}


0
 
LVL 22

Expert Comment

by:nietod
ID: 1177162
You haven't declared those 3 functions in the shape class.

What those functions are doining however seems wierd.  I'm not sure what the goal is, but I suspect that something is wrong there.
0
 

Author Comment

by:appreciative
ID: 1177163
Hi Nietod,

Thanks for replying.  Those functions are already in my Shape.h... and what those functions are supposed to do is:

3. Display Shape1 (with asterisks *)
4. Display Shape2's shape type (rect.,sqr,tri.)
5. Set Shape1 to a triangle with a length of 10
6. Display Shape1 & display Shape2 (no longer the same)
7. Assign Shape1 to Shape2 (Shape2=Shape1)
8. Display Shape2's type & display Shape2 (has now become the same)

I don't know... it probably won't work anyways, even if I ever get a "0 errors" message.  I thought my expressions/statements in my functions "seemed" right, but I can't see the result.....

thanks
appreciative

Thanks
appreciative
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177164
>>Those functions are already in my Shape.h

They probalby aren't declared exactly the same there.  They must have the same parameters and return values in both places.

Lets look at Get_Shape().  I would think its purpose is to return a value that indicates the type of shape, right?  It does that, but also other stuff.  It alters the shape value it is supposed to return (why?)  and it prints out the shape (Probably a bad idea, ib you need to print the shape.  Provide another function to do it).  Each function should accomplish just one goal.
0
 

Author Comment

by:appreciative
ID: 1177165
it is declared in my Shape.h as:
 
int Get_Shape() const;

and it is supposed to send the graphic in askerisks (*) to the main so that when I call the Display_Shape function, it displays the graphic (rect., sqr or tri.) on screen.

the Display_Shape_Type is supposed to only say if it is a rect., sqr or tri.

I'm all mixed up!!!!

I'll leave it for a bit...clear my head....

thanks
appreciative
0
 

Author Comment

by:appreciative
ID: 1177166
Not enough information....still need help... the program is not functionning....Thank you!
0
 

Author Comment

by:appreciative
ID: 1177167
Can someone please help me, I've been modifying my program ... no luck... it's still not working properly.... Thank you!!!
appreciative  
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177168
Wowie zowie!!  I'd love to help, but then I would have to read this whole thread!! :)
0
 

Author Comment

by:appreciative
ID: 1177169
It's not that long......pleeeaase!!!!  
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177170
Maybe you could post all the code you have now, and ask what questions you have, and I will see what I can do.
0
 

Author Comment

by:appreciative
ID: 1177171
Hi Scrapdog... spent most of my weekend on this one....
Here is my recent code....
It compiles, displays the menu and also displays the rest of the "couts" in my main (when it should not), we were told to do a "cin>>pause;" to have a pause between functions calls in the main, I didn't put it in, I don't know what he wants with that....
What the program is supposed to do in written in my first message wwwwaaaaayyyyy up above...  I think my expressions/statements are not right in my functions....  
Can you help???
Thanks...
appreciative

Shape.h
class Shape
{
  public:
    void Set_Shape(int);//1=rectangle, 2=square, 3=triangle
    void Display_Shape();//displays current shape on screen
    void Display_Shape_Type();//indicates to user what current shape is
    void Set_Length(int);
    void Set_Width(int);

    Shape();//default constructor
    Shape(int,int,int);//constructor which sets all 3 parameters
    ~Shape();//destructor
    Shape(const Shape &);//copy constructor prototype
    Shape operator=(const Shape &);//overloaded assignment operator prototype

    int Get_Shape() const;
    int Get_Length() const;
    int Get_Width() const;
    int Get_Shape_Type() const;
  private:
    int *shape,*length,*width,type,selection,userLength,userWidth;
};

Shape.cpp
#include <iostream.h>
#include "Shape.h"

Shape::Shape()
{
  shape=new int;
  length=new int;
  width=new int;

  *shape=1;
  *length=5;
  *width=8;
  selection=0;
  type=0;
  userLength=0;
  userWidth=0;
}

Shape::Shape(int,int,int)
{
  shape=new int;
  length=new int;
  width=new int;
  *shape=1;
  *length=5;
  *width=8;
  selection=0;
  type=0;
  userLength=0;
  userWidth=0;
}

Shape::~Shape()
{
  delete shape;
  delete length;
  delete width;
}

Shape::Shape(const Shape &Copy)
{
  shape=new int;
  length=new int;
  width=new int;
  selection=0;
  type=0;
  userLength=0;
  userWidth=0;

  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  *width=Copy.Get_Width();
  type=Copy.Get_Shape_Type();
}

Shape Shape::operator=(const Shape &Copy)
{
  if(this==&Copy)
  {
    return (*this);
  }

  delete shape;
  delete length;
  delete width;

  shape=new int;
  length=new int;
  width=new int;
  selection=0;
  type=0;
  userLength=0;
  userWidth=0;

  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  *width=Copy.Get_Width();
  type=Copy.Get_Shape_Type();
      
  return (*this);
}

void Shape::Set_Shape(int selection)
{
  *shape=selection;
}

int Shape::Get_Shape() const
{
  if (selection==2)
  {
    *shape=selection;
  }
  if (selection==3)
  {
  *shape=selection;
  }
  else
  {
  *shape=*shape;
  }      
      
  int length=0;
  int width=0;

  for (length=1;length==userLength;length++)
  {
    for (width=1;width==userWidth;width++)
    {
      cout<<"*";
    }
    cout<<endl;
  }
  return *shape;
}

void Shape::Display_Shape()
{
  *shape=Get_Shape();
}

int Shape::Get_Shape_Type() const
{
  if (selection==2)
  {
    *shape=selection;
    cout<<"You have selected a square.";
  }
  if (selection==3)
  {
    *shape=selection;
    cout<<"You have selected a triangle.";
  }
  if (selection==1)
  {
    *shape=selection;
    cout<<"You have selected a rectangle.";
  }
  return *shape;
}

void Shape::Display_Shape_Type()
{
  type=Get_Shape_Type();
}

void Shape::Set_Length(int userLength)
{
  if (userLength!=0)
  {
    *length=userLength;
  }
}

int Shape::Get_Length() const
{
  return *length;
}

void Shape::Set_Width(int userWidth)
{
  if (userWidth!=0)
  {
    *width=userWidth;
  }
}

int Shape::Get_Width() const
{
  return *width;
}

shapemain.cpp
#include <iostream.h>
#include "Shape.h"

void main (void)
{
  int selection,userLength,userWidth;
      
  Shape shape1(1,5,8);
  Shape shape2(shape1);

  cout<<endl<<"Please select desired shape: "<<endl;
  cout<<"1. Rectangle"<<endl;
  cout<<"2. Square"<<endl;
  cout<<"3. Triangle"<<endl;
  cin>>selection;

  if (selection==1)
  {
    cout<<endl<<"Please enter length: "<<endl;
    cin>>userLength;
    cout<<endl<<"Please enter width: "<<endl;
    cin>>userWidth;
  }
  else
  {
    cout<<endl<<"Please enter length: "<<endl;
    cin>>userLength;
  }
      
    cout<<endl<<"Display shape1"<<endl;
    shape1.Get_Shape();
            
    cout<<endl<<"Display shape2's shape type (rectangle, square, triangle)"<<endl;
    shape2.Get_Shape_Type();
      
    cout<<"Set shape 1 to a triangle, with a length of 10"<<endl;
    shape1.Set_Length(10);
      
    cout<<"Display shape 1 and shape2"<<endl;
    cout<<shape1.Get_Shape();
    cout<<shape2.Get_Shape();
      
    shape2=shape1;

    cout<<"Display shape2's type and display shape2"<<endl;
    cout<<shape2.Get_Shape_Type();
    cout<<shape2.Get_Shape();
}
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177172
It appears that you are not using userLength and userWith in any way.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:scrapdog
ID: 1177173
Do you have to actually DRAW the shapes?
How come the triangle is defined only by length?  Shouldn't it be base and height?
0
 

Author Comment

by:appreciative
ID: 1177174
I know that when I ask user input for length and width, it does not register it....
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177175
the "type" variable seems to do nothing...what did you mean it to do?

Doesn't "shape" contain the shape's type?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177176
You have included userWidth and userLength in your class declaration.  These are not the same as the userWidth and userLength you declared in main.  You are setting userWidth and userLength in main, and then you aren't doing anything with them...
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177177
The same goes for "selection".  It looks like your program needs an overhaul!!
0
 

Author Comment

by:appreciative
ID: 1177178
Yes I know that you get the triangle by base*height/2
but he means that a triangle is half of a rectangle or a square, see his note:
>> NOTE:  if the shape is a triangle or a square, only the length dimension applies
it's complicated.....
Yes, I have to draw (display) the shapes with asterisks (*)....
 
0
 

Author Comment

by:appreciative
ID: 1177179
I dont' know....I thought I had to declare userLength & userWidth in both the class and the main, but I only wanted to use it in the main....

shape is not supposed to contain the shape type, I have to Display_Shape in one place and then Display_Shape_Type in another... the type is if the user indicates "selection  2", it should advise the user that he chose a square.... from what I can see!!

I thought I was on the right track... and maybe not too far from a working program....sorry if it's a mess... and please don't feel obligated... I know it's very late and you have school tomorrow.... I understand ....
Thanks
appreciative  
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177180
>and maybe not too far from a working program

It is very far from a working program :)

Please be patient, I'll see what I can do.
0
 

Author Comment

by:appreciative
ID: 1177181
we were just handed out this assignment on thursday afternoon .... just scribbled on a piece of paper... and we did not get too much theory... sometimes I feel as if it's a "teach yourself course"....
0
 

Author Comment

by:appreciative
ID: 1177182
T H A N K S  Scrapdog! ! ! !
At least patience is one of my virtues...
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177183
Here is an updated version.  I trimmed out all of the unnecessaries.

#include <iostream.h>

//Shape.h
class Shape
{
  public:
    void Set_Shape(int);//1=rectangle, 2=square, 3=triangle
    void Display_Shape();//displays current shape on screen
    void Display_Shape_Type();//indicates to user what current shape is
    void Set_Length(int);
    void Set_Width(int);

    Shape();//default constructor
    Shape(int,int,int);//constructor which sets all 3 parameters
    ~Shape();//destructor
    Shape(const Shape &);//copy constructor prototype
    Shape operator=(const Shape &);//overloaded assignment operator prototype

    int Get_Shape() const;
    int Get_Length() const;
    int Get_Width() const;
    int Get_Shape_Type() const;
  private:
    int *shape,*length,*width,type;
};

//Shape.cpp
//#include <iostream.h>
//#include "Shape.h"

Shape::Shape()
{
  shape=new int;
  length=new int;
  width=new int;

  *shape=1;
  *length=5;
  *width=8;
}

Shape::Shape(int a,int b,int c)
{
  shape=new int;
  length=new int;
  width=new int;
  *shape=a;
  *length=b;
  *width=c;
}

Shape::~Shape()
{
  delete shape;
  delete length;
  delete width;
}

Shape::Shape(const Shape &Copy)
{
  shape=new int;
  length=new int;
  width=new int;
  *shape=Copy.Get_Shape();
  *length=Copy.Get_Length();
  *width=Copy.Get_Width();
  type=Copy.Get_Shape_Type();
}

Shape Shape::operator=(const Shape &Copy)
{
  if(this==&Copy)
  {
    return (*this);
  }
  else
  {
      *shape=Copy.Get_Shape();
    *length=Copy.Get_Length();
    *width=Copy.Get_Width();
 
  return (*this);
  }
}

void Shape::Set_Shape(int selection)
{
  *shape=selection;
}

int Shape::Get_Shape() const
{
  return *shape;
}

void Shape::Display_Shape()
{
  int i;
  int j;

  for (i=1;i<=*length;i++)
  {
    for (j=1;j<=*width;j++)
    {
      cout<<"*";
    }
    cout<<endl;
  }
}

int Shape::Get_Shape_Type() const
{
  if (*shape==2)
  {
    cout<<"You have selected a square.";
  }
  if (*shape==3)
  {
    cout<<"You have selected a triangle.";
  }
  if (*shape==1)
  {
    cout<<"You have selected a rectangle.";
  }
  return *shape;
}

void Shape::Display_Shape_Type()
{
  type=Get_Shape_Type();
}

void Shape::Set_Length(int userLength)
{
  if (userLength!=0)
  {
    *length=userLength;
  }
}

int Shape::Get_Length() const
{
  return *length;
}

void Shape::Set_Width(int userWidth)
{
  if (userWidth!=0)
  {
    *width=userWidth;
  }
}

int Shape::Get_Width() const
{
  return *width;
}

//shapemain.cpp
//#include <iostream.h>
//#include "Shape.h"

void main (void)
{
  int selection,userLength,userWidth;

  Shape shape1(1,5,8);
  Shape shape2(shape1);

  cout<<endl<<"Please select desired shape: "<<endl;
  cout<<"1. Rectangle"<<endl;
  cout<<"2. Square"<<endl;
  cout<<"3. Triangle"<<endl;
  cin>>selection;

  if (selection==1)
  {
    shape1.Set_Shape(1);
    cout<<endl<<"Please enter length: "<<endl;
    cin>>userLength;
      shape1.Set_Length(userLength);
    cout<<endl<<"Please enter width: "<<endl;
    cin>>userWidth;
      shape1.Set_Width(userWidth);
  }
  else
  {
      shape1.Set_Shape(selection);  
    cout<<endl<<"Please enter length: "<<endl;
    cin>>userLength;
      shape1.Set_Length(userLength);
  }

    cout<<endl<<"Display shape1"<<endl;
    shape1.Display_Shape();

    cout<<endl<<"Display shape2's shape type (rectangle, square, triangle)"<<endl;
    shape2.Get_Shape_Type();

    cout<<"Set shape 1 to a triangle, with a length of 10"<<endl;
    shape1.Set_Length(10);

    cout<<"Display shape 1 and shape2"<<endl;
    cout<<shape1.Get_Shape();
    cout<<shape2.Get_Shape();

    shape2=shape1;

    cout<<"Display shape2's type and display shape2"<<endl;
    cout<<shape2.Get_Shape_Type();
    cout<<shape2.Get_Shape();
}


I don't understand exactly what you are trying to do in main, but the functions now work.  You will need to add some more functionality to your Display_Shape function.

Let me know of any more questions.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177184
Wait a sec, I forgot to remove "type".  Type doesn't do anything, so remove any reference to it.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177185
In all three questions you have asked here at experts exchange, it seemed that the underlying problem in your programs had to do with scope of variables.  I don't think it is a very good idea to jump into object-oriented programming concepts without a complete understanding of scope and lifetime of variables.  Therefore I would suggest that you read the chapter in your textbook on scope...(any halfway decent C programming book should have a chapter devoted totally to scope and lifetime of variables).  Otherwise, trying to understand OO concepts will confuse you a lot more!!  I don't want it to seem like I am lecturing, but trust me, it will only get worse unless you understand this concept!!  I would also suggest that, after you have read and understood the chapter, that you compare the source code you gave me with the source code I gave you, and see what changes I made and why...it will make it easier for you in the long run.  If there was one suggestion for me to make, this would be it.

Other than that, if there is anything in the code I gave you that you don't understand, let me know.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177186
And if I recall correctly, you said you don't even have a textbook, so its not your fault!! :)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177187
Well, its 2 AM.  Time for bed!!

You get some sleep too.  :)
0
 

Author Comment

by:appreciative
ID: 1177188
Thanks a lot Scrapdog....
the program does not seem to work, once I ask for user length and enter a number then ask for width and enter a number, it then gives a "press any key to continue"...

I do know about the scope of a variable, that when its out of scope (function ended) it's out of memory.... what I'm having problems with is the class and whether the functions go and get the variables in the class...

I have 2 textbooks... I read, but I understand more with someone explaining the concepts (like a teacher...)!!!!  but the course I'm taking is very concentrated in a very short period of time - 10 months  (I'm also taking assembler, unix, access/SQL, PC repairs and, unfortunately, excel, powerpoint and word and I find myself swamped with assignments.... -- after Xmas comes Visual Basics, Java, Cobol, html (which I already know.... a break) and whatever else.... so 10 months is pretty condensed for such profound subjects... don't you think???)

Thanks a million,
I'll continue trying to figure out what went wrong tomorrow, no school, Rememberance Day..
totally and always
appreciative      
0
 

Author Comment

by:appreciative
ID: 1177189
I'm not a lazy person, I put in the hours, trust me..... but I just don't have time to revise what I learn... so when we learn something new, the next day it's some other new thing and it piles up without us having time to practice.... I sometimes work all night until 9:00 or 10:00 the next morning, but still it's not enough and would you believe that I'm one of the best in the class.... my marks are very good, but it's difficult... but hey I love programming (if it would only enter my brain), maybe I'm not cut out for this.... but I'll die trying before I quit... hard headed :)
well I guess I'm tired too... off to bed!!!
We seem to have 1 hour difference, it's now 3:45 here (I guess it was 3:00) when you last wrote.
I hope you're doing fine in your studies, I'm not worried though, you probably rack in A's by the barrel...
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177190
I can't tell if this question is done with yet or not.  If not, I'm back.  but the question is getting long and slow to load.  You could e-mail me the code/questions at nietod@theshop.net.

By the way, I do think answers2000 did a good job and you might want to reconsider your rejecting of his answer.  I provided additional help only because I was available and he wasn't (necessarily) available.
0
 

Author Comment

by:appreciative
ID: 1177191
not done yet....
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177192
I'd appreciate it if you'd e-mail me future corespondance, rather than post here.  I have a slow connection and this page takes too long to load.    I'll assume that future posts here are not directed to me, so I probably won't read them  (Or at least wait until a bunch have collected).  I'm headed for bed now, but I'll be back about 6 central time.  I sent you 2 e-mails, they should keep you bussy.  When is this due?
0
 
LVL 1

Expert Comment

by:pmteixeira
ID: 1177193
Hi appreciative,

I read your question and all the comments above, and after almost 40 pages, I think I must say something about it.

I send it as a comment (although I always try to send an answer - for the points, you know), because in this case everyone helped a lot and I think I shouldn't do it.

First of all, I agree with everything that the other experts said or advised you to do!

Second, I think that you (please, don't get me wrong) should rest at least a couple of hours (for example, sleeping or with some kind of sport), forgetting all about your course.
Keep this in mind, for now and for the future...

I also think that you should read, carefully, a good book about C, first, and then about C++.

Having said this, I send you the code I would do for your assignment. It is simple, and it is not better or worst then the other pieces that were already sent, just a bit different.

But please, be calm when you will try to understand it. Then compare it with other versions, and read the comments to see the differences. It works and does exactly (I think) what your teacher intended (NOTE: Never try to do more than what is asked. If it goes wrong, it's a hundred times worst than showing just what is wanted).

OK, now the code (sorry, but the indentation will be a bit 'messy' :

----------------------------------------------------------------
//Shape.h
class Shape
{
  public:
      // Constructors
    Shape();                                                            //default constructor
    Shape(int aShapeType, int aLength, int aWidth); //constructor which sets all 3 parameters
    Shape(const Shape & anotherShape);                        //copy constructor prototype

      // Destructor
    ~Shape();                                                            //destructor

      // Assignment
    const Shape & operator=(const Shape & anotherShape);      //overloaded assignment operator prototype

      // Manipulation
      void Set_Shape(int aShapeType);                              //1=rectangle, 2=square, 3=triangle
    void Set_Length(int aLength);
    void Set_Width(int aWidth);

      // Access
    int Get_Shape() const;
    int Get_Length() const;
    int Get_Width() const;

      // Other
    void Display_Shape() const;                                    //displays current shape on screen
    void Display_Shape_Type() const;                        //indicates to user what current shape is


  private:
    int *shape, *length, *width;
};

-------------------------------------------------------------

//Shape.cpp
#include <iostream.h>
#include "Shape.h"

Shape::Shape()
{
      shape = new int;      // don't generate values in the default constructor,
      length = new int;      // usually that's not intended
      width = new int;
}

Shape::Shape(int aShapeType, int aLength, int aWidth)
{
      shape = new int( aShapeType);      // you may do this in 2 steps like you did
      length = new int( aLength );      // or this way (just for you to know)
      width = new int( aWidth );
}

Shape::Shape(const Shape & anotherShape)
{
      shape = new int;
      length = new int;
      width = new int;
      *shape = anotherShape.Get_Shape();
      *length = anotherShape.Get_Length();
      *width = anotherShape.Get_Width();
}

Shape::~Shape()
{
      delete shape;
      delete length;
      delete width;
}

const Shape & Shape::operator=(const Shape & anotherShape)
{
      // you may want to keep the comparison with "this"
      // but it is not a usual test and just adds confusion
      // to anyone who reads the code!
      //
      // Anyway, if you want, keep this way:
      //
      // -------------------------------------------------
      // if ( this != &anotherShape )
      // {
      //            ... code ahead (3 assignments) ...
      // }
      // return (*this);
      // -------------------------------------------------
      //

      *shape = anotherShape.Get_Shape();
    *length = anotherShape.Get_Length();
    *width = anotherShape.Get_Width();
 
      return (*this);
}

void Shape::Set_Shape(int aShapeType)
{
      if (aShapeType >= 1 && aShapeType <= 3)            // using just valid types
            *shape = aShapeType;
}

void Shape::Set_Length(int aLength)
{
      if (aLength > 0)                                          // not allowing negative lengths
            *length = aLength;
}

void Shape::Set_Width(int aWidth)
{
      if (aWidth > 0)                                                // not allowing negative widths
            *width = aWidth;
}

int Shape::Get_Shape() const
{
      return *shape;
}

int Shape::Get_Length() const
{
      return *length;
}

int Shape::Get_Width() const
{
      return *width;
}

void Shape::Display_Shape() const
                              // you should use the 'const' in the end, everytime
                              // your method doesn't change any of the attributes
                              // (variables) of your class (here, shape, length and width).
                              // Obviously, you can't make any assignments to those attributes
                              // in this functions.
{
      int i, j, displayWidth;

      for (i = 1; i <= *length; i++)
      {
            switch (*shape)                              // sometimes it's easier to read a Switch statement
            {                                                // than a multiple If.
                  case 1:      displayWidth = *width; break;
                  case 2:      displayWidth = *length; break;
                  case 3: displayWidth = i; break;            // the triangle is 1 '*' in the first line,
            }                                                                  // 2 in the second, etc.
            for (j = 1; j <= displayWidth; j++)
                  cout<<"*";
          cout<<endl;
      }
      cout << endl << endl;
}

void Shape::Display_Shape_Type() const
{
      switch (*shape)
      {
            case 1:      cout<<"You have selected a rectangle."; break;
            case 2:      cout<<"You have selected a square."; break;
            case 3: cout<<"You have selected a triangle."; break;
      }
      cout << endl << endl;
}

---------------------------------------------------------------

//shapemain.cpp
#include <iostream.h>
#include "Shape.h"

void main( void )
{
      char key;

      cout << "1. Create an object called Shape1 with 3 param (1, 5, 8)" << endl;
      Shape Shape1(1, 5, 8);
      cin >> key;        // you have to press a character key
                           // and then the ENTER key.

      cout << "2. Create a second object called Shape2 with the copy constructor" << endl;
      Shape Shape2(Shape1);
      cin >> key;

      cout << "3. Display Shape1" << endl;
      Shape1.Display_Shape();
      cin >> key;

      cout << "4. Display Shape2's shape type" << endl;
      Shape2.Display_Shape_Type();
      cin >> key;

      cout << "5. Set Shape1 to a triangle with a length of 10" << endl;
      Shape1.Set_Shape(3);                        // triangle
      Shape1.Set_Length(10);
      cin >> key;

      cout << "6. Display Shape1 and Shape2" << endl;
      Shape1.Display_Shape();
      Shape2.Display_Shape();
      cin >> key;

      cout << "7. Assign Shape1 to Shape2" << endl;
      Shape2 = Shape1;
      cin >> key;

      cout << "8. Display Shape2's type and display Shape2" << endl;
      Shape2.Display_Shape_Type();
      Shape2.Display_Shape();
      cin >> key;
}

0
 

Author Comment

by:appreciative
ID: 1177194
Hi pmteixeira,

Thanks for your help, the program works perfectly.... and I am happy to see another way of coding it... it helps me understand more, compare the differences and see my mistakes.  Now I see that I did not even have to ask for user input.... and I will study it more when I'm a little less tired and calm, as you suggested.

I spent 4 days trying to figure it out, and, although I had help from expert, my program was still not functioning, until you sent your version.

Words can't begin to express my gratitude toward you taking the time to read all those pages and deciding to help me... I am in awe to see that you could fix it in one message, while I had to correspond many times with other experts... and my program still did not function properly... don't get me wrong here, I'm very happy that they were all there for me, I had great tips (including yours) and am very, very grateful to all of them but the fact remains that my program was not functioning...

Thanks+++++
extremely,
appreciative

Now I just don't know what to do with the points....!!!  Personally, I think that  the person who sticks with you until your program works is the one who should get the points.... It is enormously appreciated to get help from experts, but when your program is still not working and you are still racking your brains out trying to figure a way of making it work, and most of the time without results.... it's an incomplete help... and you still need help!!!

Once again, thank you very much!!!


 

     
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177195
>> I am in awe to see that you could fix it in one message, while I
>> had to correspond many times with other experts... and my program
>> still did not function properly
That is because we are trying to HELP you to write it yourself.  We did not want to provide an answer.  That is unethical and grounds for removal from EE.  Although you did a majority of the work, I would still say that pmteixeira's answer is over the line.
0
 
LVL 1

Expert Comment

by:pmteixeira
ID: 1177196
Hi nietod,

I'm sorry if my comment was misunderstood, the point was not to code for someone, but to show another way of doing it. The other version(s), like you said, was almost ready.

Anyway, for me it's much more important someone's HEALTH and CAPACITY OF ANALYSIS (I followed this question since the beginning of the weekend), and HELP him the best way I can (even if sometimes it would mean doing all the work). If this is unethical and grounds for removal (I didn't thought it that way), ok! Not my rules!

To finish I'd like to say this: suppose you have a project to develop and you must use some components you don't know nothing about. You may go to the internet and search, and compare and test and correct, or you may ask for help, as complete as it can be, preferably with examples and used/tested code that you may just use or reuse.
The first option will fill your ego but someone will always ask you why did you spend all that time (or it can go worse). The second is not so fulfilling for yourself as the first, but that someone will say 'Congratulations, all went perfect (meaning, in time)'.

This is my point of view, sorry if I did something with which you don't agree, nietod.

Sincerely,

PMT
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177197
When some one is working on a job or for their own personal interests, there is nothing wrong with giving them ALL the help you can.  But when its for school, that's different.  Schools have rules regarding accademic honsesty.  In particular, you can't submit someone else's work as your own.  

But its not just a matter of rules, I think students learn better in the long run when they struggle through it themselves (to some extent)   I think it is a matter of learning from one's mistakes.  If you hand them a really great answer and say "study this" they don't really get much out of it.  but if you get them to write a crummy answer, and eventually forge it onto a good one, then they learn why the good answer was really good and what was really bad in their original approach.  Just an opinion, but I one based in experience.
0
 

Author Comment

by:appreciative
ID: 1177198
Nietod and PMT,

I wrote the following 3 paragraphs (in a couple of messages above):

>>we were just handed out this assignment on thursday afternoon .... just scribbled on a piece of paper... and we did not get too much theory... sometimes I feel as if it's a "teach yourself course"....  

>>I have 2 textbooks... I read and read and try to understand, but I understand more with someone explaining the concepts (like a teacher...)!!!!  but the course I'm taking is very concentrated in a very short period of time - 10 months  (I'm also taking assembler, unix, access/SQL, PC architecture and, unfortunately, excel, powerpoint and word (I know the latter 3, but still have long assignments) and I find myself swamped with assignments.... -- after Xmas comes Visual Basics, Java, Cobol, html (which I already know.... a break) and whatever else.... so 10 months is pretty condensed for such profound subjects... don't you think???)

>>I'm not a lazy person, I put in the hours, trust me..... but I just don't have time to revise what I learn... so when we learn something new, the next day it's some other new thing and it piles up without us having time to practice.... I sometimes work all night until 9:00 or 10:00 the next morning, but still it's not enough and would you believe that I'm one of the best in the class.... my marks are very good, but it's difficult... but hey I love programming (if it would only enter my brain), maybe I'm not cut out for this.... but I'll die trying before I quit... hard headed :)
well I guess I'm tired too... off to bed!!!
________________________

Now, when I wrote the last message I went off to bed and what do you think my thoughts were about!!!  "I'm not good at this....why can't I program by myself without having to resort to asking for help....what is it....I'm not logical enough... what, what, what!!!!  But thinking about it today, it's just that it's too much in 10 months (I started August 10), we only had approx. 2 months to learn C at 10 hrs/week, and the person that taught C and now teaches C++ only graduated -- from the exact same course I'm taking -- 2 years ago, and was never in a programming environment/job; another one of my teachers (Access&SQL) only graduated -- again, from the exact same course -- this past June, if I would have known this, I would have chosen another establishment, where teachers have teaching credentials... So, in the meantime, I'm trying to do my darn best, but realize that, however hard (and believe me, it is hard for me because my self esteem takes a dive everytime (3rd time now) I ask for help) it is for me to ask for help in C++, I have no choice because from the little we learn,  I wouldn't be able to do it by myself... and I work very, very hard at making my programs work before actually do ask for help... and when I do go to bed, it's really because I'm at the point that I feel very weak and shaky....

So please, don't think that I'm trying to get the work done by you guys and relax while you're helping, on the contrary.... and if I do get the code at the end, it does not stop there, I study the code, compare it with other codes and what I had, find my mistakes and I learn a lot from this....believe me...!!!!

I thank you all so very much and hope and pray for the day to come when I won't have to ask for help and cause problems....

Thank you...
appreciative

   
   
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177199
I was not accusing you of accademic dishonesty, nor pmteixeira of intentionally enthical behavior.  I think what he did was wrong--slighly (I've seen far more blatent cases), but I think it was an honest mistake.

You certanly did the majority of the work.yourself and I feel confident that you understand at least all that answsers2000 and I helped you with--if not all of it.  

If you have problems in the future, I'd be happy to help.

>>  pray for the day to come when I won't have to ask for help
Wrong attitude, I'm one of the top 10 experts overall (I think 6th) and I ask questions here frequently.  Learning sometimes doesn't mean havind the answers, it means knowing how to find them...
0
 

Author Comment

by:appreciative
ID: 1177200
I know Nietod that you are not accusing anyone of wrong doing and as for asking help, believe me I only do ask as a last resort when I realize that even if I spent more hours and hours I wouldn't get any further... (I don't know why but I'm kind of ashamed when I have to ask for help -- my pride I guess) and also believe that I would honestly prefer to be able to do it all by myself, then I would feel like a million bucks...

Thanks for telling me that even as an expert you also sometimes have to ask questions, makes me feel better...

and thanks for your offer to help in the future...
appreciative
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1177201
I'm answering this as requested by appreciative in an e-mail.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now