Thread: C++ Midterm understanding

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    25

    C++ Midterm understanding

    Let me just state here that I am not looking for code. I am merely looking for help understanding the problem.

    Okay. I am the unofficial tutor of a couple of people. And today they got their midterm assignment for C++. And as usuall, its rather hard to understand what the professor wants. Perhaps someone on here can help me shed some light on what is needed. Here is the assignment, word for word:

    Write down all the shapes you can think of - both 2 dimensional and 3 dimensional and form these shapes into a shape hierarchy. Your shapes should have a base class called shape from which Class TwoDimensionalShape and Class ThreeDimensionalShape are derived. Once you have developed the hierarchy, define each of these classes in the hierarchy. You are going to be using a technique called polymorphism as you modify the hierarchy so that class Shape is an abstract base class containing the interface to the hierarchy. Derive Class TwoDimensionalShape and Class ThreeDimensionalShape - - these classes should also be abstract. Use a virtual print function to outcome the type and dimension of each class. Also include virtual area and virtual volume functions so these calculations can be performed for objects of each concrete class in the hierarchy. Write a driver program that tests the Shape class hierarchy.
    So...could someone please translate what this professor wants?

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    OK, What part don't you understand???

    Polymorphism?
    Abstract base class?
    Virtual function?

    When you ask such an open-ended question, it's like you don't understand ANY of it!

    There are things that aren't clear to me, and it might be OK to ask the professor or his assistant if he has one. Also, there are usually clues in the homework assignments.

    Every 2-dimensional shape has height and width (and area). a 3-dimensional object also has depth (and volume).

    I'd say you should at least do a circle and sphere, a square and cube, a rectangle and a rectangular cube, probably an equalateral triangle and 3-sided pyramid. Beyond that, it gets more complicated to mathematically describe shapes... So, I don't know how far the professer expects you to go.
    Last edited by DougDbug; 03-15-2006 at 04:28 PM.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Abstract base classes
    virtual functions

    as dougdbug said, you need to be a bit more specific in your questions.

    Finally, no offense, but why are you "unofficially" tutoring people in a subject you're not confident about?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    This tutorial is quite similar to the problem, good luck
    http://www.cplusplus.com/doc/tutorial/polymorphism.html

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Write down all the shapes you can think of - both 2 dimensional and 3 dimensional
    Seems easy enough.
    and form these shapes into a shape hierarchy.
    Whoa! What's a "shape hierarchy"? Sounds like inheritance, but other than that it's a bit vague. Ahhh, lookee here:
    Your shapes should have a base class called shape from which Class TwoDimensionalShape and Class ThreeDimensionalShape are derived.
    The prof lays out the exact "shape hierarchy" he wants. Presumably, all the shapes you thought of will be derived from one of those two classes.
    You are going to be using a technique called polymorphism
    Polymorphism: it employs virtual functions in the base class with the derived classes "overriding" the base class function. And, there's more:
    class Shape is an abstract base class containing the interface to the hierarchy.
    An abstract class is one that has '=0' after a virtual function, and that means you can't create objects of that class, but what the heck is an "interface hierarchy"? An interface is a function, but what functions?? Ahh, again the prof comes through(whew! asking students to do some independent, creative thinking on their own would be outrageous!):
    Use a virtual print function to outcome the type and dimension of each class. Also include virtual area and virtual volume functions so these calculations can be performed for objects of each concrete class in the hierarchy.
    Spelled out chapter and verse.
    Write a driver program that tests the Shape class hierarchy.
    Write a program that creates some objects to show that all the objects you thought of at the beginning actually work when they call the functions specified using the magic of polymorphism.
    Last edited by 7stud; 03-15-2006 at 07:30 PM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Ok. I have a start on the program based off of the tutorial linked by MadCow257. However, when I compile, it gives me errors. Heres the code so far:

    Code:
    // dynamic allocation and polymorphism
    #include <iostream>
    
    using namespace std;
    
    
    class Shape 
    {
    protected:
    	int width, height, length;
    
    public:
        void set_values (int a, int b, int c)
        { 
    		width=a; height=b, length=c; 
    	}
        
    	virtual int area (void) =0;
        void printarea (void)
        { 
    		cout << this->area() << endl; 
    	}
    
    	virtual int volume(void) =0;
        void printvolume(void)
        { 
    		cout << this->volume() << endl; 
    	}
    };
    
    class TwoDimensionalShape: public Shape 
    {
    public:
    	int area(void)
        { 
    		return (width * height); 
    	}
    };
    
    class ThreeDimensionalShape: public Shape 
    {
    public:
    	int volume(void)
        { 
    		return (width * height * length); 
    	}
    };
    
    int main() 
    {
      Shape *ppoly1 = new TwoDimensionalShape;
      Shape *ppoly2 = new ThreeDimensionalShape;
      ppoly1->set_values (4,5,0);
      ppoly2->set_values (4,5,6);
      ppoly1->printarea();
      ppoly2->printvolume();
      delete ppoly1;
      delete ppoly2;
      return 0;
    }
    I get the following errors:

    error C2259: 'TwoDimensionalShape' : cannot instantiate abstract class due to following members:

    see declaration of 'TwoDimensionalShape'

    warning C4259: 'int __thiscall Shape::volume(void)' : pure virtual function was not defined
    And there are a few more warnings and errors that are basically the same, and they point to the various Class and Function definitions.

    As to why im 'tutoring' when I don't have a full grasp of the material myself... Well, tutoring was the wrong word. Its more I show them how to do things as I figure them out myself, and help them do assignments(Note: I don't do assignments for others, I just show them how to get started). I enjoy doing this stuff, and these excersices are good practice.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >As to why im 'tutoring' when I don't have a full grasp of the material myself... Well, tutoring was the wrong word. Its more I show them how to do things as I figure them out myself, and help them do assignments
    Last edited by qqqqxxxx; 03-16-2006 at 12:45 AM.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    ok in your shape class there r two purely virtual functions.

    twodimensionalshape class inherits from shape class ,but it does not define volume,but volume is still passed over to it,so 2dimshape is an abstract class.

    therefore cannot create instance of abstract class


    similar for 3dshape class which doesnt define the area function.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by qqqqxxxx
    >As to why im 'tutoring' when I don't have a full grasp of the material myself... Well, tutoring was the wrong word. Its more I show them how to do things as I figure them out myself, and help them do assignments
    So, you're their cheater?

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by 7stud
    So, you're their cheater?
    He's their dealer...

    ...and we, ofcourse, are the Columbian Code Lords. We give him the stuff and he distributes it about to the masses. Then when enough people are addicted we raise the price of the code, buy 1000s of acres in the midwest and blow up the west coast. It's full proof and is in no way anything Gene Hackman did in a movie... umm... or two.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    I help a couple of Engineering students from India who are required to take the course. They have trouble speaking english, so they have trouble understanding the teacher, and need a bit o' extra help.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    There's no justification for cheating. You are doing their assignments for them.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Quote Originally Posted by KoshiB
    (Note: I don't do assignments for others, I just show them how to get started). I enjoy doing this stuff, and these excersices are good practice.
    Someone it seems doesn't read. I don't do the work for others. I show them the door, help them understand things better when I can.

    I also do the assignments they get fully, for my own benefit. For myself...not for them. I repeat, I don't do their work for them.



    Now, as I have gotten all the information im going to get from this topic, and have figured out enough about the problem to successfully complete it on my own, I ask an admin to close this topic please. I shall not respond back in this reguardless. Thanks for your help, anyone who did offer help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 05:53 AM
  3. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  4. Replies: 8
    Last Post: 09-09-2005, 12:34 AM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM